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

#!/usr/bin/perl -w
use 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 2007
my $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();

你可能感兴趣的:(用Perl访问华为交换机配置示例)