1

following loop does not produce the output IF I do not have the last exp->expect(5) line (kind of sleep). I guess this is because the loop is running 'too fast' for the 'sent' command to finish. How can i make sure the next command is run only when previous one is finished? I am collecting output in log file.

any advice, pl.

ty.

use Expect;
...
...
foreach my $cmd (@cmd_array){
$exp->expect(3,
    [ qr/($|#)/ => sub { shift->send("$cmd");}]
       );
exp->expect(5);
}
4

2 回答 2

0

也许你需要使用exp_continuelike-

use Expect;
...
...
foreach my $cmd (@cmd_array){
$exp->expect(3,
    [ qr/($|#)/ => sub { shift->send("$cmd\n");
                         exp_continue;}]
       );
}

另外,我认为里面的语句sub{...}只有在模式匹配时才会运行qr/($|#)/。另外,我认为在您输入输入或命令后,您通常会按回车键来执行命令,因此$cmd\n.

于 2012-09-05T01:12:38.380 回答
0

你有什么理由不能让 Net::OpenSSH 为你做这件事吗?

从其文档中,您可以使用 capture() / capture2() 方法(取决于您是否需要从进程中仅捕获 STDOUT 或 STDOUT 和 STDERR)。

例如:

for my $command (@commands) {
    my $result = $ssh->capture($command);
}

我相信这应该有效,不是吗?

(我刚刚用一个非常简单的脚本对此进行了测试,它对我来说效果很好。)

于 2012-09-06T09:58:27.463 回答