使用bash在ssh中使用Expect执行sudo [英] Execute sudo using expect inside ssh from bash
本文介绍了使用bash在ssh中使用Expect执行sudo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个脚本来自动在多个Linux主机上进行安装. 我使用ssh键登录到主机,并且在登录名内想执行sudo,我试图使用Expect,我在工作站上有,但我在服务器上没有剧本. 我该怎么做,这是我的尝试,但是没有运气:
I want to create a script that automates a installation on multiple linux hosts. I login to the hosts using ssh keys and inside the login I want to do a sudo, I am trying to use expect, which I have on the stations but I don't have on the server which runs the script. How do I do this, this is my try, but no luck with it:
#!/bin/bash
ssh user@station04 <<EOF
expect -d -c "
send \"sudo ls\"
expect {
\"password:\" { send '1234'; exp_continue }
\"$user@\"{
send "exit\r"
}
default {exit 1}
}"
EOF
exit
结果:
send: sending "sudo ls" to { exp0 }
expect: does "" (spawn_id exp0) match glob pattern "password:"? no
expect: read eof
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""
argv[0] = expect argv[1] = -d argv[2] = -c argv[3] =
send "sudo ls\r"
expect {
"password:" { send '1234'; exp_continue }
"@"{
send exitr
}
default {exit 1}
}
set argc 0
set argv0 "expect"
set argv ""
推荐答案
expect
的用法不太正确-不要send
命令;而不是spawn
命令,而send
只是其输入.因此,您的脚本将变为:
You got the usage of expect
not quite right - don't send
a command; rather spawn
the command and send
just its input. So, your script becomes:
ssh … <<EOF
expect -d -c "
spawn sudo ls
expect -nocase password: { send 1234\r }
expect eof
"
exit
EOF
这篇关于使用bash在ssh中使用Expect执行sudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文