- 论坛徽章:
- 0
|
这个礼拜开始学习Perl,断断续续快看完Learning Perl了。
前段时间公司局域网频频中招,老是有病毒实施ARP欺骗,即自己伪装成网关或将其他IP指向自己的MAC地址,每次发作时公司网络即时通时断,每次都还得到交换机上查找犯事的机器,于是就想利用Perl写个小程序监控各台机器的MAC地址,功能为扫描网络内部所有机器的IP地址和MAC地址并记录于数据库内,病毒发作时再次运行程序扫描,如某IP的MAC地址发生变化则提示我并记录在案。
下午折腾了半天才装好Nmap: arser,在家里的小区局域网内测试了一下,似乎可以正常工作了,弄完才有点怀疑这个程序的作用,不知道那个病毒发作时是仅仅伪造一个假的IP->MAC记录告诉网关还是真的改变了其他IP机器的MAC地址?
管它了,好歹是第一个程序了,贴上来看看,说不定还有人给出可行的办法呢
- #!/usr/bin/perl
- use Nmap::Parser;
- dbmopen(%NMAP,"nmapscan",0644) or die "Cannot open nmapscan:$1";
- #record them in a small databse
- open WARNMAC, ">>warnmac.csv" or die "Cannot open file:$1";
- #record it if mac address changed
- open NEWIP, ">>newip.csv" or die "Cannot open file:$1";
- #record it if found new ip
- $nmap_path='/usr/bin/nmap';
- $nmap_args='-n -sP';
- @ips=@ARGV;
- $nmapscan=new Nmap::Parser;
- $nmapscan->parsescan($nmap_path,$nmap_args,@ips);
- foreach $host ($nmapscan->all_hosts(up))
- {
- if(!$NMAP{$host->addr})
- {
- $NMAP{$host->addr}=$host->mac_addr;
- print NEWIP $host->addr,",",$host->mac_addr,"\n";
- print "New found IP:\n",$host->addr,"\t",$host->mac_addr,"\n";
- }
- elsif($NMAP{$host->addr} ne $host->mac_addr)
- {
- print WARNMAC $host->addr,",",$NMAP{$host->addr()},",",$host->mac_addr,"\n";
- print "mac address of ",$host->addr,"have changed,check warnmac.csv\n";
- }
- }
复制代码
附注:写程序时清理一下桌面,rm test*时不小心在*前面空了一格,然后,然后学习过程中那些Excerise的程序全部消失了…… |
|