Perl统计交换机下联设备ip和mac地址
#!/usr/bin/perl #Version 0.2 #2011.5.31 use Encode; use Net::Telnet::Cisco; use Spreadsheet::WriteExcel; open HOST,"</home/host.txt"; open IP,"</home/iplist.txt"; chomp(my @host=<HOST>); close HOST; chomp(my @ip=<IP>); close IP; my $xls = Spreadsheet::WriteExcel->new( "/home/Switch.xls" ); foreach $h (@host){ print $h; my $session = Net::Telnet::Cisco->new(Host => "$h"); my $hContent = $xls->add_worksheet( "Switch_$h" ); $HH=0; $session->login('User', 'Pwd'); # Enable mode if ($session->enable("EnPWD") ) { print "Cennect is ok\n"; } else { warn "Can't enable: " . $session->errmsg; } foreach $i (@ip){ $hContent->set_column(0,100,15); my $contentStyle = $xls->add_format(); $contentStyle->set_size( 10 ); $contentStyle->set_bold(); #设置字体为粗体 $contentStyle->set_align( 'center' ); #设置单元格居中 $contentStyle->set_text_wrap(); #是否回车换行 $contentStyle->set_color('black'); #设置单元格前景色为红色 my @outputing = $session->cmd("ping $i"); my @arp = $session->cmd("show ip arp | in $i"); my @result=split( ' ' , $arp[0] ); my @mac = $session->cmd("show mac-address-table dy add $result[3]"); my @IPlist = split( ' ' , $mac[5] ); if ($IPlist[3]=~/.*\/.*/){ $HH+=1; $hContent->write("A$HH", decode( 'gb2312', "$i" ), $contentStyle ); $hContent->write("B$HH", decode( 'gb2312', "$IPlist[1]" ), $contentStyle); $hContent->write("C$HH", decode( 'gb2312', "$IPlist[3]" ), $contentStyle); } } $session->close; } $xls->close();?