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