在Bash脚本中无法使用expect

时间:2013-12-13 10:14:30

标签: linux bash ssh expect

在我的bash脚本文件中,我尝试使用expectssh命令提供密码,但它不起作用。这是我的剧本:

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

执行脚本后的输出:

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

有人可以告诉我,如何在我的脚本中使用expect而不将#!/bin/bash更改为#!/usr/bin/expect吗?

2 个答案:

答案 0 :(得分:4)

以下在OS X终端中作为单行bash脚本工作。它仅适用于受防火墙保护的LAN。有关我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'

答案 1 :(得分:2)

ssh(以及任何其他密码阅读工具)不是从其标准输入中读取其密码。它在其终端设备上使用了一些棘手的ioctl() - s。这是因为您无法在管道中提供密码。

这不是一个大问题,因为广泛使用的明文密码造成了更多的伤害,好像有时我们需要找到一些替代的,无密码的解决方案。

在ssh的情况下,有一个非常简单的事情。谷歌的ssh-keygen。我建议使用它,配置一个无密码的ssh,一切都会好的。