我需要一个脚本来配置PDU (配电装置)。该脚本将从另一个程序中调用。我创建了获取参数的Expect脚本,通过ssh连接到PDU并运行命令。诸如“pwd”或“lang”之类的短命令可以正常工作,但长命令会等待用户交互。如果我什么也不做,连接将在超时后关闭,如果我按Enter,脚本将继续并通过。
这里有一个脚本:
spawn ssh $user@$pdu
interact -o -nobuffer "assword:" return
send "$password\r"
interact -o -nobuffer "apc>" return
send "olStatus $host\r"
interact -o -nobuffer "apc>" return使用debug标志运行它会得到以下输出
apc>tty_set: raw = 0, echo = 1
send: sending "olStatus machine-123456\r" to { exp4 }
defining key apc>, action return
tty_raw_noecho: was raw = 0 echo = 1
spawn id exp4 sent <olStatus machine-123>
olStatus machine-123spawn id exp0 sent <\r>
spawn id exp4 sent <456\r\n23: machine-123456: On \r\nE000: Success\r\n\r\napc>\r\napc>>
456
23: machine-123456: On
E000: Success我在这里添加了评论

我该如何解决这个问题?
发布于 2021-03-14 22:26:05
这是我的建议:
spawn ssh $user@$pdu
expect "assword:"
send "$password\r"
expect -re {apc>$}
send "olStatus $host\r"
expect -re {apc>$}
send "exit\r" # or "quit" or whatever
expect eof但是在启用debug的情况下运行它,并让我知道你得到了什么。
发布于 2021-03-15 22:17:41
多亏了@GlennJackman,我运行了autoexpect,这有助于我理解如何解决这个问题。也许这是我的系统问题,但目前无关紧要。
需要做的是在两个步骤中发送
send "olStatus "
expect "olStatus"
send "machine-123456\r"
expect -re {apc>$}https://stackoverflow.com/questions/66580041
复制相似问题