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

时间:2017-03-31 13:03:32

标签: linux ssh login expect spawn

我是从本地机器,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)

似乎我有语法错误。我试图添加/删除" \"内部和外部我的spawn语句,都不起作用。如何解决?

谢谢!

1 个答案:

答案 0 :(得分:2)

应该是:

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