Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Creating Loop for unix ping command in perl 1

Status
Not open for further replies.

geoffinva3

Technical User
Feb 15, 2005
9
US
I tried Perl and wrote the following bit of code: Now I need to not just print each line in the file to standard out but I want to ping each ip address on each line.
Any ideas?

Thanks


1: #!/usr/local/bin/perl

2:

3: if (open(MYFILE, "file1")) {

4: $line = <MYFILE>;

5: while ($line ne "") {

6: print ($line);

7: $line = <MYFILE>;

8: }
 
Not tested:
Code:
#!/usr/local/bin/perl
use strict;
use warnings;

#your ping command, with any options, e.g. "ping -n 2"
my $pingcmd = "yourcommandhere"; 

open(MYFILE, "file1") || die qq(Can't open "file1" for read!\n);

while(my $line = <MYFILE>) {
    chomp $line;
    print "$line\n";
    my $cmd = "$pingcmd $line";
    my @results = qx($cmd);
    for my $r (@results) {
        print $r;
    }
}
HTH
 
I get the error:

bash-2.03$ test.ovpl testfile
bash: ./test.ovpl: No such file or directory
 
Mike the file exists I created it form your input and called it test.ovpl did a chmod +x on it and issued it as
./test.ovpl and the filename containing the IP's
 
bash: ./test.ovpl: No such file or directory
Your shell is telling you there's no file in your current directory named test.ovpl. Are you sure that's the correct name and it exists in the directory you're trying to run the script from? (Or that the directory where the script exists is in your execution search path?)

Also, if you want to read your input filename from the command line (your post didn't indicate this), you need to make a little change to the script:
Code:
[b]my $file1 = shift;
chomp $file1;[/b]
open(MYFILE, "[b]$file1"[/b]) || die qq(Can't open "[b]$file1[/b]" for read!\n);
...

 
You are the MAN!!!!!!!!!!!!!!!!!! it was my perl path

It was in a non-standard location

/opt/OV/bin/Perl/bin/perl


wooohooo


thanks and thanks for the extra stuff


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top