ruby调用telnet,远程运行命令

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

require 'net/telnet'
 
# 连接到远程主机 foobar
telnet = Net::Telnet.new("Host" => "foobar") {|c| print c}
 
# 登陆
telnet.login("your name", "your password") {|c| print c}
# 登陆后等待提示
 
telnet.cmd("ls") {|c| print c}
# 执行命令后等待提示
 
# 稍复杂的例子
telnet.cmd("sleep 5 && echo foobar &") {|c| print c}
 
STDOUT.flush # <- 若没有这句的话,是很难看出程序已经运行到这里的
 
# 等待前面命令的输出
telnet.waitfor(/foobar\Z/) {|c| print c}
 
# 结束登陆会话
telnet.cmd("exit") {|c| print c}
telnet.close


#http://www.kuqin.com/rubycndocument/man/addlib/Net-Telnet.html