Category: | Networking Code |
Author/Contact Info | |
Description: | This program scans trough a given C-Class subnet and lists the addresses that respond to ping to a file. I used it to list the addresses on our network, so i could find unused addresses. It's not pretty or witty, but it worked for me. The script keeps on scanning, until interrupted. Although the replys are written only once to the file. Use the code with option 'sort <filename>' to sort the output It uses a sort routine suggested by tye in the QA section on sorting. (Thanks for that) Any suggestions are welcome. This is also my first code post, so be gentle. /cab |
#type 'perl scansub.pl' for help # #This program scans a C-class subnet and prints the results to corresp +onding text file. #If a computer has once answered the ping, it will not be added to the + list again. #To sort the output type 'perl scansub.pl sort <textfile>' use strict; use Net::Ping; my $host; my $subnet; my %state; my $h; my $time; my $if; my $of; my $line; if ($ARGV[0] eq 'sort'){ &sort; exit; } unless ($subnet=$ARGV[0]){ &usage; exit; } open OF,">>$subnet\.txt"; for $h(1 .. 255){ $host="$subnet\.$h"; $state{$host}='' } while(){ open OF,">>$subnet\.txt"; for $h(1 .. 254){ #sleep 2; $host="$subnet\.$h"; my $p=Net::Ping->new('icmp'); print "\nPinging host $host"; if ($p->ping($host,1)){ print " alive"; if ($state{$host} ne 'up'){ $time=localtime(); print OF "$host\twas up at $time\n"; $state{$host}='up'; } } } close OF; sleep 600 } sub usage{ print" This program scans a C-class subnet and lists all machines, that have answered ping, in a text file. Use this program to sort the output. Usage: perl scansub.pl <subnet> Example: perl scansub.pl 192.168.0 Sort: perl scansub.pl sort <file> Example: perl scansub.pl sort 192.168.0.txt "; } sub sort{ unless ($if=$ARGV[1]){ &usage; exit; } open IF,"$if" or die "Cannot open file $if for read: $!"; $of=">s.$if"; open OF,$of; my @data=<IF>; my @sorted=grep {s/(^|\D)0+(\d)/$1$2/g,1} sort grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @data; foreach $line(@sorted){ print OF $line; } close IF; close OF; exit; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scan C-Class for used IP-addresses
by Chmrr (Vicar) on Dec 07, 2001 at 18:41 UTC | |
Re: Scan C-Class for used IP-addresses
by grinder (Bishop) on Dec 10, 2001 at 18:09 UTC | |
Re: Scan C-Class for used IP-addresses
by cab (Beadle) on Dec 07, 2001 at 16:41 UTC | |
by Chmrr (Vicar) on Dec 07, 2001 at 17:54 UTC | |
by cab (Beadle) on Dec 07, 2001 at 18:21 UTC |
Back to
Code Catacombs