2012-04-18 23 views
4

sshでローカルスクリプトをプログラムで実行する際に問題があります。
これは、ローカルホスト上のシェル変数置換に問題があるかどうかはわかりません。pexpect - sshを介してscript.shを実行

手動で実行し、

ssh [email protected] '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)[email protected]%(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 /アプリケーション/ Splunk_TA_nix/binに/ cpu.sh:そのようなファイルやディレクトリはありません

私はパスワードが正しく渡されていると思いますので、pexpectが

答えて

3
、[にpP] asswordラインに衝突されます

ここにpexpectマニュアルの注記:

Pexpectは、 リダイレクト、パイプ、またはワイルドカード(>、|、または*)などのシェルメタ文字を解釈しないことに注意してください。これはよくある間違いです。 コマンドを実行して別のコマンドをパイプしたい場合は、 シェルを起動する必要があります。

これは、現用回線

command = """/bin/bash -c "/usr/bin/ssh %(username)[email protected]%(hostname)s 'bash -s' < %(splunk_bin_dir)s/cpu.sh" """ % locals() 
+0

ですありがとうございました! このPexpectの機能について実際には知りませんでした –

関連する問題