在 bash 中使用 ssh 内的 Expect 执行 sudo

2024-04-23

我想创建一个脚本来自动在多个 Linux 主机上进行安装。 我使用 ssh 密钥登录到主机,在登录中我想做一个 sudo,我正在尝试使用 Expect,我在站上有它,但是我的服务器上没有运行脚本。 我该怎么做,这是我的尝试,但没有运气:

#!/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 ""

A.K

那这个呢?

#!/bin/bash

expect <<'END'
spawn ssh user@station04
expect "password:"
send "$pw\r"
expect "#"
send "sudo ls\r"

END
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 bash 中使用 ssh 内的 Expect 执行 sudo 的相关文章