<?xml version="1.0" encoding="windows-1252"?>
<node id="130178" title="Scan C-Class for used IP-addresses" created="2001-12-07 11:23:59" updated="2005-08-11 07:09:15">
<type id="1748">
sourcecode</type>
<author id="115065">
cab</author>
<data>
<field name="doctext">
&lt;TT&gt;&lt;font size=-1&gt;
&lt;CODE&gt;
#type 'perl scansub.pl' for help
#
#This program scans a C-class subnet and prints the results to corresponding 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 &lt;textfile&gt;'

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'){
	&amp;sort;
	exit;
}

unless ($subnet=$ARGV[0]){
	&amp;usage;
	exit;
}
open OF,"&gt;&gt;$subnet\.txt";


for $h(1 .. 255){
	$host="$subnet\.$h";
	$state{$host}=''
}


while(){
open OF,"&gt;&gt;$subnet\.txt";
for $h(1 .. 254){
	#sleep 2;
	$host="$subnet\.$h";
	my $p=Net::Ping-&gt;new('icmp');
	print "\nPinging host $host";
	if ($p-&gt;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 &lt;subnet&gt;
Example:	perl scansub.pl 192.168.0

Sort:		perl scansub.pl sort &lt;file&gt;
Example:	perl scansub.pl sort 192.168.0.txt

";
}

sub sort{
	unless ($if=$ARGV[1]){
		&amp;usage;
		exit;
	}
open IF,"$if" or die "Cannot open file $if for read: $!";
$of="&gt;s.$if";
open OF,$of;

my @data=&lt;IF&gt;;
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;
}
&lt;/CODE&gt;
&lt;/font&gt;&lt;/TT&gt;</field>
<field name="codedescription">
This program scans trough a given C-Class subnet and lists the addresses that respond to ping to a file.&lt;br&gt;
I used it to list the addresses on our network, so i could find unused addresses.&lt;br&gt;
It's not pretty or witty, but it worked for me.&lt;br&gt;
The script keeps on scanning, until interrupted. Although the replys are written only once to the file.&lt;br&gt;
Use the code with option 'sort &lt;filename&gt;' to sort the output&lt;br&gt;
It uses a sort routine suggested by tye in the QA section on sorting. (Thanks for that)&lt;br&gt;
Any suggestions are welcome.&lt;br&gt;
This is also my first code post, so be gentle.&lt;br&gt;&lt;br&gt;
/cab</field>
<field name="codecategory">
Networking Code</field>
<field name="codeauthor">
</field>
</data>
</node>
