modified: n.fq
[GalaxyCodeBases.git] / tools / etc / wakeonlan
blob90874a7253e0b522e4540f266c43e38b78d1b154
1 #!/usr/bin/perl -w
3 # $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
5 #########################################################################
7 use strict;
8 use Socket;
9 use Getopt::Std;
10 use vars qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f);
11 $VERSION = '0.41';
13 my $DEFAULT_IP = '255.255.255.255';
14 my $DEFAULT_PORT = getservbyname('discard', 'udp');
17 # Process the command line
20 getopts("hvp:i:f:");
22 if ($opt_h) { usage(); exit(0); }
23 if ($opt_v) { print "wakeonlan version $VERSION\n"; exit(0); }
24 if (!$opt_f and !@ARGV) { usage(); exit(0); }
25 if ($opt_i) { $DEFAULT_IP = $opt_i; } # override default value
26 if ($opt_p) { $DEFAULT_PORT = $opt_p; } # override default value
28 if ($opt_f) { process_file($opt_f); }
30 # The rest of the command line is a list of hardware addresses
32 foreach (@ARGV) {
33 wake($_, $opt_i, $opt_p);
37 # wake
39 # The 'magic packet' consists of 6 times 0xFF followed by 16 times
40 # the hardware address of the NIC. This sequence can be encapsulated
41 # in any kind of packet, in this case an UDP packet targeted at the
42 # discard port (9).
45 sub wake
47 my $hwaddr = shift;
48 my $ipaddr = shift || $DEFAULT_IP;
49 my $port = shift || $DEFAULT_PORT;
51 my ($raddr, $them, $proto);
52 my ($hwaddr_re, $pkt);
54 # Validate hardware address (ethernet address)
56 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
57 if ($hwaddr !~ m/^$hwaddr_re$/) {
58 warn "Invalid hardware address: $hwaddr\n";
59 return undef;
62 # Generate magic sequence
64 foreach (split /:/, $hwaddr) {
65 $pkt .= chr(hex($_));
67 $pkt = chr(0xFF) x 6 . $pkt x 16;
69 # Allocate socket and send packet
71 $raddr = gethostbyname($ipaddr);
72 $them = pack_sockaddr_in($port, $raddr);
73 $proto = getprotobyname('udp');
75 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
76 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
78 print "Sending magic packet to $ipaddr:$port with $hwaddr\n";
80 send(S, $pkt, 0, $them) or die "send : $!";
81 close S;
85 # process_file
88 sub process_file {
89 my $filename = shift;
90 my ($hwaddr, $ipaddr, $port);
92 open (F, "<$filename") or die "open : $!";
93 while(<F>) {
94 next if /^\s*#/; # ignore comments
95 next if /^\s*$/; # ignore empty lines
97 chomp;
98 ($hwaddr, $ipaddr, $port) = split;
100 wake($hwaddr, $ipaddr, $port);
102 close F;
107 # Usage
110 sub usage {
111 print <<__USAGE__;
112 Usage
113 wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
115 Options
117 this information
119 displays the script version
120 -i ip_address
121 set the destination IP address
122 default: 255.255.255.255 (the limited broadcast address)
123 -p port
124 set the destination port
125 default: 9 (the discard port)
126 -f file
127 uses file as a source of hardware addresses
129 See also
130 wakeonlan(1)
132 __USAGE__
136 __END__
138 # Script documentation
140 =head1 NAME
142 wakeonlan - Perl script to wake up computers
144 =head1 SYNOPSIS
146 wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
148 =head1 DESCRIPTION
150 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.
152 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.
154 =head1 OPTIONS
156 =over
158 =item B<-h>
160 Displays the help information.
162 =item B<-v>
164 Displays the script version.
166 =item B<-i ip_address>
168 Destination IP address. Unless you have static ARP tables you should
169 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).
171 =item B<-p port>
173 Destination port. Default: 9 (the discard port).
175 =item B<-f file>
177 File with hardware addresses of wakeable computers. For an example check
178 the file lab001.wol in the examples subdirectory.
180 =back
182 =head1 EXAMPLES
184 Using the limited broadcast address (255.255.255.255):
186 $ wakeonlan 01:02:03:04:05:06
187 $ wakeonlan 01:02:03:04:05:06 01:02:03:04:05:07
189 Using a subnet broadcast address:
191 $ wakeonlan -i 192.168.1.255 01:02:03:04:05:06
193 Using another destination port:
195 $ wakeonlan -i 192.168.1.255 -p 1234 01:02:03:04:05:06
197 Using a file as source of hardware and IP addresses:
199 $ wakeonlan -f examples/lab001.wol
200 $ wakeonlan -f examples/lab001.wol 01:02:03:04:05:06
202 =head1 AUTHOR
204 José Pedro Oliveira <jpo@di.uminho.pt> maintaining and expanding original work done by Ico Doornekamp <ico@edd.dhs.org>.
206 =head1 COPYRIGHT
208 Copyright (c) 2000-2005 José Pedro Oliveira.
210 This is free software. You may modify it and distribute it under Perl's Artistic Licence. Modified versions must be clearly indicated.
212 =head1 SEE ALSO
214 For more information regarding this script and Wakeonlan technology just check the following address http://gsd.di.uminho.pt/jpo/software/wakeonlan/.
216 =cut