Netboot Mailing List (by thread)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Wake On LAN?

Here's a Perl version of the wakeup program. Let me know if it works
and send me any improvements. It's GPLed.

#!/usr/bin/perl -w
# Reads from stdin or a file lines of the form
# aa:bb:cc:dd:ee;ff or
# aa:bb:cc:dd:ee:ff
# which are hostname and MAC addresses of NICs to send a wakeup packet.
# This program may have to be run with superuser privilege because it
# may need to inject an ARP entry into the cache.
# Be careful, you could corrupt valid entries if those NICs are
# already active.
# Perl version by after DOS/Windows C version posted by
# on the Netboot mailing list
# Released under GNU Public License, 2000-01-05
use Socket;

while (<>) {
	($ip, $mac) = split;
	next if !defined($ip) or !defined($mac) or $ip eq '' or $mac eq '';
	&send_wakeup_packet($ip, $mac);

sub send_wakeup_packet {
	($ip, $mac) = @_;

	if (!defined($iaddr = inet_aton($ip))) {
		print "Cannot resolve $ip\n";
	if ($mac !~ /[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}/i)  {
		print "Malformed MAC address $mac\n";
	# Inject entry into ARP table, in case it's not there already
	system("arp -s $ip $mac") == 0
		or print "Warning: arp command failed, you need to be root\n";
	# Remove colons
	$mac =~ tr/://d;
	# Magic packet is 6 bytes of FF followed by the MAC address 16 times
	$magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
	# Create socket
	socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp'))
		or die "socket $!\n";
	# Send the wakeup packet
	print "Sending wakeup packet to $ip at MAC address $mac\n";
	defined(send(S, $magic, 0, sockaddr_in(0x2fff, $iaddr)))
		or print "send: $!\n";

For requests or suggestions regarding this mailing list archive please write to