3 # $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
5 #########################################################################
10 use vars
qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f);
13 my $DEFAULT_IP = '255.255.255.255';
14 my $DEFAULT_PORT = getservbyname('discard', 'udp');
17 # Process the command line
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
33 wake($_, $opt_i, $opt_p);
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
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";
62 # Generate magic sequence
64 foreach (split /:/, $hwaddr) {
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 : $!";
90 my ($hwaddr, $ipaddr, $port);
92 open (F, "<$filename") or die "open : $!";
94 next if /^\s*#/; # ignore comments
95 next if /^\s*$/; # ignore empty lines
98 ($hwaddr, $ipaddr, $port) = split;
100 wake($hwaddr, $ipaddr, $port);
113 wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
119 displays the script version
121 set the destination IP address
122 default: 255.255.255.255 (the limited broadcast address)
124 set the destination port
125 default: 9 (the discard port)
127 uses file as a source of hardware addresses
138 # Script documentation
142 wakeonlan - Perl script to wake up computers
146 wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
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.
160 Displays the help information.
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).
173 Destination port. Default: 9 (the discard port).
177 File with hardware addresses of wakeable computers. For an example check
178 the file lab001.wol in the examples subdirectory.
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
204 José Pedro Oliveira <jpo@di.uminho.pt> maintaining and expanding original work done by Ico Doornekamp <ico@edd.dhs.org>.
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.
214 For more information regarding this script and Wakeonlan technology just check the following address http://gsd.di.uminho.pt/jpo/software/wakeonlan/.