lanier has asked for the wisdom of the Perl Monks concerning the following question:

this is my script to test whether or not I can ftp for the specified user for each host. But in only does the first host and never continues and I can't figure out what is wrong. I had the list of hosts in a file then I did an array as shown below and still nothing. PS. Thanks to the users who told me about Net::FTP.
#! /u/ss/bin/perl -w use Net::FTP; $ntsrv="ntsrv"; @number=qw(143 148 171 177 180 184 186 191 207 225 226 229 471 530 593 + 661 663 669 672 673 674 675 676 677 673 674 675 676 677 678 679 679A 681 685 687 691 700 702 703 7 +06 707 709 711 715 717 731 732 734 737 738 741 743 749 751 755 761 762 768 769 773 777 779 780 78 +1 788 821 824 828 875 886); $count=0; $in=" "; while ($count <= $#number) { $in=$ntsrv.$number[$count]; print "$in \n"; $ftp=Net::FTP->new("$in") or warn "Can't open $in: $!" ; $ftp->login("sfouser",'password') or warn "Can't login: $!"; if (!defined $ftp->ls){ print "Directory is empty \n"; } else { print $ftp->ls; print "\n"; } $ftp->put("test.txt") or warn "Can't transfer file: $!"; print $ftp->ls; print "\n"; $ftp->delete("test.txt") or warn "Can't delete file: $!"; if (!defined $ftp->ls){ print "Directory is empty \n"; } else { print $ftp->ls; print "\n"; } $ftp->quit() or warn "Can't quit from $in: $!"; print "finished\n \n"; $count++; }

Replies are listed 'Best First'.
Re: Problems with Loop
by Helter (Chaplain) on Sep 06, 2002 at 21:13 UTC
    I know I may be nit-picking, but in this case I think it is more readable, and makes more sense to use a foreach loop here.

    #!/usr/bin/perl -w use strict; my $ntsrv="ntsrv"; my @number=qw(143 148 171 177 180 184 186 191 207 225 226 229 471 530 +593 661 663 669 672 673 674 675 676 677 673 674 675 676 677 678 679 6 +79A 681 685 687 691 700 702 703 706 707 709 711 715 717 731 732 734 7 +37 738 741 743 749 751 755 761 762 768 769 773 777 779 780 781 788 82 +1 824 828 875 886); my $in=" "; foreach $in (@number) { $in=$ntsrv.$in; print "$in \n"; --------------Insert ftp code here (not installed at my site...)---- print "finished\n \n"; }


    The loop you had worked fine for me, so maybe it's not the loop that is not working, but your code is hanging when it is trying to connect to the first ftp server? Try debugging the code:
    perl -d file.pl "n" to execute the next line
    It can do lots of useful stuff and is well worth learning. My O'Reilly "Programming Perl" has a good tutorial that I always refer to, I don't know off the top of my head of any online tutorials to the debugger. Good luck!
Re: Problems with Loop
by Marza (Vicar) on Sep 06, 2002 at 20:37 UTC

    I ran your code with no problems. What are the errors you seeing? Is it something like:

    synsun1000 Can't open synsun1000: Invalid argument at D:\Sysad-perl\perltest\test +1.pl line 13. Can't call method "login" on an undefined value at D:\Sysad-perl\perlt +est\test1. pl line 14.

    If that is similar to what you are getting then the problem is that your second system is not available or does not exist.

    If not, you might consider puting an availability check if your systems are not always up.

    update

    Another passing thought from looking at your code. I see "ntsrv" if they are nt servers, they have to have the FTP deamon running. So best bet is to show the error you are getting.

Re: Problems with Loop
by blaze (Friar) on Sep 06, 2002 at 19:57 UTC
    well, i cant see anything wrong with the code itself (besides the missing 'use strict' :) ), maybe someone else can find problems with the code...as far as general troubleshooting have you tried to print $#number to make sure it is 69 or tried to print foreach @number just to make sure @number has what it should have
Re: Problems with Loop
by traveler (Parson) on Sep 06, 2002 at 20:57 UTC
    Marza's idea is a good one, send the output. Before you do, put a print before and after the login. If you program just appears to "hang", maybe Net::FTP waits a long time trying to connect.

    HTH, --traveler

Re: Problems with Loop
by lanier (Initiate) on Sep 09, 2002 at 14:31 UTC
    Update It was not my code. Some of the servers were down and the password was not consistent with every server. Thanks for your help.

      As Helter said I too may be nit-picking but technically it is your code. If you have a "reasonable" probability of hitting something, you should code for it. Scripts blowing up is not pretty.

      Consider this as well: I have worked for some pretty computer lame bosses and if they saw your script blowup, they would not think "Oh the machine is down" they would ask "What is wrong with your scipt?"

      If it were me, I would put in something to deal with machine outage and password issues. But again that is just me! ;-)

      Glad to see you solved it!