返回顶部

收藏

ruby调用telnet,远程运行命令

更多
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

标签:ruby

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2018-06-20 00:00:00 Support of Ruby 2.2 has ended
  2. 博主 发表 2018-06-19 00:26:00 ruby/rails - 根据浏览器的语言,来自动识别
  3. Alex Wood 发表 2014-10-15 13:06:29 Deploying Ruby on Rails Applications to AWS OpsWor
  4. Alex Wood 发表 2014-10-22 12:04:19 Caching the Rails Asset Pipeline with Amazon Cloud
  5. Trevor Rowe 发表 2014-11-18 19:04:17 Using Resources
  6. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  7. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  8. Trevor Rowe 发表 2014-11-26 19:04:20 Client Response Stubs
  9. Alex Wood 发表 2014-12-02 17:32:13 AWS re:Invent 2014 Ruby Recap
  10. Alex Wood 发表 2014-12-10 17:20:18 Using Amazon RDS with Ruby on Rails and AWS OpsWor
  11. Trevor Rowe 发表 2015-01-28 17:48:41 Upcoming Stable Release of AWS SDK for Ruby – Vers
  12. Trevor Rowe 发表 2015-02-05 23:46:12 Announcing V2 of the AWS SDK for Ruby

发表评论