以下函数登录到路由器,执行命令以获取IPsec会话状态并以字符串形式返回接口名称和IP地址。而不是返回一个字符串,我希望该函数返回散列数组。有人可以帮我解决这个问题吗?在perl中返回哈希数组
sub cryptoSessionStatus {
my ($self,$interface) = @_;
my $status = 0;
my $peer_ip = 0;
#command to check the tunnel status
my $cmd = 'command goes here ' . $interface;
#$self->_login();
my $tunnel_status = $self->_login->exec($cmd);
#Regex to match the 'tunnel status' and 'peer ip' string in the cmd output
#Session status: DOWN/UP
#Peer: x.x.x.x
foreach my $line ( $tunnel_status) {
if ($line =~ m/Session\s+status:\s+(.*)/) {
$status = $1;
}
if ($line =~ m/Peer:\s+(\d+.\d+.\d+.\d+)/) {
$peer_ip = $1;
}
}
return ($status,$peer_ip);
}
函数调用:
my $tunnel_obj = test::Cryptotunnels->new('host'=> 'ip');
my $crypto_sessions = $tunnel_obj->cryptoSessionStatus("tunnel1");
你的意思是你想要有一个散列Status => Ip?你想那样回来? –
@Ed Dunn我想要ip和状态作为哈希阵列返回Array =( { ip =>“x.x.x.x”, status =>“up”, } ); – virat
输出结果如何?我们能拿到样品吗? –