【问题标题】:Capture return code from Expect从 Expect 捕获返回码
【发布时间】:2018-04-19 23:03:25
【问题描述】:

试图从linux机器执行expect脚本来捕获放置在windows机器中的powershell脚本的返回码。

run.sh

expect ssh.exp $USER $HOST $PASSWD "PowerShell -NonInteractive -NoProfile -Command test.ps1"

ssh.exp

#!/usr/bin/expect

set user [lindex $argv 0];
set host [lindex $argv 1];
set pass [lindex $argv 2];
set cmd [lindex $argv 3];


spawn ssh -C -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$host

expect "password:"
send "$pass\r"

expect -re ".*?>"
send "$cmd\r"

expect -re ".*?>"
send "exit\r"

interact

test.ps1

function test {
    $i = 1
    if ($i -eq 2) {
        Write-Host "success"
    }
    else {
        Write-Host "failed"
        $host.SetShouldExit(5)
        exit
    }
}
Write-Host "Exiting with code 5"
test

预期返回码:5

【问题讨论】:

  • 不要像这样扔代码,你应该解释你的3个代码来改进可能的答案

标签: linux shell powershell ssh expect


【解决方案1】:

需要查看退出状态:

send "$cmd\r"
expect -re ".*?>"

send "echo \$lastexitcode\r"
expect -re {\y(\d+)\y.*>}
set exit_status $expect_out(1,string)

send "exit\r"
expect eof

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2017-05-07
    • 2014-01-23
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多