Created
February 29, 2012 20:00
-
-
Save selman/1944010 to your computer and use it in GitHub Desktop.
network pinging over child processes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THIS IS AWESOME