Created
May 8, 2016 11:10
-
-
Save tetherit/c9b5376ba5d948b6db7c926a6c05f55b to your computer and use it in GitHub Desktop.
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.to_a[1..-2] | |
pings = ips.map do |ipaddr| | |
ip = ipaddr.to_s | |
pid = spawn("ping -q -W 2 -c 1 #{ip}", [:err, :out] => "/dev/null") | |
[pid, ip] | |
end | |
def arp_spawn(ip) | |
rout, wout = IO.pipe | |
pid = spawn("arp #{ip} | awk '{ print $4 }'", out: wout, err: "/dev/null") | |
wout.close | |
[pid, ip, rout] | |
end | |
arps = [] | |
pings.each do |pid, ip| | |
Process.wait(pid) | |
arps << arp_spawn(ip) if $?.exitstatus.zero? # online | |
end | |
arps.each do |pid, ip, rout| | |
Process.wait(pid) | |
p "#{ip} #{rout.read.strip}" # ip, mac | |
rout.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment