我想创建一个脚本来自动在多个Linux主机上进行安装.
我使用ssh密钥登录到主机,并且在登录名内想执行sudo,我尝试使用Expect,我在工作站上有,但在运行脚本的服务器上没有.
我该怎么做,这是我的尝试,但是没有运气:
#!/bin/bash
ssh user@station04 <
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 ""
解决方法:
A.K
那这个呢?
#!/bin/bash
expect <
spawn ssh user@station04
expect "password:"
send "$pw\r"
expect "#"
send "sudo ls\r"
END
标签:expect,bash,linux,ssh
来源: https://codeday.me/bug/20191012/1902769.html