Skip to content

Instantly share code, notes, and snippets.

@szimek
Last active January 3, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szimek/8472201 to your computer and use it in GitHub Desktop.
Save szimek/8472201 to your computer and use it in GitHub Desktop.
Find who's online in the local network
#!/usr/bin/env ruby
require "redis"
require "sinatra"
require "sinatra/json"
redis = Redis.new
get "/" do
macs_online = redis.smembers("mac-online")
names_online = if macs_online.empty?
[]
else
redis.hmget("mapping", macs_online).compact.uniq
end
names_all = redis.hvals("mapping")
result = names_all.reduce({}) {|acc, name| acc[name] = names_online.include?(name) ? "online" : "offline"; acc}
json result
end
post "/devices-online" do
data = request.body.read
mac_addresses = data.split("\n")
redis.del("mac-online")
redis.sadd("mac-online", mac_addresses)
status 200
end
post "/devices-mapping" do
data = request.body.read
mac_mapping = data.split("\n").map{|e| e.split(",")}.flatten
redis.del("mapping")
redis.hmset("mapping", *mac_mapping)
redis.del("mac-all")
redis.sadd("mac-all", Hash[*mac_mapping].keys)
status 200
end
#!/usr/bin/env bash
echo "Scanning..."
MACS_ONLINE=`sudo nmap -sn 192.168.1.0/24 | grep -Eio "([0-9A-F]{2}:){5}[0-9A-F]{2}"`
MACS_MAPPING=$(<arp-mapping.csv)
echo "Done."
echo "Uploading data..."
curl http://localhost:4567/devices-mapping -d "$MACS_MAPPING" -XPOST --header 'Content-Type: text/csv'
curl http://localhost:4567/devices-online -d "$MACS_ONLINE" -XPOST --header 'Content-Type: text/csv'
echo "Done."
source "https://rubygems.org"
ruby "2.1.0"
gem "sinatra"
gem "sinatra-contrib"
gem "redis"
C0:3F:0E:17:1B:E4 Computer
3C:07:71:3A:A3:84 Phone
3C:07:71:3A:A3:85 TV
C0:3F:0E:17:1B:E5 Computer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment