【问题标题】:Expect script for ssh connection with password and additional operations期望 ssh 连接脚本与密码和其他操作
【发布时间】:2015-05-13 00:49:06
【问题描述】:

我发现以下脚本可以让我无需手动输入所需密码即可访问服务器。 可悲的是,我不知道建立连接后如何执行命令:(

#!/usr/bin/expect -f
spawn ssh user@server
expect "assword:"
send "pw123\r"
interact
#the following is not executed anymore
cd /tmp/

cd /tmp/ 命令没有执行,有人知道怎么做吗? 我不在乎安全:) 基于密钥的身份验证不是一种选择。

编辑: 好的,我找到了适合我需求的解决方案:

#!/usr/bin/expect -f
spawn user@server
expect "assword:"
send "pw123\r"
expect "> " { send "cd /tmp\r" }
interact

expect "> " 必须像您的提示一样。

【问题讨论】:

标签: shell ssh expect


【解决方案1】:

建立连接后,您将处于通过脚本连接到的远程主机的 shell 中。因此,要执行任何命令,您必须在命令提示符下手动执行命令。

如果您只想在远程服务器上自动执行命令而不需要 ssh,那么您可以使用以下命令。

#!/usr/bin/expect -f

#Changed here
spawn ssh user@server "cd /tmp && ls"
expect "password:"
send "pw123\r"
interact

【讨论】:

    猜你喜欢
    • 2015-12-20
    • 2013-08-25
    • 1970-01-01
    • 2015-11-18
    • 2019-05-14
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多