通过 $self->send() 在 Perl Expect 模块中的远程主机上获取执行的命令的输出 [英] Get the output of a command executed via $self->send() on a remote host in Perl Expect module

查看:39
本文介绍了通过 $self->send() 在 Perl Expect 模块中的远程主机上获取执行的命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Perl 中使用 Expect 模块来执行交互式活动并在远程机器上执行命令.在这方面请求您的帮助

I am using Expect module in Perl to do an interactive activity and execute a command on a remote machine. Request your help on this

这是我使用的步骤

将用户切换到另一个帐户.发送登录密码.获得新用户的 shell 后,执行 ssh 命令以连接到远程机器.

Do a switch user to another account. Sends the password to login. Once I get the shell for the new user, execute a ssh command to connect to a remote machine.

然后我想在那个远程机器上执行一个命令并得到它的响应.我能够在远程机器上执行命令.我也在我的终端上看到输出.但是我无法在变量中捕获它,以便我可以将它与值进行比较.

Then I want to execute a command in that remote machine and get its response. I am able to execute the command on the remote machine. I am seeing the output on my terminal too. But I am not able to capture it in a variable so that I can compare it against a value.

use Expect;   
my $exp = Expect->new;
$exp->raw_pty(1);

$exp->spawn('su - crazy_user') or die "Cannot spawn switch user cmd: $!\n"
$exp->expect($timeout,
  [ qr/Password:/i,
            sub {   my $self = shift;
                    $self->send("$passwd\n");
                    exp_continue;
     }],

  [ qr/\-bash\-4.1\$/i,
            sub {   my $self = shift;
                    $self->send("ssh $REMOTE_MACHINE\n");
        $self->send("$COMMAND1\n");
        exp_continue;
  }]
 );
 $exp->soft_close();

如何通过 $self->send("$COMMAND1\n") 获取我在远程机器上执行的 $COMMAND1 的结果?

How can I get the result of the $COMMAND1 that I executed on the remote machine via $self->send("$COMMAND1\n") ?

推荐答案

我绝不是这方面的专家,但由于到目前为止没有其他人回答过,让我尝试一下.

I am by no means an expert on this but as noone else has answered so far, let me attempt it.

您的期望命令是 su,因此,正常的期望仅适用于该命令回复您原始 shell 的任何内容.然而,这只是密码提示,可能还有一些错误消息.您仍然可以发送命令,但它们的响应显示在新用户的 shell 上,而不是执行 expect 命令的 shell 上.这就是为什么它们显示在屏幕上,但不在您的 expect 对象可用的流中.请注意,如果您直接 ssh 到哪里,您可能会遇到同样的问题(我不确定为什么您会先 su 然后 ssh,您不能直接 ssh crazy-user@remote_machine 吗?).

Your expect command is the su and as such, normal expecting will only work on whatever that command answers back to your original shell. That however is only the password prompt and maybe some error messages. You can still send commands, but their responses show up on the shell of the new user, not the shell the expect command has been executed in. That is why they show up on screen but not in the stream available to your expect object. Note that you would likely encounter the very same problem if you where to ssh directly (i am not sure why you would su and then ssh anyways, could you not directly ssh crazy-user@remote_machine?).

解决方案可能是将 sussh 直接删除到您使用 Net::SSH::Expect 而不是普通的 Expect,因为它给出您在其输出流中写入远程控制台的所有内容.但是要小心,如果我没记错的话,检查流的语法略有不同.

The solution is probably to get rid of the su and ssh directly into the user you need on the remote machine employing Net::SSH::Expect instead of plain Expect as it gives you everything written to the remote console in its output stream. But be careful, if i remember correctly, the syntax for inspecting the stream is slightly different.

这篇关于通过 $self->send() 在 Perl Expect 模块中的远程主机上获取执行的命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆