Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I had copious free time on my hands, so I decided to give this a once-over. I tried to scribble down what I was doing and why in the comments, but do ask if there's anything confusing that I changed -- either in how I diddled with it, or why.

Yeah, it looks a lot more verbose, but it's actually a bit more concise, with copious comments. Which come would say is a good thing. ;>

#!/usr/bin/perl -w ## Always use warnings # # Run with no arguments for help # # This program scans a C-class subnet and prints the results to corres +ponding text file. # If a computer has once answered the ping, it will not be added to th +e list again. # To sort the output type 'perl sort <textfile>' use strict; use Net::Ping; ## Unbuffer your putput so we see it in real time $|++; ## Let's be nice and be case-insensitive.. sort_file($ARGV[1]) if lc $ARGV[0] eq 'sort'; my $subnet = shift @ARGV; ## Usage always exits, so we can be succint here.. usage() unless $subnet; ## Periods don't have to be escaped.. # open OF,">>$subnet.txt" or die "Can't create $subnet.txt: $!"; ## ..and ALWAYS check the return value of open! ## And it turns out that we'll end up doing this shortly anyways, so w +e can take it out here. ## This look isn't useful, really. Hash values start out being undefi +ned. # for (1 .. 255){ ## You could also save yourself a variable AND roll it into one line. # $state{"$subnet.$_"}='' # } ## I pulled this object out of the loop -- we can use the same Net::Pi +ng object over and over. my $p = Net::Ping->new('icmp'); my %state; while () { ## Always check the return value of open.. open OF,">>$subnet.txt" or die "Can't create $subnet.txt: $!"; ## Unbuffer the OF filehandle -- that way, you can tail the file, an +d killing the process won't lose any data select OF; $|++; select STDOUT; ## Scope stuff as tightly as possible -- hence the my $h right here, + not up top. for my $h (1 .. 254){ #sleep 2; ## Ditto with scoping $host to this block. ## Again, period arn't special or anything in double quotes. my $host = "$subnet.$h"; print "\nPinging host $host"; if ($p->ping($host,1)) { ## If we didn't know it was up, spew that out now.. print OF "$host\twas up at ", scalar localtime, "\n" unless $sta +te{$host}; ## Update the number of times we've seen it up $state{$host}++; ## And let STDOUT know about it, too. print " alive ($state{$host} times)"; } else { print " down"; } } close OF; sleep 600; } sub usage { ## HERE documents are your friend for this kind of thing: print <<"EO_USAGE"; This program scans a C-class subnet and lists all machines, that have answered ping in a text file. You can also use this program to sort the output. Usage: perl $0 <subnet> Example: perl $0 192.168.0 Sort: perl $0 sort <file> Example: perl $0 sort 192.168.0.txt EO_USAGE exit; } ## Naming subroutines the same name as builtins is vaguely sketchy, ## and probably good practice to avoid doing. sub sort_file { ## Find out the filename by what we were passed. my $file = shift; usage() unless $file; ## Good error checking here. ++ open IF,"$file" or die "Cannot open file $file for read: $!"; ## I moved the read and close all together up here ## It's a little more obvious what you're doing this way my @data=<IF>; close IF; ## Not overly useful variable $of cleaned out -- it was only used on +ce ## ..and always check the return value of open. Is there a pattern +here? ;> open OF,">s.$file" or die "Cannot open file s.$file for write: $!"; ## Any particular reason you were using grep here instead of map? my @sorted = map {s/(^|\D)0+(\d)(?=\t)/$1$2/g; $_} sort map {s/(\d+)(?=\t)/sprintf"%03.3d",$1/ge; $_} @data; ## You can print it all in one go -- no need for a foreach. Just pr +int the array print OF @sorted; close OF; exit; }

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

In reply to Re: Scan C-Class for used IP-addresses by Chmrr
in thread Scan C-Class for used IP-addresses by cab

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2018-05-27 14:26 GMT
    Find Nodes?
      Voting Booth?