在 Perl 中使用 Expect 获取生成会话的会话 ID
Get session ID of spawned session using Expect in Perl
我正在使用 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";
您是否尝试过使用 Expect 的 $exp->pid()
方法。 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
我正在使用 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";
您是否尝试过使用 Expect 的 $exp->pid()
方法。 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