Arp-сканер на perl. Версия 0.4

Немного исправил и допилил. Теперь не используется промежуточный лог-файл, и заодно парсится файлик со списком комнат.

#!/usr/bin/perl -w
#Arping addon by Snake
#http://snake.khd.ru
#2010-10-02
#v0.4
use Shell;
use DBI;
my @h51;
my @h5;
print "Scan 10.12.51.0/24n";
for ($i=1; $i<255; $i++){
    my $ip="10.12.51.$i";
    my @h5_5=`/usr/local/sbin/arping -i em0 -c 5 -w 5 $ip`;
    push (@h51, @h5_5);
    }
print "Scan 10.12.5.0/24n";
for ($i=1; $i<255; $i++){
    my $ip="10.12.5.$i";
    my @h5_3=`/usr/local/sbin/arping -i vlan1205 -c 5 -w 5 $ip`;
    push (@h5, @h5_3);
    }
print "Connect to databasen";
my $db = DBI->connect('DBI:mysql:arpbase;host=localhost', 'arpuser', 'arppass'
               ) || die "Could not connect to database: $DBI::errstr";
#Last scanned addresses;
print "Updating databasen";
#Flush online status
$db->do("UPDATE iptable SET online = 0 WHERE online = 1");
my $j;
#Update database
foreach (@h5){
    my $line=$_;
    if (($dstmac,$dstip)=$line=~/60 bytes from (.{17}) ((.*?)): index=d* time=d*.d* msec/){
        if ($dstmac ne $oldmac || $dstip ne $oldip){
            $db->do("UPDATE iptable SET mac = '$dstmac', online = 1 WHERE ip = '$dstip'");
            #print "Write to database: $dstip $dstmac";
        }
        $oldip=$dstip;
        $oldmac=$dstmac;
        ++$j;
    }
}
foreach (@h51){
    my $line=$_;
    if (($dstmac,$dstip)=$line=~/60 bytes from (.{17}) ((.*?)): index=d* time=d*.d* msec/){
        if ($dstmac ne $oldmac || $dstip ne $oldip){
            $db->do("UPDATE iptable SET mac = '$dstmac', online = 1 WHERE ip = '$dstip'");
            #print "Write to database: $dstip $dstmac";
        }
        $oldip=$dstip;
        $oldmac=$dstmac;
        ++$j;
    }
}

open(ROOM, "iptable") || die "Can't open file: $!";
while(){
    chomp;
    my $line=$_;
    ($room,$roomip)=$line=~/(d*): (.*)/;
    @roomip=split/, / => $roomip;
    foreach (@roomip){
        $db->do("UPDATE iptable SET room = '$room' WHERE ip = '$_'");
    }
}
close(ROOM);

Leave a Reply