Skip to content

Instantly share code, notes, and snippets.

@selman
Created February 29, 2012 20:00
Show Gist options
  • Save selman/1944010 to your computer and use it in GitHub Desktop.
Save selman/1944010 to your computer and use it in GitHub Desktop.
network pinging over child processes
time ruby pinger.rb
192.168.1.4
192.168.1.59
192.168.1.90
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.105
192.168.1.107
192.168.1.136
192.168.1.145
real 0m1.174s
user 0m0.027s
sys 0m0.077s
require 'ipaddr'
ips = IPAddr.new("192.168.1.0/24").to_range
children = ips.map do |ipaddr|
ip = ipaddr.to_s
pid = spawn("ping -q -W 1 -c 1 #{ip}",
[:err, :out] => "/dev/null")
[pid, ip]
end
children.each do |pid, ip|
# end child process & update $? (process exit status)
Process.wait(pid)
# 0 pinged / 1 no reply / 2 errors
puts ip if $?.exitstatus.zero?
end
@hfossli
Copy link

hfossli commented Sep 16, 2015

THIS IS AWESOME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment