pexpect - 通过 ssh 运行 script.sh [英] pexpect - run script.sh over ssh

查看:39
本文介绍了pexpect - 通过 ssh 运行 script.sh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 ssh 以编程方式运行本地脚本时遇到问题.
我不确定这是否与本地主机上的 shell 变量替换有关.

I'm having trouble programmatically running a local script over ssh.
I'm unsure if this is a problem with the shell variable substitution on the local host.

手动运行时,

ssh monit@server1 'bash -s' < /u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh

我得到了预期的输出,

CPU pctUser pctNice pctSystem pctIowait pctIdle
全部 11.21 0.00 1.50 0.31 86.98
0 0.00 0.00 0.00 0.00 100.00
1 3.00 0.00 1.00 0.00 96.00....

CPU pctUser pctNice pctSystem pctIowait pctIdle
all 11.21 0.00 1.50 0.31 86.98
0 0.00 0.00 0.00 0.00 100.00
1 3.00 0.00 1.00 0.00 96.00 ....

但我明白

bash:/u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh: 没有那个文件或目录

bash: /u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh: No such file or directory

运行以下代码时,

splunk_bin_dir = '/u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin'
hostname = 'server1'
username = 'monit'
password = 'monit#_'


command = "/usr/bin/ssh %(username)s@%(hostname)s 'bash -s' < %(splunk_bin_dir)s/cpu.sh" % locals()
print command

ssh_new_conn = 'Are you sure you want to continue connecting'

p = pexpect.spawn(command, timeout=360)
# Handles the 3 possible connection outcomes:
# a) Ssh to the remote host for the first time, triggering 'Are you sure you want to continue connecting'
# b) ask you for password
# c) No password is needed at all, because you already have the key.
i = p.expect([ssh_new_conn,'[pP]assword:',pexpect.EOF])
print ' Initial pexpect command output: ', i
if i == 0:
    # send 'yes'
    p.sendline('yes')
    i = p.expect(['[pP]assword:',pexpect.EOF])
    print 'sent yes. pexpect command output', i
    if i == 0:
        # send the password
        p.sendline(password)
        p.expect(pexpect.EOF)
elif i == 1:
    # send the password
    p.sendline(password)
    p.expect(pexpect.EOF)
elif i == 2:
    print "pexpect faced key or connection timeout"
    pass

print p.before

这些是打印输出,

/usr/bin/ssh monit@server1 'bash -s' </u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh
初始 pexpect 命令输出:1
bash:/u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh: 没有那个文件或目录

/usr/bin/ssh monit@server1 'bash -s' < /u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh
Initial pexpect command output: 1
bash: /u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh: No such file or directory

pexpect 碰到 [pP]assword 行,所以我猜密码被正确传递,

pexpect is bumping into the [pP]assword line so I guess the password is being correctly passed,

推荐答案

这里是 pexpect 手册的注释:

Here the note from pexpect manual:

请记住,Pexpect 不会解释 shell 元字符,例如重定向、管道或通配符(>、| 或 *).这是一个常见的错误.如果你想运行一个命令并通过另一个命令管道它然后您还必须启动一个 shell.

Remember that Pexpect does NOT interpret shell meta characters such as redirect, pipe, or wild cards (>, |, or *). This is a common mistake. If you want to run a command and pipe it through another command then you must also start a shell.

这是工作线

command = """/bin/bash -c "/usr/bin/ssh  %(username)s@%(hostname)s 'bash -s' < %(splunk_bin_dir)s/cpu.sh" """ % locals()

这篇关于pexpect - 通过 ssh 运行 script.sh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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