Linux 在 bash 脚本中使用 expect 为 SSH 命令提供密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4780893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:12:24  来源:igfitidea点击:

Use expect in bash script to provide password to SSH command

linuxbashsshexpect

提问by Max

To those who want to reply that I should use SSH keys please abstain

对于那些想回复我应该使用 SSH 密钥的人,请弃权

I'm trying to use expect in an bash script to provide the SSH password. Providing the password works but I don't end up in the SSH session as I should, it goes back strait to bash.

我正在尝试在 bash 脚本中使用 expect 来提供 SSH 密码。提供密码有效,但我没有像我应该的那样在 SSH 会话中结束,它又回到了 bash 海峡。

My script:

我的脚本:

#!/bin/bash

read -s PWD

/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com'
expect "password"
send "$PWD\n" 
EOD
echo "you're out"

The output of my script:

我的脚本的输出:

spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
usr@$myhost.example.com's password: you're out

I would like to have my SSH session and only when I exit it to go back to my bash script. The reason why I am using bash before expect is because I have use a menu I can choose which unit to connect to.

我想要我的 SSH 会话,并且只有当我退出它才能返回到我的 bash 脚本时。我在期望之前使用 bash 的原因是因为我使用了一个菜单,我可以选择要连接到哪个单元。

Thanks

谢谢

采纳答案by Piotr Król

Mixing bash and expect is not a good way to achieve the desired effect. I'd try to use only Expect:

混合使用 bash 和 expect 并不是达到预期效果的好方法。我会尝试只使用预期:

#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
#use correct prompt
set prompt ":|#|\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact

Sample solution for bash could be:

bash 的示例解决方案可能是:

#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'

This will wait for enter and than return (for a moment) interactive session.

这将等待进入然后返回(片刻)交互式会话。

回答by user562374

Use the helper tool fd0ssh(from hxtools, not pmt), it works without having to expect a particular prompt from the ssh program.

使用辅助工具fd0ssh(来自 hxtools,而不是 pmt),它无需期待来自 ssh 程序的特定提示即可工作。

回答by dr-jan

Add the 'interact' expect command just before your EOD:

在 EOD 之前添加“interact”expect 命令:

#!/bin/bash

read -s PWD

/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
expect "password"
send "$PWD\n" 
interact
EOD
echo "you're out"

This should let you interact with the remote machine until you logout. Then you'll be back in bash.

这应该让您与远程机器进行交互,直到您注销。然后你会回到 bash 中。

回答by dotnix

The easiest way is to use sshpass. This is available in Ubuntu/Debian repos and you don't have to deal with integrating expect with bash.

最简单的方法是使用sshpass。这在 Ubuntu/Debian 存储库中可用,您无需处理将 expect 与 bash 集成的问题。

An example:

一个例子:

sshpass -p<password> ssh <arguments>
sshpass -ptest1324 ssh [email protected] ls -l /tmp

The above command can be easily integrated with a bash script.

上面的命令可以很容易地与 bash 脚本集成。

Note:Please read Security Considerationssection in man sshpassfor full understanding of security implications.

注意:请阅读安全注意事项部分man sshpass以充分了解安全隐患。

回答by Timmah

Also make sure to use

还要确保使用

send -- "$PWD\r" 

instead, as passwords starting with a dash (-) will fail otherwise.

相反,因为以破折号 (-) 开头的密码将失败。

The above wont interpret a string starting with a dash as an option to the send command.

以上不会将以破折号开头的字符串解释为发送命令的选项。

回答by Jimbo

Another way that I found useful to use a smallexpect script from a bash script is as follows.

我发现使用bash 脚本中的期望脚本有用的另一种方法如下。

...
bash-script start
bash-commands
...
expect - <<EOF 
spawn your-command-here
expect "some-pattern"
send "some-command"
...
...
EOF
...
more bash commands
...

This works because ...If the string "-" is supplied as a filename, standard input is read instead...

这有效,因为 ...If the string "-" is supplied as a filename, standard input is read instead...

回答by damn_c

After looking for an answer for the question for months, I finally find a really best solution: writing a simple script.

在为这个问题寻找了几个月的答案后,我终于找到了一个真正最好的解决方案:编写一个简单的脚本。

#!/usr/bin/expect

set timeout 20

set cmd [lrange $argv 1 end]
set password [lindex $argv 0]

eval spawn $cmd
expect "Password:"
send "$password\r";
interact

Put it to /usr/bin/exp, then you can use:

把它放到/usr/bin/exp,然后你可以使用:

  • exp <password> ssh <anything>
  • exp <password> scp <anysrc> <anydst>
  • exp <password> ssh <anything>
  • exp <password> scp <anysrc> <anydst>

Done!

完毕!

回答by Vijay S B

A simple expect script

一个简单的期望脚本

Remotelogin.exp

远程登录.exp

    #!/usr/bin/expect
    set user [lindex $argv 1]
    set ip [lindex $argv 0]
    set password [lindex $argv 2]
    spawn ssh $user@$ip
    expect "password"
    send "$password\r"
    interact

Example:

例子:

    ./Remotelogin.exp <ip> <user name> <password>

回答by user

sshpassis broken if you try to use it inside Sublime Text build target, inside a Makefile. Instead of sshpassyou can use passh: https://github.com/clarkwang/passh

sshpass如果您尝试在 Sublime Text 构建目标中,在 Makefile 中使用它,则会损坏。而不是sshpass你可以使用passhhttps: //github.com/clarkwang/passh

With sshpassyou would do:

sshpass你一起做:

sshpass -p pa$$word ssh user@host

With passhyou would do:

passh你一起做:

passh -p pa$$word ssh user@host

Note: Do not forget to use -o StrictHostKeyChecking=no, otherwise the connection will hang on the first time you use it. For example:

注意:不要忘记使用-o StrictHostKeyChecking=no,否则第一次使用时连接会挂掉。例如:

passh -p pa$$word ssh -o StrictHostKeyChecking=no user@host

References:

参考:

  1. Send command for password doesn't work using expect script in ssh connection
  2. https://askubuntu.com/questions/87449/how-to-disable-strict-host-key-checking-in-ssh
  3. https://linuxcommando.blogspot.com/2008/10/how-to-disable-ssh-host-key-checking.html
  4. https://serverfault.com/questions/330503/scp-without-known-hosts-check
  5. https://debian-administration.org/article/587/pam_mount_and_sshfs_with_password_authentication
  1. 在 ssh 连接中使用 expect 脚本发送密码命令不起作用
  2. https://askubuntu.com/questions/87449/how-to-disable-strict-host-key-checking-in-ssh
  3. https://linuxcommando.blogspot.com/2008/10/how-to-disable-ssh-host-key-checking.html
  4. https://serverfault.com/questions/330503/scp-without-known-hosts-check
  5. https://debian-administration.org/article/587/pam_mount_and_sshfs_with_password_authentication