使用Perl中的Expect获取生成的会话的会话ID

时间:2015-07-20 05:07:05

标签: perl expect.pm

我使用Expect来ssh。我想知道生成的会话的会话ID。我该怎么做?

这是我的代码:

my $addr = "10.101.10.102";
my $cmd = "ssh username@".$addr;
my $exp = Expect->spawn($cmd) or die "Cannot spawn command\n";

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用Expect的$exp->pid()方法? Expect模块的文档说:

$object->pid()
Return pid of $object, if one exists. Initialized filehandles will not have pids (of course).

我在一个简单的测试中尝试了telnet到本地主机,并执行unix ps命令来查看进程以及$exp->pid()命令,它们匹配。

use strict;
use Expect;
my $exp = Expect->spawn("ssh localhost") or die "cannot spawn command\n";
print `ps -ef | grep -i "ssh localhost\$"` ;
print "PID of spawned process is: ", $exp->pid(), "\n";

<强>输出

providnt  4389  4302  0 09:42 pts/1    00:00:00 ssh localhost
PID of spawned process is: 4389