-->
调用nbtscan并分析产生的结果,将mac地址保存到数据文件中,列出更新的数据!
nbtscan的主页为:http://www.unixwiz.net/tools/
此处用到两个模块,运行脚本前先安装模块:
cpan Getopt::Std
cpan IO::Prompt
首先生原始数据:
[root@supersun macip]# ./ipmac.pl -i m
File exists,Continue?Y
检查更新:
[root@supersun macip]# ./ipmac.pl -f m
Nhost: 192.168.1.16
Now mac: 00:16:76:58:f8:fa
Desc: WORKGROUP\QIN-0796344B3B3
Add it ?(y/other): y
Chost: 192.168.1.46
Last mac: 10:16:96:0c:86:7c
Now mac: 00:16:96:0c:86:7c
Desc: WORKGROUP\1E66F457EDDD4CA
Save it ?(y/other): y
脚本如下:
#!/usr/bin/perl
use strict;
use Getopt::Std;
use IO::Prompt;
my $network='192.168.1.0/24';
my %opts;
my %hosts_file=();
my %hosts_scan=();
my ($hosts_change,$hosts_new);
#参数:
# -i 初始化文件名
# -e 是否交互
# -f 数据文件
# -n 扫描范围
getopt("ifn",\%opts);
useage() and exit if scalar(@ARGV) eq 0;
$network=$opts{n} if exists $opts{n};
#初始化数据文件
if(exists $opts{i}){
if( -f $opts{i} ){
unless (prompt -YESNO,"File exists,Continue?"){
exit;
}
}
#扫描网络
%hosts_scan=nbtscan($network);
#打印输出
putout(\%hosts_scan,$opts{i});
exit;
}
#查看数据变动
#扫描网络
%hosts_scan=nbtscan($network);
#导出数据
%hosts_file=dataexport( $opts{f} );
#查找数据变动
($hosts_change,$hosts_new)=whochange(\%hosts_scan,\%hosts_file);
foreach my $ip(@$hosts_new){
print "Nhost: $ip\n";
print "Now mac: $hosts_scan{$ip}{mac}\nDesc: $hosts_scan{$ip}{desc}\n\n";
if(prompt -one_char,-yes,"Add it ?(y/other): "){
unshift @{$hosts_file{$ip}},$hosts_scan{$ip};
}
print "\n";
}
foreach my $ip(@$hosts_change){
print "Chost: $ip\n";
print "Last mac: $hosts_file{$ip}[0]{mac}\nNow mac: $hosts_scan{$ip}{mac}\nDesc: $hosts_scan{$ip}{desc}\n";
if(prompt -one_char,-yes,"Save it ?(y/other): "){
splice(@{$hosts_file{$ip}},0,0,$hosts_scan{$ip});
}
print "\n";
}
open FD,'>',$opts{f};
foreach my $ip (keys %hosts_file){
my @ip_data=@{$hosts_file{$ip}};
print FD "$ip\n";
foreach my $num (0..$#{$hosts_file{$ip}}){
print FD "mac: $ip_data[$_]{mac}\n";
print FD "desc: $ip_data[$_]{desc}\n";
print FD "----\n";
}
print FD "\n";
}
sub useage {
print <<"__USEAGE__";
始初化:
$0 -i filename [-n NETWORK]
更新数据:
$0 -m filename [-n NETWORK]
NETWORK举例(默认为192.168.1.0/24):
192.168.12.0/24
192.168.12.64-97
__USEAGE__
}
#此子例程作用是扫描内网,将扫描结果存入关联数组
#关联数据组结构为:
#一级关键字为:ip
#二级关键字为:mac(物理地址)、desc(描述与共享主机名有关)
sub nbtscan{
my $network=shift;
open NBTSCAN,"nbtscan -m $network 2>/dev/dull |";
my %hosts;
while(<NBTSCAN>){
my $utile=$_;
chomp $utile;
my @host=split /\s+/,$utile;
my $ip=shift @host;
$hosts{$ip}{desc}=shift @host;
$hosts{$ip}{mac}=shift @host;
}
return %hosts;
}
#用于初始化打印
sub putout{
#传入数据关联数组引用和对象文件句柄
my $hostref=shift;
my $file=shift;
open FD,'>',$file;
my %hosts=%$hostref;
foreach my $ip(sort keys %hosts){
print FD "$ip\n";
print FD "mac: $hosts{$ip}{mac}\n";
print FD "desc: $hosts{$ip}{desc}\n";
print FD "----\n";
print FD "\n";
}
close FD;
}
#读取数据文件
sub dataexport{
my $file=shift;
my %hosts;
open FD,$file;
$/='';
while(<FD>){
my $cc=$_;
chomp $cc;
my @util=split /\n/,$cc;
my $ip=shift @util;
my $index=0;
foreach my $i (@util){
$index++,next if $i =~ /\Q----\E/;
$i =~ m/(.*?): (.*)/;
my ($key,$value)=($1,$2);
$hosts{$ip}[$index]{$key}=$value;
}
}
$/='\n';
close FD;
return %hosts;
}
sub whochange {
my ($hosts_scan,$hosts_file)=@_;
my @hosts_change=();
my @hosts_new=();
foreach my $ip (keys %$hosts_scan){
if(exists $hosts_file{$ip}){
splice(@hosts_change,0,0,$ip) if $$hosts_file{$ip}[0]{mac} ne $$hosts_scan{$ip}{mac};
}else{
unshift @hosts_new,$ip;
}
}
return \@hosts_change,\@hosts_new;
}

发表评论