我正在使用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";
您是否尝试过使用$exp->pid()
Expect 的方法。Expect 模块的文档说:
$object->pid()
Return pid of $object, if one exists. Initialized filehandles will not have pids (of course).
我已经在一个简单的测试中尝试了这个来远程登录到本地主机,并执行一个 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