混合bash和Expect不是达到预期效果的好方法。我会尝试仅使用Expect:
#!/usr/bin/expecteval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com#use correct promptset prompt ":|#|\$"interact -o -nobuffer -re $prompt returnsend "my_passwordr"interact -o -nobuffer -re $prompt returnsend "my_command1r"interact -o -nobuffer -re $prompt returnsend "my_command2r"interact
bash的示例解决方案可能是:
#!/bin/bash/usr/bin/expect -c 'expect "n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'这将等待进入并返回(暂时)交互式会话。



