File: wakeonlan

package info (click to toggle)
wakeonlan 0.41-11
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 120 kB
  • ctags: 10
  • sloc: perl: 120; makefile: 38
file content (262 lines) | stat: -rwxr-xr-x 6,381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
109
110
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/perl -w
#
# $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
#
#########################################################################       

use strict;
use Net::hostent;
use Socket;
use Getopt::Std;
use vars qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f);
$VERSION = '0.41';

my $DEFAULT_IP      = '255.255.255.255';
my $DEFAULT_PORT    = getservbyname('discard', 'udp');

#
# Process the command line
#

getopts("hvp:i:f:");

if ($opt_h) { usage(); exit(0); }
if ($opt_v) { print "wakeonlan version $VERSION\n"; exit(0); }
if (!$opt_f and !@ARGV) { usage(); exit(0); }
if ($opt_i) { $DEFAULT_IP = $opt_i; }		# override default value
if ($opt_p) { $DEFAULT_PORT = $opt_p; }		# override default value

if ($opt_f) { process_file($opt_f); }

# The rest of the command line is a list of hardware addresses 

foreach (@ARGV) {
	wake($_, $opt_i, $opt_p);
} 

#
# wake
#
# The 'magic packet' consists of 6 times 0xFF followed by 16 times
# the hardware address of the NIC. This sequence can be encapsulated
# in any kind of packet, in this case an UDP packet targeted at the
# discard port (9).
#                                                                               

sub wake
{
	my $host    = shift;
	my $ipaddr  = shift || $DEFAULT_IP;
	my $port    = shift || $DEFAULT_PORT;

	my ($raddr, $them, $proto);
	my ($hwaddr, $hwaddr_re, $pkt);
	
	# get the hardware address (ethernet address)

	$hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
	if ($host =~ m/^$hwaddr_re$/) {
		$hwaddr = $host;
	} else {
		# $host is not a hardware address, try to resolve it
		my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
		my $ip_addr;
		if ($host =~ m/^$ip_re$/) {
			$ip_addr = $host;
		} else {
			my $h;
			unless ($h = gethost($host)) {
				warn "$host is not a hardware address and I could not resolve it as to an IP address.\n";
				return undef;
			}
			$ip_addr = inet_ntoa($h->addr);
		}
		# look up ip in /etc/ethers
		unless (open (ETHERS, '<', '/etc/ethers')) {
			warn "$host is not a hardware address and I could not open /etc/ethers.\n";
			return undef;
		}
		while (<ETHERS>) {
			if (($_ !~ m/^$/) && ($_ !~ m/^#/)) { # ignore comments
				my ($mac, $ip);
				($mac, $ip) = split(' ', $_, 3);
				if ($ip =~ m/^$ip$/) {
					if ($ip eq $ip_addr or $ip eq $host) {
						$hwaddr = $mac;
						last;
					}
					next;
				} else {
					my $h2;
					unless ($h2 = gethost($ip)) {
						next;
					}
					if (inet_ntoa($h2->addr) eq $ip_addr) {
						$hwaddr = $mac;
						last;
					}
				}
			}
		}
		close (ETHERS);
		unless (defined($hwaddr)) {
			warn "Could not find $host in /etc/ethers\n";
			return undef;
		}
	}

	# Generate magic sequence

	foreach (split /:/, $hwaddr) {
		$pkt .= chr(hex($_));
	}
	$pkt = chr(0xFF) x 6 . $pkt x 16;

	# Allocate socket and send packet

	$raddr = gethostbyname($ipaddr)->addr;
	$them = pack_sockaddr_in($port, $raddr);
	$proto = getprotobyname('udp');

	socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
	setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";

	print "Sending magic packet to $ipaddr:$port with $hwaddr\n";

	send(S, $pkt, 0, $them) or die "send : $!";
	close S;
}

#
# process_file
#

sub process_file {
	my $filename = shift;
	my ($hwaddr, $ipaddr, $port);

	open (F, "<$filename") or die "open : $!";
	while(<F>) {
		next if /^\s*#/;		# ignore comments
		next if /^\s*$/;		# ignore empty lines

		chomp;
		($hwaddr, $ipaddr, $port) = split;

		wake($hwaddr, $ipaddr, $port);
	}
	close F;
}


#
# Usage
#

sub usage {
print <<__USAGE__;
Usage
    wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]

Options
    -h
        this information
    -v
        displays the script version
    -i ip_address
        set the destination IP address
        default: 255.255.255.255 (the limited broadcast address)
    -p port
        set the destination port
        default: 9 (the discard port)
    -f file 
        uses file as a source of hardware addresses

See also
    wakeonlan(1)    

__USAGE__
}


__END__

# Script documentation

=head1 NAME

wakeonlan - Perl script to wake up computers

=head1 SYNOPSIS

wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]

=head1 DESCRIPTION

This script sends 'magic packets' to wake-on-lan enabled ethernet adapters and motherboards, in order to switch on the called PC. Be sure to connect the NIC with the motherboard if neccesary, and enable the WOL function in the BIOS.

The 'magic packet' consists of 6 times 0xFF followed by 16 times the hardware address of the NIC. This sequence can be encapsulated in any kind of packet. This script uses UDP packets.

=head1 OPTIONS

=over

=item B<-h>

Displays the help information.

=item B<-v>

Displays the script version.

=item B<-i ip_address>

Destination IP address. Unless you have static ARP tables you should
use some kind of broadcast address (the broadcast address of the network where the computer resides or the limited broadcast address). Default: 255.255.255.255 (the limited broadcast address).

=item B<-p port>

Destination port. Default: 9 (the discard port).

=item B<-f file>

File with hardware addresses of wakeable computers. For an example check
the file lab001.wol in the examples subdirectory.

=back

=head1 EXAMPLES

Using the limited broadcast address (255.255.255.255):

    $ wakeonlan 01:02:03:04:05:06
    $ wakeonlan 01:02:03:04:05:06 01:02:03:04:05:07

Using a subnet broadcast address:

    $ wakeonlan -i 192.168.1.255 01:02:03:04:05:06

Using another destination port:

    $ wakeonlan -i 192.168.1.255 -p 1234 01:02:03:04:05:06

Using a file as source of hardware and IP addresses:

    $ wakeonlan -f examples/lab001.wol
    $ wakeonlan -f examples/lab001.wol 01:02:03:04:05:06

=head1 AUTHOR

Jos Pedro Oliveira <jpo@di.uminho.pt> maintaining and expanding original work done by Ico Doornekamp <ico@edd.dhs.org>.

=head1 COPYRIGHT

Copyright (c) 2000-2005 Jos Pedro Oliveira.

This is free software.  You may modify it and distribute it under Perl's Artistic Licence.  Modified versions must be clearly indicated.                                                    

=head1 SEE ALSO

For more information regarding this script and Wakeonlan technology just check the following address http://gsd.di.uminho.pt/jpo/software/wakeonlan/.

=cut