Mark Vanmiddlesworth wrote:
> 
> this is a script that systematically pings every host on the network,
> and writes the output to the screen. If the -n arg is specified, it
> will also write to a disk. What's wrong with the syntax, especially
> line 12?
> 
> #!/usr/bin/env perl
> 
> $x = 1;
> $b = 0;
>          for ($x = 1; $x < 256; $x++) {
>                  $a = `ping -q -c 1  192.168.1.$b`;
>                  $b += 1;
>                  print "192.168.1.$b   ";
>                  print $a;
>                  $usage="ping.pl [-n]"
>                  if ($arg = "-n") {$e = 0}
>                  else {$e = 1}
>                  if ($e = 1) {
>                          open (LOG, ">>/perl/pinglog.txt";
>                          print `date`;
>                          print $a;
>                          }
>                  else {}
>                  }
> 
> Am I doing my ifs correctly? Thanks,

You should probably check the arguments and open the log file outside of
the loop.  You should use warnings and strict when developing your
program.

#!/usr/bin/env perl
use warnings;
use strict;
use Socket;
use Net::Ping;
use Getopt::Std;

my $usage = "ping.pl [-h] [-n [logfile]]\n";

my %opts;
getopt( 'hn', \%opts );

die $usage if exists $opts{h};
if ( exists $opts{n} ) {
    $opts{n} = '/perl/pinglog.txt' if $opts{n} eq '';
    open LOG, '>>', $opts{n} or die "Cannot open $opts{n}: $!";
    select LOG; $|++;
    }

my $start = inet_aton( '192.168.1.1' );
my $end   = inet_aton( '192.168.1.255' );

my $p = Net::Ping->new();

for my $ip ( $start .. $end ) {
    my $host = inet_ntoa( $ip );

    print scalar localtime, ' ' if exists $opts{n};
    print "$host is ";
    print 'NOT ' unless $p->ping( $host, 2 );
    print "reachable.\n";
    }

$p->close();

__END__


> got root?

Why yes ... yes I do.  :-)



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to