Module: Wol::Runner

Defined in:
lib/wol/runner.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) parse!(args)

Parse the give arguments



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wol/runner.rb', line 40

def self.parse!(args)
  args = ["-h"] if args.empty?

  opts = OptionParser.new do |opts|
    # Set a banner, displayed at the top
    # of the help screen.
    opts.banner = "Ruby Wake-On-LAN #{Wol::VERSION}\nUsage: wol -i ADDRESS ff:ff:ff:ff:ff:ff"

    opts.separator "\nSpecific options:"

    opts.on( '-q', '--quiet', 'No console output' ) do
      @options[:quiet] = true
    end

    opts.on( '-i', '--address 255.255.255.255', 'Set the destination address' ) do |address|
      @options[:address] = address
    end

    opts.on( '-p', '--port 9', Integer, 'Set the destination port' ) do |port|
      @options[:port] = port
    end

    opts.on( '-d', '--delay 0.01', Float, 'Delay between sending packets in seconds') do |delay|
      @options[:delay] = delay
    end

    opts.on( '-c', '--count 3', Integer, 'Number of packets to send. Default 3') do |count|
      @options[:count] = count
    end

    opts.on( '-f', '--file FILE', 'TODO: Uses a file as a source of addresses') do |file|
      @options[:file] = file
    end

    opts.separator "\nCommon options:"

    opts.on_tail( '-h', '--help', 'Display this message' ) do
      @options[:nothing_to_wake] = true
      puts opts
    end

    opts.on_tail( '-s', '--sample-file', 'Display a sample file') do
      @options[:nothing_to_wake] = true
      puts @sample_file
    end

    opts.on_tail( '-v', '--version', 'Show version') do
      @options[:nothing_to_wake] = true
      puts "Ruby Wake-On-LAN #{Wol::VERSION}"
    end
  end

  begin
    opts.parse!(args)

    if @options[:file].nil?
      args.each do |arg|
        @options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
      end

      @options[:macs].uniq!
    end

    return @options
  rescue OptionParser::InvalidOption => e
    STDERR.puts e.message, "\n", opts
    return -1
  end
end

+ (Object) run(argv)

Parse the command line options, then use them to wake up any given hosts.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wol/runner.rb', line 139

def self.run(argv)
  begin
    parse!(argv)

    return wake
  rescue SocketError => e
    puts e.message
    return -1
  rescue Exception => e
    STDERR.puts e.message
    return -1
  end
end

+ (Object) wake

Send WOL MagicPackets based on the parsed options



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wol/runner.rb', line 111

def self.wake
  if @options[:file]
    hosts = ParseFile.read_and_parse_file(@options[:file])

    hosts.each do |host|
      @options[:address], @options[:macs], @options[:port] = host[:address], host[:mac], host[:port]

      message = WakeOnLan.new(@options).wake.to_s
      puts message unless options[:quiet]

      return 0
    end
  elsif !@options[:macs].empty?
    @options[:macs].each do |mac|
      @options[:mac] = mac
      message = WakeOnLan.new(@options).wake.to_s
      puts message unless @options[:quiet]
    end

    return 0
  elsif @options[:macs].empty? && @options[:file].nil? && !@options[:nothing_to_wake]
    raise Exception, "You must supply a MAC address or a file"
  else
    return -1
  end
end