Class: Wakeup::Waker

Inherits:
Object
  • Object
show all
Defined in:
lib/wakeup.rb

Class Method Summary collapse

Class Method Details

.wake(hostname, mac_addr) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/wakeup.rb', line 16

def self.wake(hostname, mac_addr)
  udp_sock = UDPSocket.new
  udp_sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
  address = mac_addr.split(":").map(&:hex).map(&:chr).inject(:+).force_encoding("UTF-8")
  data = "\xFF\xFF\xFF\xFF\xFF\xFF" + (address * 16) # Purposefully using double quotes
  udp_sock.send(data, 0, "<broadcast>", Wakeup::PORT) # Need to send this before in order for it to work
  udp_sock.send(data, 0, hostname, Wakeup::PORT)
  puts "Wakeup command sent to host #{hostname}"
end