我正在尝试使用以下命令执行远程脚本Net::SSH::Expect
方法是使用 perl 模块连接到其中一台服务器。
$ssh->exec("python test.py");
脚本如下所示:
#!/usr/bin/perl
use Net::SSH::Expect;
my $ssh = Net::SSH::Expect->new (
host => 'hostip', #deb-2-scripting ip
user => 'user',
password => 'password',
raw_pty => 1
);
..
..
$result = $ssh->exec("python test.py");
..
..
$ssh->close();
实际上,这个远程脚本(test.py)
执行所需的时间比预期的要长(因为它内部有很多检查(。
- 有什么方法可以
(test.py)
触发此脚本并继续执行我的perl脚本(我不想等到 test.py 执行完成(。 或 - 我怎么能等到 test.py 执行完成而不超时。 因为我可以通过提供
$ssh->exec("python test.py", 10);
看到它等待 10 秒,以便test.py
执行成功。
由于传递给$ssh->exec("command")
的命令是由服务器上的 shell 运行的,因此您可以像在普通 shell 命令一样在后台运行 Python 命令,方法是在命令末尾包含 &(与号(:
$ssh->exec("python test.py &");
这将导致exec()
立即返回,而 python 命令在服务器上作为后台进程运行。
编辑:
似乎$ssh->exec($cmd, $timeout)
无法确切地说出$cmd
何时完成。我想知道这是否是一个错误?它始终等待$timeout
秒,即使命令在此之前完成也是如此。解决方法是将$ssh->send()
与如下所示的$ssh->waitfor()
结合使用:
$str = '__DONE__';
$ssh->send("python test.py; echo $str");
my $result = $ssh->waitfor($str, undef); #<-- undef means wait for ever or
# until $str is matched.