【问题标题】:Use Pexpect or expect for a ssh task, and how to capture errors对 ssh 任务使用 Pexpect 或 expect,以及如何捕获错误
【发布时间】:2013-04-18 22:23:21
【问题描述】:

我想使用一个期望或 Pexpect 文件来自动化一个 shell 命令 喜欢:

 for host in HostList:
      ssh -t kitty@host 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"'

如果我手动输入命令,有3种可能 对于情况2,我必须输入yes,对于其他情况我不需要输入任何内容 不需要密码。

1)

   [kitty@goeland kitty]$ ssh -t kitty@202.171.61.205 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"' 
    nohup: ignoring input and appending output to `nohup.out'
    Connection to 202.171.61.205 closed.

2)

    [kitty@goeland kitty]$ ssh -t kitty@202.171.61.205 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"' 
    The authenticity of host '202.171.61.205 (202.171.61.205)' can't be established.
    RSA key fingerprint is ff:0b:9c:a9:72:52:8f:53:0d:04:d5:ea:d9:3c:56:37.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '202.171.61.205' (RSA) to the list of known hosts.
    nohup: ignoring input and appending output to `nohup.out'
    Connection to 202.171.61.205 closed.

3) 不同的错误,目前我还没有得到

对于3,我想知道错误

那么如何使用expectPexpect 来做到这一点? 最好Pexpect 谢谢!

【问题讨论】:

    标签: linux expect pexpect


    【解决方案1】:

    你已经使用了 pxssh 模块,这是 这个类扩展了 pexpect.spawn 来专门设置 SSH 连接。 这会添加登录、注销和等待 shell 提示的方法。该模块会在您的 2 问题“是”

    中自动处理

    示例代码:- Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko

    并缓存您的错误登录:- 示例代码就像

    host = pxssh.pxssh()
    host.login (hostname, username, password)
    host.logfile = open(/tmp/logfile.txt, "w")
    host.sendline("ls -l")
    

    打开 logfile.txt 你的终端输出存储在文本文件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多