Class: Wol::WakeOnLan

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (WakeOnLan) initialize(options = {})

Create a new WakeOnLan instance. See the WakeOnLan class documentation for options.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wol/wakeonlan.rb', line 35

def initialize(options = {})
  @mac = options[:mac] ||= "ff:ff:ff:ff:ff:ff"
  @address = options[:address] ||= "255.255.255.255"
  @port = options[:port] ||= 9
  @count = options[:count] ||= 1
  @delay = options[:delay] ||= 0.01
  @quiet = options[:quiet]

  @socket=UDPSocket.open()

  @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
end

Instance Attribute Details

- (Object) address

Specify the destination address. Either a IP or hostname. Defaults to “255.255.255.255”



17
18
19
# File 'lib/wol/wakeonlan.rb', line 17

def address
  @address
end

- (Object) count

How many times to send the MagicPacket. Defaults to 1



23
24
25
# File 'lib/wol/wakeonlan.rb', line 23

def count
  @count
end

- (Object) delay

How many seconds to wait between sending packets. Defaults to 0.01



26
27
28
# File 'lib/wol/wakeonlan.rb', line 26

def delay
  @delay
end

- (Object) mac

Specify the destination MAC Address. Defaults to the broadcast MAC, “ff:ff:ff:ff:ff:ff”



14
15
16
# File 'lib/wol/wakeonlan.rb', line 14

def mac
  @mac
end

- (Object) port

The destination port. Defaults to the discard port, 9



20
21
22
# File 'lib/wol/wakeonlan.rb', line 20

def port
  @port
end

- (Object) quiet

What to return? Returns a string summary if false, else returns nil



29
30
31
# File 'lib/wol/wakeonlan.rb', line 29

def quiet
  @quiet
end

- (Object) socket (readonly)

The socket opened by WakeOnLan initialization



32
33
34
# File 'lib/wol/wakeonlan.rb', line 32

def socket
  @socket
end

Instance Method Details

- (Object) close

Close the socket opened by Wol initialization



49
50
51
52
# File 'lib/wol/wakeonlan.rb', line 49

def close
  @socket.close
  @socket=""
end

- (Object) wake

Wake host



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wol/wakeonlan.rb', line 55

def wake
  magicpacket = (0xff.chr)*6+(@mac.split(/:/).pack("H*H*H*H*H*H*"))*16

  @count.times do
    @socket.send(magicpacket, 0, @address, @port)
    sleep @delay if @delay > 0 unless @count == 1
  end

  if @quiet
    return nil
  else
    return @count == 1 ? "Sending magic packet to #{@address}:#{@port} with #{@mac}" : "Sending magic packet to #{@address}:#{@port} with #{@mac} #{@count} times"
  end
end