Last active
December 19, 2015 03:19
-
-
Save hnaohiro/5889716 to your computer and use it in GitHub Desktop.
LAN内のすべての端末のIPアドレスとMACアドレスを収集
This file contains hidden or 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
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