尝试从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
答案 0 :(得分: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