`

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('/')

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics