Ruby-Expect和Pty的问题 [英] Ruby - Problems with Expect and Pty
本文介绍了Ruby-Expect和Pty的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个Ruby脚本,它将ssh切换到服务器,运行给定命令,并从中获取输出.这是我到目前为止的内容,主要是根据编程Ruby 本书改编而成:
I'm trying to write a Ruby script that will ssh over to a server, run a given command, and fetch the output from it. Here's what I've got so far, mostly adapted from the Programming Ruby book:
require 'pty'
require 'expect'
$expect_verbose = true
PTY.spawn("ssh root@x.y") do |reader, writer, pid|
reader.expect(/root@x.y's password:.*/)
writer.puts("password")
reader.expect(/.*/)
writer.puts("ls -l")
reader.expect(/.*/)
answer = reader.gets
puts "Answer = #{answer}"
end
不幸的是,我回来的只是这个:
Unfortunately all I'm getting back is this:
Answer = .y's password:
知道我做错了什么以及如何减轻这种情况吗?
Any idea what I've done wrong and how to alleviate this?
推荐答案
为此,我建议使用net-ssh gem:sudo gem install net-ssh
:
For this I recommend using the net-ssh gem: sudo gem install net-ssh
: http://net-ssh.rubyforge.org/ssh/v2/api/index.html
代码有点像这样:
require 'rubygems'
require 'net/ssh'
Net::SSH.start('your-server', 'username', :password => "password") do |ssh|
puts ssh.exec!("ls -la")
end
这篇关于Ruby-Expect和Pty的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文