简体   繁体   English

无法在Bash脚本中使用Expect

[英]Cannot use expect in Bash script

In my bash script file, I try to use expect to provide password for ssh command but it doesn't work. 在我的bash脚本文件中,我尝试使用expectssh命令提供密码,但是它不起作用。 Here is my script: 这是我的脚本:

#!/bin/bash

/usr/bin/expect << EOD
spawn ssh root@192.168.1.201
expect "root@192.168.1.201's password:"
send "mypassword\r"
interact
expect eof
EOD

And the output after I execute the script: 执行脚本后的输出:

[oracle@BTMVNSRV191 Desktop]$ ./login.sh
spawn ssh root@192.168.1.201
root@192.168.1.201's password: [oracle@BTMVNSRV191 Desktop]$ 

Could someone let me know, how to use expect in my script without changing #!/bin/bash to #!/usr/bin/expect ? 有人可以让我知道,如何在我的脚本中使用expect而不将#!/bin/bash更改为#!/usr/bin/expect吗?

The following works as a single line of bash script in OS X Terminal. 以下内容在OS X Terminal中作为bash脚本的一行。 It was only intended for use on a firewall protected LAN. 它仅适用于受防火墙保护的LAN。 Further details at my original post . 更多详细信息,请参见我的原始帖子

expect -c 'spawn ssh -o StrictHostKeyChecking=no remote-user@remote-IP; 
expect assword; send remote-password\r; expect remote-user$; 
send "sudo shutdown -h +1\r"; expect assword; send remote-password\r; interact'

ssh (and any other password reading tool) reads its password not from its standard input. ssh(和任何其他密码读取工具)不从其标准输入读取其密码。 It uses some tricky ioctl()-s on its terminal device. 它在其终端设备上使用一些棘手的ioctl()-s。 This is because you can't give them your password in a pipe. 这是因为您无法通过管道向他们提供密码。

It is not really a big problem, because widely used cleartext passwords caused more harm as if sometimes we need to find some alternative, password-less solution. 这并不是一个大问题,因为广泛使用的明文密码造成的危害更大,好像有时我们需要找到一些替代的无密码解决方案。

In cases of the ssh, there is a very simple thing for that. 对于ssh,有一个非常简单的方法。 Google for ssh-keygen . Google的ssh-keygen I suggest to use that, configure a passwordless ssh and everything will be fine. 我建议使用它,配置无密码的ssh,一切都会好起来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM