开发者

expect script freezes when called from Ruby

I've a list of hosts on which I need to install my public key. For this purpose, I've written a ruby script that calls an expect script and passes to it my password, the host name and public key file. This expect script in turn performs ssh-copy-id to each host, by feeding in the password and answering "yes" for unknown host keys.

The expect script works absolutely fine when run for command line. But when executed from a ruby script, expect fails to answer "yes" for the unknown host key confirmation : "Are you sure you want to continue connecting (yes/no)?". The expect script just freezes when the yes/no question is thrown to it.

Any help will be greatly appreciated.

Here is my ruby script :

#!/usr/bin/env ruby -w

hosts=['test@blah1.edu','test2@blah2.edu'开发者_如何转开发,'test3@blah3.edu']
password="blahblahblah"
key_file="/home/blah/.ssh/id_rsa.pub"

hosts.each{ |host|
   command="expect sshcopy.exp #{host} #{key_file} #{password}"
  `#{command}`
}

And here is my expect script sshcopy.exp :

set host [lrange $argv 0 0]
set key_file [lrange $argv 1 1]
set password [lrange $argv 2 2]
spawn  ssh-copy-id -i $key_file $host
expect -nocase "*password: $" {send "$password\r"; interact}  -nocase "*are you sure you want to continue connecting (yes/no)? $" {send "yes\r"}  eof{exit}
expect -nocase "*password: $" {send "$password\r"; interact} eof{exit}

You will see two expect statements above. The first statement handles the case when the password is asked immediately (i.e. host key is known) by interacting immediately. It also handles the case when an unknown host is identified, by answering "yes".

The second expect statement is executed when the first expect answered "yes" leading to a password being asked.


I imagine the problem is that you call the expect script with `backticks` however the expect script interacts. It seems the ruby backticks don't allow full interaction (blocking stdin perhaps). You might want to investigate ruby's expect module and do away with the separate expect script.


Thanks guys. But I found that the answer to the problem is very simple :

  puts `#{command}`

I assumed that `` in ruby would merge the stdout and stderr handles of the shell command with the ruby script. This is wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜