Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Fixed some bugs, small design changes, CLI options now 100% compatibl…
Browse files Browse the repository at this point in the history
…e with the original perl version.
  • Loading branch information
matiaskorhonen committed Nov 12, 2009
1 parent 893b31d commit 3d6e76a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -10,7 +10,7 @@ begin
gem.email = "korhonen.matt@gmail.com"
gem.homepage = "http://github.com/k33l0r/wol"
gem.authors = ["Matias Korhonen"]
gem.rubyforge_project = 'wol'
gem.rubyforge_project = 'wol'
gem.add_development_dependency "rspec", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Expand Down
44 changes: 18 additions & 26 deletions lib/wol/runner.rb
Expand Up @@ -28,7 +28,7 @@ def self.parse(args)
options[:quiet] = false
options[:address] = "255.255.255.255"
options[:port] = 9
options[:interval] = 0.01
options[:delay] = 0.01
options[:count] = 3
options[:macs] = Array.new
options[:file] = nil
Expand All @@ -45,16 +45,16 @@ def self.parse(args)
options[:quiet] = true
end

opts.on( '-a', '--address 255.255.255.255', 'Set the destination address' ) do |address|
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( '-i', '--interval 0.01', Float, 'Interval between sending packets in seconds') do |interval|
options[:interval] = interval
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|
Expand Down Expand Up @@ -103,19 +103,26 @@ def self.parse(args)
end

def self.version
"Ruby Wake-On-LAN version 0.2.2"
"Ruby Wake-On-LAN version 0.3.0"
end

def self.wake(options = {})
begin
if options[:file].nil?
if !options[:macs].empty?
puts WakeOnLan.new(options).wake unless options[:quiet]
else
puts "You have to specify a file or MAC address"
if options[:file]
hosts = ParseFile.read_and_parse_file(options[:file])

for host in hosts
options[:address], options[:macs], options[:port] = host[:address], host[:mac], host[:port]

puts WakeOnLan.new(options).wake
end
elsif options[:macs]
options[:macs].each do |mac|
options[:mac] = mac
puts WakeOnLan.new(options).wake
end
else
puts wake_from_file(options) unless options[:quiet]
puts "You have to specify a file or MAC address"
end
rescue Exception => e
puts "An error occured. Please check your inputs."
Expand All @@ -125,21 +132,6 @@ def self.wake(options = {})
end
end

def self.wake_from_file(options = {})
message = ""
hosts = ParseFile.read_and_parse_file(options[:file])

for host in hosts
options[:address], options[:macs], options[:port] = host[:address], host[:mac], host[:port]

unless options[:quiet]
message << WakeOnLan.new(options).wake
end
end

return options[:quiet] ? nil : message
end

def self.run!
options = parse(ARGV)

Expand Down
34 changes: 10 additions & 24 deletions lib/wol/wakeonlan.rb
Expand Up @@ -13,7 +13,7 @@

module Wol
class WakeOnLan
attr_accessor :macs, :address, :port, :count, :interval, :quiet
attr_accessor :mac, :address, :port, :count, :delay, :quiet
attr_reader :socket

# Create a new instance
Expand All @@ -22,14 +22,14 @@ class WakeOnLan
# * <tt>:address => "255.255.255.255"</tt> - Specify the destination address. Either a IP or hostname. Defaults to "255.255.255.255"
# * <tt>:port => 9</tt> - The destination port. Defaults to the discard port, 9
# * <tt>:count => 1</tt> - How many times to send the MagicPacket. Defaults to 1
# * <tt>:interval => 0.01</tt> - How many seconds to wait between sending packets. Defaults to 0.01
# * <tt>:delay => 0.01</tt> - How many seconds to wait between sending packets. Defaults to 0.01
# * <tt>:quiet => false</tt> - What to return? Returns a string summary if false, else returns nil
def initialize(options = {})
@macs = options[:macs] ||= ["ff:ff:ff:ff:ff:ff"]
@mac = options[:mac] ||= "ff:ff:ff:ff:ff:ff"
@address = options[:address] ||= "255.255.255.255"
@port = options[:port] ||= 9
@count = options[:count] ||= 1
@interval = options[:interval] ||= 0.01
@delay = options[:delay] ||= 0.01
@quiet = options[:quiet]

@socket=UDPSocket.open()
Expand All @@ -43,34 +43,20 @@ def close
@socket=""
end

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

@macs.each do |mac|
messages << send_packet(mac) + "\n"
@count.times do
@socket.send(magicpacket, 0, @address, @port)
sleep @delay if @delay > 0 unless @count == 1
end

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

private

# Send the actual packet
def send_packet(mac)
magicpacket = (0xff.chr)*6+(mac.split(/:/).pack("H*H*H*H*H*H*"))*16

@count.times do
@socket.send(magicpacket, 0, @address, @port)

sleep @interval if @interval > 0 unless @count == 1
end

return @count == 1 ? "Sending magic packet to #{@address}:#{@port} with #{mac}" : "Sending magic packet to #{@address}:#{@port} with #{mac} #{@count} times"
end
end
end
14 changes: 5 additions & 9 deletions spec/wakeonlan_spec.rb
Expand Up @@ -2,27 +2,23 @@

describe "Wake-On-LAN" do
it "should send a MagicPacket to the broadcast addresses" do
Wol::WakeOnLan.new.wake.should == "Sending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:ff:ff\n"
Wol::WakeOnLan.new.wake.should == "Sending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:ff:ff"
end

it "should send a MagicPacket to a specified mac" do
Wol::WakeOnLan.new(:macs => "ff:ff:ff:ff:ff:cc").wake.should == "Sending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:ff:cc\n"
Wol::WakeOnLan.new(:mac => "ff:ff:ff:ff:ff:cc").wake.should == "Sending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:ff:cc"
end

it "should send a MagicPacket to a specified address" do
Wol::WakeOnLan.new(:address => "example.com").wake.should == "Sending magic packet to example.com:9 with ff:ff:ff:ff:ff:ff\n"
Wol::WakeOnLan.new(:address => "example.com").wake.should == "Sending magic packet to example.com:9 with ff:ff:ff:ff:ff:ff"
end

it "should send a MagicPacket to a specified port" do
Wol::WakeOnLan.new(:port => 12).wake.should == "Sending magic packet to 255.255.255.255:12 with ff:ff:ff:ff:ff:ff\n"
Wol::WakeOnLan.new(:port => 12).wake.should == "Sending magic packet to 255.255.255.255:12 with ff:ff:ff:ff:ff:ff"
end

it "should send a MagicPacket to a specified mac, address, and port" do
Wol::WakeOnLan.new(:macs => "00:08:a1:a9:58:f6", :address => "example.com", :port => 80).wake.should == "Sending magic packet to example.com:80 with 00:08:a1:a9:58:f6\n"
end

it "should send MagicPackets to several hardware addresses at once" do
Wol::WakeOnLan.new(:macs => ["ff:ff:ff:ff:ff:cc", "ff:ff:ff:ff:cc:cc", "ff:ff:ff:ccf:cc:cc"]).wake.should == "Sending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:ff:cc\nSending magic packet to 255.255.255.255:9 with ff:ff:ff:ff:cc:cc\nSending magic packet to 255.255.255.255:9 with ff:ff:ff:ccf:cc:cc\n"
Wol::WakeOnLan.new(:mac => "00:08:a1:a9:58:f6", :address => "example.com", :port => 80).wake.should == "Sending magic packet to example.com:80 with 00:08:a1:a9:58:f6"
end

it "should return nil if quiet is set to true" do
Expand Down

0 comments on commit 3d6e76a

Please sign in to comment.