gpt4 book ai didi

linux - 从 Expect 捕获返回码

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:18 24 4
gpt4 key购买 nike

尝试从 linux 机器上执行 expect 脚本以捕获放置在 windows 机器上的 powershell 脚本的返回代码。

运行.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

最佳答案

需要查看退出状态:

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

关于linux - 从 Expect 捕获返回码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com