我创建了一个功能,它基本上登录到路由器,执行命令,解析命令输出并打印Peer status
和Peer ip
函数按预期返回值。除了状态和对等IP之外,如果我想返回3个值,例如:$ session_id,$ session_state和$ time。我如何用哈希做到这一点?我希望函数返回哈希中的所有五个值。
sub session_status {
my ($self,$interface_name) = @_;
my $status = 0;
my $peer_ip = 0;
#command to chek the status
my $cmd = 'show crypto session interface ' . $interface_name;
$self->_login();
my $tunnel_status = $self->exec($cmd);
#Regex to match the 'tunnel status' and 'peer ip' string in the cmd output
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_status, $peer_ip) = $sshobject->session_status("Tunnel1");
print "TUNNEL STATUS : $tunnel_status\n";
print "PEER IP : $peer_ip\n";
输出:
TUNNEL STATUS : DOWN
PEER IP : 100.81.1.42
答案 0 :(得分:2)
试试这个:
return { 'status' => $status, 'peer_ip' => $peer_ip,
'session_id' => $session_id, 'session_state' => $session_state,
'time' => $time };
然后你可以这样做:
my $stuff = $sshobject->session_status("Tunnel1");
print "TUNNEL STATUS : $stuff->{'status'}\n";
print "PEER IP : $stuff->{'peer_ip'}\n";
# etc