Skip to content

Instantly share code, notes, and snippets.

@syxanash
Last active June 4, 2023 13:51
Show Gist options
  • Save syxanash/08d6b9bcd339f7df0b75cb3b9090c2bd to your computer and use it in GitHub Desktop.
Save syxanash/08d6b9bcd339f7df0b75cb3b9090c2bd to your computer and use it in GitHub Desktop.
who is home?
require 'rest-client'
require 'json'
=begin
Endpoint url must return something like this:
[
{
"name": "television-sony",
"mac": "00:11:22:33:44:55",
"display": false
},
...
{
"name": "macbook",
"mac": "AA:BB:CC:DD:EE:FF",
"display": true
}
=end
def stored_devices
mac_devices_stored = {}
begin
mac_devices_file_content = RestClient.get(
'https://myendpoint.withjson.hosts.com'
)
mac_devices_stored = JSON.parse(mac_devices_file_content)
rescue => e
end
mac_devices_stored
end
def active_devices
scan_cmd = `arp-scan --retry=8 --ignoredups -I en0 --localnet`
output_lines = scan_cmd.split("\n")
active_mac_list = []
address_lines = output_lines.slice(2, output_lines.size - 5)
address_lines.each do |line|
address_data = line.split("\t")
active_mac_list.push(address_data)
end
active_mac_list
end
def inclues_mac(address_list, mac_address)
address_list.each do |address|
if address == mac_address
return true
end
end
return false
end
puts 'downloading MAC list...'
devices_stored_list = stored_devices
puts 'scanning the network...'
devices_found = active_devices
devices_found.each do |device|
is_saved = false
should_display = true
devices_stored_list.each do |stored_device|
if device[1].upcase == stored_device['mac'].upcase
is_saved = true
should_display = stored_device['display']
if should_display
puts "#{device[0]}\t#{device[1]}\t#{stored_device['name']}"
end
end
end
unless is_saved
puts "#{device[0]}\t#{device[1]}\t#{device[2]}\t(not saved)"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment