Class: Wol::WakeOnLan
- Inherits:
-
Object
- Object
- Wol::WakeOnLan
- Defined in:
- lib/wol/wakeonlan.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Specify the destination address.
-
#count ⇒ Object
How many times to send the MagicPacket.
-
#delay ⇒ Object
How many seconds to wait between sending packets.
-
#mac ⇒ Object
Specify the destination MAC Address.
-
#port ⇒ Object
The destination port.
-
#quiet ⇒ Object
What to return? Returns a string summary if false, else returns nil.
-
#socket ⇒ Object
readonly
The socket opened by WakeOnLan initialization.
Instance Method Summary collapse
-
#close ⇒ Object
Close the socket opened by Wol initialization.
-
#initialize(options = {}) ⇒ WakeOnLan
constructor
Create a new WakeOnLan instance.
-
#wake ⇒ Object
Wake host.
Constructor Details
#initialize(options = {}) ⇒ WakeOnLan
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( = {}) @mac = [:mac] ||= "ff:ff:ff:ff:ff:ff" @address = [:address] ||= "255.255.255.255" @port = [:port] ||= 9 @count = [:count] ||= 1 @delay = [:delay] ||= 0.01 @quiet = [:quiet] @socket=UDPSocket.open() @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1) end |
Instance Attribute Details
#address ⇒ Object
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 |
#count ⇒ Object
How many times to send the MagicPacket. Defaults to 1
23 24 25 |
# File 'lib/wol/wakeonlan.rb', line 23 def count @count end |
#delay ⇒ Object
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 |
#mac ⇒ Object
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 |
#port ⇒ Object
The destination port. Defaults to the discard port, 9
20 21 22 |
# File 'lib/wol/wakeonlan.rb', line 20 def port @port end |
#quiet ⇒ Object
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 |
#socket ⇒ Object (readonly)
The socket opened by WakeOnLan initialization
32 33 34 |
# File 'lib/wol/wakeonlan.rb', line 32 def socket @socket end |
Instance Method Details
#close ⇒ Object
Close the socket opened by Wol initialization
49 50 51 52 |
# File 'lib/wol/wakeonlan.rb', line 49 def close @socket.close @socket="" end |
#wake ⇒ Object
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 |