Perl访问华为系列交换机路由器配置参数模块
[plain]
#包名:HWNET 功能:对华为交换机和路由器进行自动维护的包 引用方法:use HWNET;
#
package HWNET;
use strict;
use Data::Dump qw(dump);
use Net::Telnet;
use POSIX qw(strftime);
use Net::Ping;
#
# 用于测试的子程序
#
sub test{
#读取子程序参数
my $this = shift;
my $telnet = $this->{'TELNET_OBJECT'};
my $prompt = $this->{'PROMPT'};
$telnet->put("\n");
#my ($prematch, $match) = $telnet->waitfor("/$prompt/");
print $telnet->lastline;
return ;
}
#
# 功能:支持的设备型号列表
#
# 列表项示例:
# S8016' => 'Quidway S8016 Routing Switch'
# ^
# 设备型号 'disp ver'命令输出结果中标志设备型号的特征字符串
#
# 说明: 如果需要支持新的设备型号支持,需要在此列表中增加相应的条目
#
my %device_type_list =(
'S8016' => 'Quidway S8016 Routing Switch',
'NE05' => 'Quidway NetEngine 05',
'NE40-8' => 'Quidway NetEngine 40-8 Universal Switching Router',
'NE16' => 'Quidway NetEngine 16E',
'AR46-40' => 'Quidway AR46-40',
'S3050' => 'Quidway S3050C',
'S2026' => 'Quidway S2026',
'S2403H-EI' => 'Quidway S2403H-EI',
'S3526E' => 'Quidway S3526E',
'S2008-EI' => 'Quidway S2008-EI',
'S3050C' => 'Quidway S3050C',
);
# 返回程序包支持的设备型号列表
sub supported_device_type_list{
return keys %device_type_list;
}
#
# HWNET对象的构造器
#
sub new{
my $class = shift;
my $this = {
'STATE'=>'UNCONNECTED',
'INPUT_LOG'=>'in.txt',
'OUTPUT_LOG'=>'out.txt',
'TIMEOUT'=>5, 'PROMPT'=>''
};
return bless $this, $class;
}
#
#登录到网络设备的用户视图
#
sub reset{
my $this = shift;
# 关闭telnet对象
my $telnet = $this->{'TELNET_OBJECT'};
$telnet->close() if $telnet;
# 将各对象各属性复位到初始值
$this = {
'STATE' => 'UNCONNECTED',
'INPUT_LOG' => 'input_log.txt',
'OUTPUT_LOG' => 'output_log.txt',
'TIMEOUT' => 5,
'PROMPT' => ''
};
}
sub login{
#读取子程序参数
my ($this, $host,$username, $password,$su_password) = @_;
my $prompt = $this->{'PROMPT'};
$prompt = '(?:<.+?>|\[.+?\])$' if !$prompt;
my $ret=undef; #设置子程序默认返回值
# 验证子程序执行的前提条件
return $ret if $this->{'STATE'} ne 'UNCONNECTED';
my $input_log = $this->{'INPUT_LOG'};
my $output_log = $this->{'OUTPUT_LOG'};
my $timeout = $this->{'TIMEOUT'};
my $telnet = Net::Telnet->new(
Timeout=>$timeout,
Prompt=>"/$prompt/",
Input_log=>$input_log,
Output_log=>$output_log,
Errmode=>'return'
);
#如果建立telnet对象失败,返回
return $ret unless $telnet;
$this->{'STATE'}='CONNECTED'; #设置HWNET对象状态
#登录到普通用户模式
return $ret unless $telnet->open($host);
my (undef, $match) = $telnet->waitfor('/Password:|Username:/');
if($match eq 'Username:'){
return $ret unless $telnet->put($username."\n");  
补充:Web开发 , 其他 ,