如何在expect脚本中从shell脚本获取返回值

时间:2016-05-20 06:36:14

标签: shell expect

在expect脚本中我正在执行ssh并调用一个返回值的bash脚本。如何在expect脚本中获取shell脚本的返回值? Wait将返回Spawn的状态,但我想要shell脚本的retrun值。提前谢谢。

    #!/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 $user@$ip sudo 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]"
    }
   }
   auto {*}$argv

0 个答案:

没有答案