Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ping for reachability of remote host in Ruby

Tags:

ruby

I tried using

Ping.pingecho("10.102.52.42", 30)

for reachability of the remote host. This statement returns null even if I'm able to ping the IP manually. Is there an efficient way to determine the reachability of the remote machine in Ruby?

like image 934
Bindu Avatar asked Jan 12 '14 23:01

Bindu


People also ask

Can be used to test the reachability of a host on an IP network?

Ping is a computer network administration utility used to test the reachability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer.


1 Answers

I use the net-ping gem which you need to install. Then the code is easy:

#!/usr/bin/env ruby

require 'net/ping'

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

chost = '10.0.0.1'
puts up?(chost) # prints "true" if ping replies
like image 137
patm Avatar answered Sep 19 '22 08:09

patm