2016-05-13 6 views
0

リモートスクリプトを実行してスクリプトの戻り状況を確認していますが、次のようにしてパスワードのステータスを返しますが、呼び出されたスクリプトのステータスは返しません。呼び出されたスクリプトの戻り状況を取得します。事前に感謝してください。Expectでリモートスクリプトの結果を取得

#!/usr/bin/expect 
proc auto { } { 
global argv 
set timeout 120 
set ip XXXX.XXX.XX.XX 
set user name 
set password pass 
set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no} 
set script /path-to-script/test.sh 
spawn ssh {*}$ssh_opts [email protected]$ip bash $script {*}$argv 
expect "Password:" 
send "$password\r" 
send "echo $?\r" 
expect { 
    "0\r" { puts "Test passed."; } 
    timeout { puts "Test failed."; } 
} 
expect eof 
} 
auto {*}$argv 

答えて

1

あなたはssh bash remote_scriptを自動化しているので、あなたは、あなたがecho $?できるシェルプロンプトを取得するつもりはない - sshがあなたのスクリプトとして終了を起動しますが。

あなたがする必要があるのは、スポーンされたプロセスの終了ステータスを取得することです(sshはリモートコマンドの終了ステータスで終了することになっています)。あなたの終了ステータスはwaitです(他の情報もあります)

spawn ssh {*}$ssh_opts [email protected]$ip bash $script {*}$argv 
expect { 
    "Password:" { send "$password\r"; exp_continue } 
    timeout  { puts "Test failed." } 
    eof 
} 

# ssh command is now finished 
exp_close 
set info [wait] 
# [lindex $info 0] is the PID of the ssh process 
# [lindex $info 1] is the spawn id 
# [lindex $info 2] is the success/failure indicator 
if {[lindex $info 2] == 0} { 
    puts "exit status = [lindex $info 3]" 
} else { 
    puts "error code = [lindex $info 3]" 
} 
+0

ありがとうグレン・ジャックマン – marjun