9 August 2025

Ping machines in ruby

by Romain Dehasseleer


Ping in Ruby

Don’t remember the hostname or exact IP of your second PC? Too lazy to check your router?

Use net/ping with IPAddr, for example:

require 'ipaddr'
require 'net/ping'

network = IPAddr.new("192.168.0.0/24")
hosts = network.to_range.to_a[1..-2]

threads = []

hosts.each do |ip|
  threads << Thread.new do
    check = Net::Ping::External.new(ip.to_s)
    puts "#{ip} is up" if check.ping?
  end
end

threads.each(&:join)

Make sure ICMP (ping) isn’t blocked on the target machines.

tags: ruby