Skip to content

Instantly share code, notes, and snippets.

@tyrone-sudeium
Created January 26, 2013 11:13
Show Gist options
  • Select an option

  • Save tyrone-sudeium/4641782 to your computer and use it in GitHub Desktop.

Select an option

Save tyrone-sudeium/4641782 to your computer and use it in GitHub Desktop.
Something I wrote a lifetime ago that'll send a Wake On LAN magic packet to a specified computer when a specified device joins the WiFi zone.
require 'rubygems'
require 'snmp'
require 'socket'
iPhones = [ "00:23:32:f4:c4:a1" ]
pcs = [ "00:1B:21:29:70:1D" ]
wakeUp = Hash::new
woken = Hash::new
seen = Hash::new
iPhones.each do |s|
wakeUp[s] = pcs[iPhones.index(s)]
woken[s] = false;
seen[s] = false;
end
wakeUp.each do |k,v|
puts "When I see \"#{k}\" I'll wake up \"#{v}\"."
end
def wake(mac)
wol_magic=(0xff.chr)*6+(mac.split(/:/).pack("H*H*H*H*H*H*"))*16
socket = UDPSocket.open()
socket.setsockopt(Socket::SOL_SOCKET,Socket::SO_BROADCAST,1)
10.times { socket.send(wol_magic, 0, "192.168.1.255", "discard") }
socket.close()
end
ipTable_columns = ["ipNetToMediaPhysAddress"]
while true do
SNMP::Manager.open(:Host => '192.168.1.254', :Community => "secret") do |manager|
manager.walk(ipTable_columns) do |row|
row.each do |vb|
mac = vb.value.unpack("H2H2H2H2H2H2").join(":")
#puts mac
iPhones.each do |s|
if (s == mac)
if woken[s]
seen[s] = true
next
end
puts "#{mac} found! Waking #{wakeUp[mac]}"
wake(pcs[iPhones.index(s)])
woken[s] = true
seen[s] = true
break
end
end
end
end
end
#puts "---"
iPhones.each do |s|
if seen[s] != true
woken[s] = false
end
seen[s] = false
end
sleep(5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment