如何 ssh+expect 登录并 su 到远程机器上的另一个用户?

How to ssh+expect to login and su to another user on remote machine?

我来自本地机器,ssh 到远程机器,sudo su 到管理员用户。我不想退出 ssh,我希望留在远程机器上,所以我尝试了:

$cat 
#!/usr/bin/expect
spawn "/usr/bin/ssh myself@myremotebox -t \"/usr/bin/sudo su admin\""
expect "Password: " {send "mypassword\r"}


$./myssh.exp
spawn /usr/bin/ssh myself@myremotebox -t "/usr/bin/sudo su admin"
couldn't execute "/usr/bin/ssh myself@myremotebox -t "/usr/bin/sudo su admin"": no such file or directory
    while executing
"spawn "/usr/bin/ssh myself@myremotebox -t \"/usr/bin/sudo su admin\"""
    (file "./myssh.exp" line 2)

我好像遇到了语法错误。我试图 add/remove 里面的“\”和 outisde 我的 spawn 语句,都不起作用。如何解决?

谢谢!

应该是:

spawn /usr/bin/ssh -t myself@myremotebox "/usr/bin/sudo su admin"
expect "Password: " { send "mypassword\r" }
interact