wakeonlan 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ bin/wakeonlan
5
+ lib/wakeonlan.rb
@@ -0,0 +1,41 @@
1
+ = Wake On LAN
2
+
3
+ * http://wakeonlan.rubyforge.com/
4
+ * mailto:jens@hilli.dk
5
+
6
+ == Description
7
+
8
+ Wake On LAN library and system executable
9
+
10
+ == Prerequisites
11
+
12
+ Ruby 1.8.6 or later
13
+
14
+ == Installation
15
+
16
+ You can use RubyGems [rubyforge.org/projects/rubygems] to install the latest version of the WakeOnLAN library remotely.
17
+
18
+ gem install --remote wakeonlan
19
+
20
+
21
+ == Usage
22
+
23
+ require 'wakeonlan'
24
+
25
+ wol = WakeOnLan.new
26
+ wol.wake("00:14:5e:2a:1c:f7") # Send magic packet to the machine with the MAC address 00:14:5e:2a:1c:f7
27
+ wol.close
28
+
29
+ or just simply:
30
+
31
+ WakeOnLan.wake!("00:14:5e:2a:1c:f7")
32
+
33
+ or
34
+
35
+ WakeOnLan.wake!("my-sleepy-host")
36
+
37
+ if you have a /etc/ethers file. See `man 5 ethers` on a UNIX/BSD/Linux system
38
+
39
+ == LICENSE
40
+
41
+ WakeOnLAN library is Copyright (c) 2009 by Jens Hilligsoe. It is free software. Redistribution is permitted under the same terms and conditions as the standard Ruby distribution. See the COPYING file in the Ruby distribution for details.
@@ -0,0 +1,51 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/lib")
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'echoe'
6
+ require 'wakeonlan'
7
+
8
+
9
+ # Common package properties
10
+ PKG_NAME = ENV['PKG_NAME'] || WakeOnLan::GEM
11
+ PKG_VERSION = ENV['PKG_VERSION'] || WakeOnLan::VERSION
12
+ RUBYFORGE_PROJECT = 'wakeonlan'
13
+
14
+ if ENV['SNAPSHOT'].to_i == 1
15
+ PKG_VERSION << "." << Time.now.utc.strftime("%Y%m%d%H%M%S")
16
+ end
17
+
18
+
19
+ Echoe.new(PKG_NAME, PKG_VERSION) do |p|
20
+ p.author = "Jens Hilligsoe"
21
+ p.email = "jens@hilli.dk"
22
+ p.summary = "A Wake On LAN (WOL) client and library."
23
+ p.url = "http://wakeonlan.rubyforge.com/"
24
+ p.project = RUBYFORGE_PROJECT
25
+ p.description = <<-EOD
26
+ WakeOnLAN is a WOL client and library written in pure Ruby.
27
+ EOD
28
+ p.ignore_pattern = [ "nbproject/**/*", "docs/**/*"]
29
+ p.need_zip = true
30
+ p.has_rdoc = true
31
+ p.files = FileList["{bin,lib,docs}/**/*"].exclude("rdoc").to_a
32
+
33
+ p.development_dependencies += ["rake ~>0.8",
34
+ "echoe ~>3.1"]
35
+ end
36
+
37
+
38
+ desc "Open an irb session preloaded with this library"
39
+ task :console do
40
+ sh "irb -rubygems -I lib -r wakeonlan.rb"
41
+ end
42
+
43
+ begin
44
+ require 'code_statistics'
45
+ desc "Show library's code statistics"
46
+ task :stats do
47
+ CodeStatistics.new(["WakeOnLan", "lib"]).to_s
48
+ end
49
+ rescue LoadError
50
+ puts "CodeStatistics (Rails) is not available"
51
+ end
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ # $Id: wakeonlan 1 2009-10-27 22:43:55Z hilli $
3
+
4
+ require 'optparse'
5
+ require 'rubygems'
6
+ require File.dirname(__FILE__) + '/../lib/wakeonlan'
7
+
8
+ options = {
9
+ :ethers => nil,
10
+ :ip => "255.255.255.255",
11
+ :port => 9,
12
+ :verbose => false
13
+ }
14
+
15
+ opts = OptionParser.new do |opts|
16
+ opts.banner = "WakeOnLan: Ruby WakeOnLan Client"
17
+ opts.define_head "Usage: wakeonlan [options] [[MAC|IP|hostname] ...]"
18
+ opts.separator " "
19
+ opts.separator "Examples:"
20
+ opts.separator " wakeonlan 00:1e:4f:43:6d:f4"
21
+ opts.separator " wakeonlan 192.168.1.2"
22
+ opts.separator " "
23
+ opts.separator "Options:"
24
+
25
+ opts.on("-e FILE", "--ethers FILE", String, "Use FILE instead of /etc/ethers [default]") do |e|
26
+ options[:ethers] = e
27
+ end
28
+
29
+ opts.on("-p PORT", "--port PORT", Integer, "Port number to send the magick packet to - Default is 9") do |p|
30
+ options[:port] = p
31
+ end
32
+
33
+ opts.on("-v", "--verbose", "Verbose output") do |p|
34
+ options[:verbose] = true
35
+ end
36
+
37
+ opts.on("-a IP", "--dest-ip IP", String, "IP address to send magic packet to. " +
38
+ " This can be a gateway to another subnet. The default is the broadcast address 255.255.255.255") do |i|
39
+ options[:ip] = i
40
+ end
41
+
42
+ opts.separator " "
43
+
44
+ opts.on_tail("-h", "--help", "Show this message") do
45
+ puts opts
46
+ exit
47
+ end
48
+
49
+ opts.on_tail("-V", "--version", "Show version") do
50
+ puts "#{WakeOnLan::NAME} #{WakeOnLan::VERSION}"
51
+ exit
52
+ end
53
+ end
54
+ opts.parse!
55
+
56
+ wakies = ARGV.shift
57
+
58
+ if wakies.to_s.strip.empty?
59
+ puts opts
60
+ exit 1
61
+ end
62
+
63
+ @wol = WakeOnLan.new
64
+ @wol.ethers = options[:ethers] if options[:ethers]
65
+ @wol.verbose = options[:verbose] if options[:verbose]
66
+ wakies.split.each do |wake|
67
+ @wol.wake(@wol.resolve_to_mac(wake), options[:ip], options[:port])
68
+ end
69
+ @wol.close
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ # $Id: wakeonlan.rb 1 2009-10-27 22:43:55Z hilli $
3
+ # Jens Hilligsoe <jens@hilli.dk>, 2009
4
+ #
5
+ # Based on work by
6
+ # Kevin R. Bullock <kbullock@ringworld.org>, K.Kodama <kodama@math.kobe-u.ac.jp>,
7
+ # Jose Pedro Oliveira <jpo@di.uminho.pt> and Ico Doornekamp <ico@edd.dhs.org>
8
+ #
9
+ # Licensed under the Ruby licence
10
+
11
+ require "socket"
12
+
13
+ class WakeOnLan
14
+
15
+ GEM = "wakeonlan"
16
+ NAME = "WakeOnLan"
17
+ VERSION = "0.0.1"
18
+
19
+ attr_reader :socket
20
+ attr_accessor :ethers
21
+ attr_accessor :verbose
22
+
23
+ # Exceptions
24
+ class AddressException < RuntimeError; end
25
+ class SetupException < RuntimeError; end
26
+
27
+ def initialize
28
+ @ethers = "/etc/ethers"
29
+ # Create socket and set options
30
+ @socket = UDPSocket.open
31
+ @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
32
+ if "".respond_to?(:lines) then # Ruby 1.9 check
33
+ @@Regexp_MAC_addr = Regexp.new( '^' + (('[0-9A-Fa-f]' * 2).lines.to_a * 6).join(':') + '$' )
34
+ @@Regexp_IP_addr = Regexp.new( '^' + ('(([0-9][0-9]?)|([01][0-9][0-9])|(2[0-4][0-9])|(2[0-5][0-5]))'.lines.to_a * 4).join('\.') + '$' )
35
+ else
36
+ @@Regexp_MAC_addr = Regexp.new( '^' + (('[0-9A-Fa-f]' * 2).to_a * 6).join(':') + '$' )
37
+ @@Regexp_IP_addr = Regexp.new( '^' + ('(([0-9][0-9]?)|([01][0-9][0-9])|(2[0-4][0-9])|(2[0-5][0-5]))'.to_a * 4).join('\.') + '$' )
38
+ end
39
+ end
40
+
41
+ # Do the waking up bits
42
+ def wake(mac, ip="255.255.255.255", port=9)
43
+ # Validate mac
44
+ raise AddressException, 'Invalid MAC address' unless self.valid_mac?( mac )
45
+ # Validate ip
46
+ raise AddressException, 'Invalid IP address' unless self.valid_ip?( ip )
47
+ # Create Magic Packet
48
+ magic = 0xFF.chr * 6 + (mac.split(/:/).pack("H*H*H*H*H*H*") * 16)
49
+ @socket.connect(ip,port)
50
+ puts "Sending magic packet to #{ip}:#{port} with #{mac}" if @verbose
51
+ 3.times{ socket.send(magic,0) }
52
+ end
53
+
54
+ # Shorthand class method to facilitate WakeOnLan.wake!(MAC) kind of action
55
+ def self.wake!(mac, ip="255.255.255.255", port=9)
56
+ wol = WakeOnLan.new
57
+ wol.wake(resolve_to_mac(mac), ip, port)
58
+ wol.close
59
+ end
60
+
61
+ # Try to resolve the input given to a MAC address. Input can be either a hostname, IP or MAC address
62
+ def resolve_to_mac(input)
63
+ if valid_mac?(input)
64
+ return input
65
+ end
66
+ lookup_in_ethers(input)
67
+ end
68
+
69
+ # Given a hostname or IP, look it up in the ethers file and return the associated MAC address
70
+ def lookup_in_ethers(input)
71
+ if File.exist?(@ethers) and File.readable?(@ethers)
72
+ File.open(@ethers,'r') do |f|
73
+ f.each_line do |line|
74
+ mac, host = line.split
75
+ if input.match(host)
76
+ return mac
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ # MAC address validation
84
+ def valid_mac?(mac)
85
+ if mac =~ @@Regexp_MAC_addr
86
+ true
87
+ else
88
+ false
89
+ end
90
+ end
91
+
92
+ # IP address validation
93
+ def valid_ip?( ip )
94
+ if ip =~ @@Regexp_IP_addr
95
+ true
96
+ else
97
+ false
98
+ end
99
+ end
100
+
101
+ # Close the socket after use
102
+ def close
103
+ @socket.close
104
+ @socket=""
105
+ end
106
+ end # WakeOnLan
107
+
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{wakeonlan}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jens Hilligsoe"]
9
+ s.date = %q{2009-10-27}
10
+ s.default_executable = %q{wakeonlan}
11
+ s.description = %q{ WakeOnLAN is a WOL client and library written in pure Ruby.
12
+ }
13
+ s.email = %q{jens@hilli.dk}
14
+ s.executables = ["wakeonlan"]
15
+ s.extra_rdoc_files = ["README.rdoc", "bin/wakeonlan", "lib/wakeonlan.rb"]
16
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "bin/wakeonlan", "lib/wakeonlan.rb", "wakeonlan.gemspec"]
17
+ s.homepage = %q{http://wakeonlan.rubyforge.com/}
18
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Wakeonlan", "--main", "README.rdoc"]
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{wakeonlan}
21
+ s.rubygems_version = %q{1.3.5}
22
+ s.summary = %q{A Wake On LAN (WOL) client and library.}
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ s.add_development_dependency(%q<rake>, ["~> 0.8"])
30
+ s.add_development_dependency(%q<echoe>, ["~> 3.1"])
31
+ else
32
+ s.add_dependency(%q<rake>, ["~> 0.8"])
33
+ s.add_dependency(%q<echoe>, ["~> 3.1"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<rake>, ["~> 0.8"])
37
+ s.add_dependency(%q<echoe>, ["~> 3.1"])
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wakeonlan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jens Hilligsoe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-27 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "0.8"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: echoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: "3.1"
34
+ version:
35
+ description: " WakeOnLAN is a WOL client and library written in pure Ruby.\n"
36
+ email: jens@hilli.dk
37
+ executables:
38
+ - wakeonlan
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - bin/wakeonlan
44
+ - lib/wakeonlan.rb
45
+ files:
46
+ - Manifest
47
+ - README.rdoc
48
+ - Rakefile
49
+ - bin/wakeonlan
50
+ - lib/wakeonlan.rb
51
+ - wakeonlan.gemspec
52
+ has_rdoc: true
53
+ homepage: http://wakeonlan.rubyforge.com/
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --line-numbers
59
+ - --inline-source
60
+ - --title
61
+ - Wakeonlan
62
+ - --main
63
+ - README.rdoc
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "1.2"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project: wakeonlan
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A Wake On LAN (WOL) client and library.
85
+ test_files: []
86
+