Skip to content

Instantly share code, notes, and snippets.

@tobinharris
Last active December 18, 2015 01:08
Show Gist options
  • Save tobinharris/5701294 to your computer and use it in GitHub Desktop.
Save tobinharris/5701294 to your computer and use it in GitHub Desktop.
class Device
attr_accessor :mac
attr_accessor :ip
attr_accessor :name
attr_accessor :is_online
attr_accessor :last_seen_at
attr_accessor :last_status_change_at
attr_accessor :status_changed
def initialize(options={})
@is_online = nil
@last_status_change_at = Time.new
options.each{|k,v| self.send("#{k}=",v)}
end
def is_online=(v)
#puts "Setting #{v}"
if @is_online != v then
@last_status_change_at = Time.new
@status_changed = true
else
@status_changed = false
end
@is_online = v
@last_seen_at = Time.new if @is_online == true
end
end
@monitored = []
@unmonitored = []
def test_network
arp = `arp -a`
#puts arp
lines = arp.split("\n")
lines.each do |l|
m = l.match(/\? \((\d+\.\d+\.\d+\.\d+)\) at ([^ ]+) /)
ip = m[1]
mac = m[2]
device = @monitored.select{|m|m.mac.upcase == mac.upcase}.first
other = @unmonitored.select{|m|m.mac.upcase == mac.upcase}.first
if not other
puts "Unknown device detected #{ip} #{mac} at #{Time.new}."
@unmonitored << Device.new(:name=>"Unknown", :mac=>mac, :ip=>ip)
end
next if device.nil?
puts "Testing #{device.name}"
res = `ping -i 1 -t 3 -c 2 #{ip}`
if res =~ /0 packets received/
device.is_online = false
else
device.is_online = true
end
if device.status_changed then
if device.is_online then
`say "#{device.name} is in the room"`
campfire "#{device.name} is in the room"
else
campfire "#{device.name} has buggered off"
`say "#{device.name} has buggered off"`
end
end
end
end
def campfire(words)
waffles = "509958"
`curl -X POST -H "Access-Control-Allow-Origin:*" -H "Content-Type: application/xml" -d "content: <message><body>#{words}</body></message>" "https://aee4dfcf64046f59c463918c834f6a8f92820321:X@pocketworks.campfirenow.com/room/#{waffles}/speak.xml"`
end
tobin = Device.new :mac=>'34:C0:59:EF:43:F1', :name=>"Tobin's iPhone"
adam = Device.new :mac=>'bc:3b:af:6b:23:dd', :name=>"Adam's iPhone"
@monitored << tobin
@monitored << adam
puts "Running..."
while true
test_network
sleep 2
end
#ping -i 5 -c 2 192.168.1.255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment