You may want to have a look at Net::Ping, but that isn't what you asked....

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

Where is your 'use strict' 'use warnings'?

$x = 1;
$b = 0;
        for ($x = 1; $x < 256; $x++) {
better written as:

foreach my $x (1 .. 255) {

                $a = `ping -q -c 1  192.168.1.$b`;
                $b += 1;
                print "192.168.1.$b   ";
                print $a;
                $usage="ping.pl [-n]"
Missing semi-colon....

                if ($arg = "-n") {$e = 0}
                else {$e = 1}
                if ($e = 1) {
                        open (LOG, ">>/perl/pinglog.txt";
Missing parentheses. And you should always check to see if open succeeded:

open(LOG,">>/perl/pinglog.txt") or die "Can't open log for appending: $!";

                        print `date`;
                        print $a;
                        }
                else {}
                }

While not using semi-colons at the end of blocks is syntactically correct, IMHO it is a very *bad* habit to get into. As it leads to the above.

Couple other things,

1. Name your variables something useful, you have x, b, a, e, and arg, all of which tell yourself and future readers nothing about what they are doing.

2. Theoretically your script could open and close the same file to append to 255 times, you should check to see if you need to open the log before the foreach. Then print to the log if need be during the foreach, but don't reopen it, especially since you aren't closing it, which brings us to.

3. Remember to close your open files when you are done with them.

Are you in a learning mode, or writing production code?

http://danconia.org


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

Reply via email to