-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwakeonlan.pl
executable file
·371 lines (254 loc) · 7.73 KB
/
wakeonlan.pl
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/perl -w
#
# $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
#
######################################################################
use strict;
use Socket;
use Getopt::Long;
use Pod::Usage;
our $VERSION = '0.41_90';
use constant {
PORT_MIN => 0,
PORT_MAX => 65535,
};
#
# Hardware address formats
#
our @hwaddr_regexs = (
# xx:xx:xx:xx:xx:xx (canonical)
'^(?:[\da-f]{1,2}:){5}[\da-f]{1,2}$',
# xx-xx-xx-xx-xx-xx (Windows)
'^(?:[\da-f]{1,2}-){5}[\da-f]{1,2}$',
# xxxxxx-xxxxxx (Hewlett-Packard switches)
'^[\da-f]{6}-[\da-f]{6}$',
# xxxxxxxxxxxx (Intel Landesk)
'^[\da-f]{12}$',
);
my $DEFAULT_IP = '255.255.255.255';
my $DEFAULT_PORT = getservbyname('discard', 'udp');
my $verbose = 1;
my $dryrun = 0;
my $filename = '';
my @queue = ();
my %stats = (
total => 0,
valid => 0,
invalid => 0,
sent => 0,
);
######################################################################
sub isValidPort {
my $port = shift;
return ($port >= PORT_MIN and $port <= PORT_MAX) ? 1 : 0;
}
sub isValidIPAddress {
my $ipaddress = shift;
my $t = 0;
$t += $_ for map { ($_ <= 255)?1:0 }
$ipaddress =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
return ($t == 4) ? 1 : 0;
}
sub isValidHardwareAddress {
my $hwaddr = shift;
foreach my $re (@hwaddr_regexs) {
return 1 if ($hwaddr =~ m/$re/i);
}
return 0;
}
######################################################################
sub loadFromCommandLine {
for my $arg (@_) {
$stats{total}++;
if (! isValidHardwareAddress($arg) ) {
warn "Invalid hardware address: $arg\n";
$stats{invalid}++;
next;
}
$stats{valid}++;
push @queue, [ $arg, $DEFAULT_IP, $DEFAULT_PORT ];
}
}
sub loadFromFile {
my $filename = shift;
my ($hwaddr, $ipaddr, $port);
open (my $FILE, '<', $filename) or die "open : $!";
while(<$FILE>) {
next if /^\s*#/; # ignore comment lines
next if /^\s*$/; # ignore empty lines
$stats{total}++;
chomp;
($hwaddr, $ipaddr, $port) = split;
if (! isValidHardwareAddress($hwaddr) ) {
warn "Invalid hardware address: $hwaddr\n";
$stats{invalid}++;
next;
}
$ipaddr = $DEFAULT_IP unless defined($ipaddr);
if (! isValidIPAddress($ipaddr) ) {
warn "Invalid IP address: $ipaddr\n";
$stats{invalid}++;
next;
}
$port = $DEFAULT_PORT unless defined($port);
if (! isValidPort($port) ) {
warn "Invalid port number: $port\n";
$stats{invalid}++;
next;
}
$stats{valid}++;
push @queue, [ $hwaddr, $ipaddr, $port ];
}
close $FILE;
}
######################################################################
#
# 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 ($sock, $hwaddr, $ipaddr, $port) = @_;
my ($raddr, $them, $pkt);
#
# Expects hardware address in canonical form (xx:xx:xx:xx:xx:xx)
#
#
# Generate the magic sequence
#
foreach (split /:/, $hwaddr) {
$pkt .= chr(hex($_));
}
$pkt = chr(0xFF) x 6 . $pkt x 16;
#
# Send packet
#
# Patch by Eugenio Jarosiewicz <ej0 at mac.com>
# $raddr = inet_aton($ipaddr);
#
$raddr = gethostbyname($ipaddr);
$them = pack_sockaddr_in($port, $raddr);
print "Sending magic packet to $ipaddr:$port with payload $hwaddr\n"
if $verbose;
#send($sock, $pkt, 0, $them) or die "send : $!";
send($sock, $pkt, 0, $them) unless $dryrun;
}
sub sendMagicPackets {
if (! @queue) {
warn "Nothing to do!\n";
return;
}
my $sock;
my $proto = getprotobyname('udp');
socket($sock, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
for my $ref (@queue) {
wake($sock, $ref->[0], $ref->[1], $ref->[2]);
$stats{sent}++;
}
close $sock;
}
######################################################################
# main
######################################################################
#
# Process the command line
#
GetOptions(
"h|help" => sub { pod2usage( -exitval => 0, -verbose => 1); },
"v|version" => sub { print "wakeonlan $VERSION\n"; exit(0); },
"q|quiet" => sub { $verbose = 0; },
"i|ip=s" => \$DEFAULT_IP,
"p|port=i" => \$DEFAULT_PORT,
"f|file=s" => \$filename,
"n|dry-run" => sub { $dryrun = 1; },
) or pod2usage( -exitval => 1, -verbose => 1);
#
# Validate information
#
#
if (! isValidPort($DEFAULT_PORT)) {
warn "Invalid default port number: $DEFAULT_PORT\n";
exit(2);
}
if (! isValidIPAddress($DEFAULT_IP)) {
warn "Invalid default IP address: $DEFAULT_IP\n";
exit(3);
}
if ($filename and ! -f $filename) {
warn "Invalid filename: $filename\n";
exit(4);
}
#
# Nothing to do ?
#
if (!$filename and !@ARGV) { pod2usage( -exitval => 0, -verbose => 1); };
#
# Load hardware addresses
#
loadFromCommandLine(@ARGV);
loadFromFile($filename) if $filename;
# print Dumper(@queue);
sendMagicPackets();
#
# Print statistics
#
if ($verbose) {
printf "Hardware addresses: <total=%d, valid=%d, invalid=%d>\n",
$stats{total}, $stats{valid}, $stats{invalid};
printf "Magic packets: <sent=%d>\n", $stats{sent};
}
exit 0;
##########
__END__
# Script documentation
=encoding utf8
=head1 NAME
wakeonlan - Perl script to wake up computers
=head1 SYNOPSIS
wakeonlan [-h|--help] [-v|--version] [-q|--quiet] [-n|--dry-run] [-i|--ip IP_address] [-p|--port port] [-f|--file file_name] [[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 necessary, and to 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, --help>
Displays the help information.
=item B<-v, --version>
Displays the script version.
=item B<-i, --ip=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=port>
Destination port. Default: 9 (the discard port).
=item B<-f, --file=file_name>
File with hardware addresses of wakeable computers. For an example check
the file lab001.wol in the examples subdirectory.
=item B<-q, --quiet>
Quiet mode.
=item B<-n, --dry-run>
Print the commands that would be executed, but do not execute them.
=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-2009 José Pedro Oliveira.
This is free software. You may modify it and distribute it under Perl's Artistic License. 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
# vim:set ai ts=4 sw=4 sts=4 syntax=perl: