Ruby Rails 系统调用Ping的使用

想用RoR下调用系统命令,一般exec和system 和syscall

想使用在Rails下,调用ping命令

最常见的是ruby支持的Ping用法如下:

 

Ping contains routines to test for the reachability of remote hosts. Currently the only routine implemented is pingecho(). 

Ping.pingecho uses a TCP echo (not an ICMP echo) to determine if the remote host is reachable. This is usually adequate to tell that a remote host is available to telnet, ftp, or ssh to. 

Warning: Ping.pingecho may block for a long time if DNS resolution is slow. Requiring ‘resolv-replace’ allows non-blocking name resolution. 

Usage: 

  require 'ping'

  puts "'jimmy' is alive and kicking" if Ping.pingecho('jimmy', 10)
Return true if we can open a connection to the hostname or IP address host on port service (which defaults to the "echo" port) waiting up to timeout seconds. 
  Ping.pingecho('192.168.1.27',timeout=50,service='echo')
Example: 

  require 'ping'

  Ping.pingecho "google.com", 10, 80

 

但ruby自带的ping,不但不能返回avg max min等参数,而且,ping的效果也和系统ping有很大的出入,所以,用以下两种:

   

system("ping 192.168.1.25 -c 2")

 

上面的返回值,还是true和flase

所以,用下面的

      @sum=`ping #{@ana[:ip]} -c #{@count}`

         @temp=@sum.slice(/(\.|\d|\/)+\//).split('/')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值