当前位置: 代码迷 >> 交换机/路由器 >> 用Perl访问华为交换机配备示例
  详细解决方案

用Perl访问华为交换机配备示例

热度:9407   发布时间:2013-02-26 00:00:00.0
用Perl访问华为交换机配置示例
#!/usr/bin/perl -wuse lib '../hwlib';use Net::Telnet;use Time::Local;use POSIX qw(strftime);use HWNET;use Net::Ping;my $p = Net::Ping->new();my $host='10.49.64.1';if(!$p->ping($host)){    print STDERR "Can NOT ping '$host' !";    goto r2;}my $hw = new HWNET;$hw->login($host, 'admin', 'pass1', 'pass2');$hw->super();# 取得交换机时间my @sw_clock = $hw->exec_cmd('disp clock');goto r1 if @sw_clock == 0;# $4年$5月$6日 $1时$2分$3秒;my $re_time = '(\d{2}):(\d{2}):(\d{2}) UTC [a-zA-Z]{3} (\d{4})/(\d{2})/(\d{2})';$sw_clock[0] =~ /$re_time/;my $time = timelocal($3,$2,$1,$6,$5-1,$4);#my $s =localtime($time);#print 'ok' if time < $time;# 取得最近12条logbuffer 记录my @log = $hw->exec_cmd('disp logb size 10');my %arp_hash=();#%Jan 29 11:42:01 2007my $re = '^%[a-zA-Z]{3} \d{2} \d{2}:\d{2}:\d{2} \d{4} .+? sourced by ([0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}) on';for(@log){    next if !($_ =~ /$re/i);    $arp_hash{$1} = 1 if !exists($arp_hash{$1});    $arp_hash{$1} = $arp_hash{$1} + 1;}for(keys %arp_hash){    print STDERR "有毒的机器MAC地址为:$_", "\n" if($arp_hash{$_} > 1);}print STDERR '没有发现中毒的MAC机器地址!', "\n" if keys %arp_hash == 0;r1:$hw->logout;r2:$p->close();