Skip to content

Instantly share code, notes, and snippets.

@seventhsense
Forked from hnaohiro/ip_mac.rb
Created December 28, 2013 04:23
Show Gist options
  • Save seventhsense/8156098 to your computer and use it in GitHub Desktop.
Save seventhsense/8156098 to your computer and use it in GitHub Desktop.
def ping(ip)
r = Regexp.new('[1-9]\d* (packets )?received')
result = r.match(`ping -c 1 -W 1 #{ip}`)
result != nil
end
def arp(ip)
r = Regexp.new('[0-f]+:[0-f]+:[0-f]+:[0-f]+:[0-f]+:[0-f]+')
result = r.match(`arp -n #{ip}`)
result ? result.to_s : nil
end
def get_mac_addresses
mac_adresses = Array.new
network_address = '192.168.1'
2.upto(254) do |n|
ip = '%s.%d' % [network_address, n]
if ping(ip)
mac_adresse << arp(ip)
end
end
mac_adresses
end
p get_mac_addresses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment