プログラムで ssh 経由でローカル スクリプトを実行するのに問題があります。
これがローカル ホストでのシェル変数の置換に問題があるかどうかはわかりません。
手動で実行する場合、
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 ....
しかし、私は得る
bash: /u02/splunk/splunk/etc/apps/Splunk_TA_nix/bin/cpu.sh: そのようなファイルまたはディレクトリはありません
次のコードを実行すると、
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: そのようなファイルまたはディレクトリはありません
pexpect が [pP]assword 行にぶつかっているので、パスワードが正しく渡されていると思います。