Module: Poweron

Defined in:
lib/poweron.rb,
lib/poweron/version.rb

Constant Summary collapse

VERSION =

Poweron version

'0.0.0'
VERSION_ARRAY =

:nodoc:

VERSION.split(/\./).map { |x| x.to_i }
VERSION_MAJOR =

:nodoc:

VERSION_ARRAY[0]
VERSION_MINOR =

:nodoc:

VERSION_ARRAY[1]
VERSION_BUILD =

:nodoc:

VERSION_ARRAY[2]

Class Method Summary collapse

Class Method Details

.poweron(interface, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/poweron.rb', line 10

def poweron(interface, opts = {})
  mac =
    if interface =~ /\A[0-9a-f]{2}(:[0-9a-f]{2}){5}\Z/i
      interface
    elsif File.readable?('/etc/ethers')
      File.open('/etc/ethers') do |ethers|
        ethers.find { |line| line =~ /^(\S+)\s+#{interface}/ and break $1 }
      end
    end
  mac or raise ArgumentError, "interface #{interface.inspect} could not be resolved"
  mac = mac.split(/:/).map { |x| x.to_i(16) }
  mac.size == 6 and mac.all? { |x| (0..0xff) === x } or
    raise ArgumentError, "mac address has to be 6 bytes long"
  port = opts[:port] || 9
  (0..0xffff) === port or raise  ArgumentError, "port in invalid range"
  ip = opts[:ip] || '255.255.255.255'
  ip = IPAddr === ip ? ip : IPAddr.new(ip)
  data = [ 0xff ] * 6 + mac * 16
  udp = UDPSocket.new
  udp.setsockopt Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1
  udp.connect ip.to_s, port
  udp.send data.pack('c*'), 0
end