How to ping for reachability of remote host in Ruby

1.if you has a firewall, remember to open icmp-type 8 first

iptables -A INPUT -i $EXTIF -p icmp --icmp-type 8 -j ACCEPT

2.use net-ping gem,of course, you should install it first.

gem install net-ping
require 'net/ping'

def up?(host)
    check = Net::Ping::External.new(host)
    check.ping?
end

host = 'google.com'
puts up?(host)

3.use system ping command

def up?(host, timeout=5)
  system "ping -c 1 -t #{timeout} #{host} >/dev/null"
end

p up?('localhost')

references:

https://stackoverflow.com/questions/21081639/how-to-ping-for-reachability-of-remote-host-in-ruby

O

分類: ruby,標籤: 。這篇內容的永久連結

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *