Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tetherit/c9b5376ba5d948b6db7c926a6c05f55b to your computer and use it in GitHub Desktop.
Save tetherit/c9b5376ba5d948b6db7c926a6c05f55b to your computer and use it in GitHub Desktop.
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