当我尝试传递无效的主机名时,代码将进入无限循环。
my $s = Net::Appliance::Session->new({
personality => 'ios',
transport => 'SSH',
host => $ip
});
有没有办法克服这个错误?
编辑:这是我的完整代码:我使用子程序下载我的网络设备的配置文件。当我在 download_config 中传入无效的 IP 地址时,它将进入无限循环。
sub download_config
{
my ($ip) = @_;
my $s = Net::Appliance::Session->new({
personality => 'ios',
transport => 'SSH',
host => $ip,
Timeout => 1
});
$s->set_global_log_at('debug'); # maximum debugging
eval {
$s->connect({ username => $username, password => $password });
$s->begin_privileged({ password => $enable_password });
#get hostname to set the file name
$hostname_result = $s->cmd('sh run | inc hostname');
$hostname_result =~ m/hostname (.*)/;
$hostname = $1;
#download the file
my @running_config = $s->cmd('sh run');
@running_config = @running_config[ 2 .. (@running_config -1)];#remove header and footer of the file
open(FH, "> temp/".$hostname.".txt") or die("Cannot open config file : $!");
print FH @running_config;
close FH;
$s->end_privileged;
};
if ($@) {
#when the login details are wrong
print redirect('../../na/unauthorised.html');
}
$s->close;
}