#!/usr/bin/perl -w # Magic Packet for the Web # Perl version by ken.yap@acm.org after DOS/Windows C version posted by # Steve_Marfisi@3com.com on the Netboot mailing list # modified to work with web by G. Knauf # Released under GNU Public License # require "cgi-lib.pl"; use Socket; $ver = 'v0.52 © gk 2003-Apr-24 11:00:00'; # Defaults - Modify to point to the location of your mac file $www = "$ENV{'DOCUMENT_ROOT'}/perldemo"; $maclist = "$www/maclist.txt"; # Defaults - Modify to fit to your network $defbc = '255.255.255.255'; $port = 60000; MAIN: { # Read in all the variables set by the form if (&ReadParse(*input)) { &process_form(); } else { &print_form(); } } sub process_form { # Print the header print &PrintHeader(); # If defined new mac save it if (defined($input{'mac'})) { print &HtmlTop("Result of adding an entry to the maclist"); print '

'; &add_entry; print '


'; print '
'; } else { # send magic packets to selected macs print &HtmlTop("Result of sending magic packets to multiple PCs"); print '

'; if (defined($input{'all'})) { &process_file(S); } else { foreach $x (keys %input) { my ($brc,$mac) = split(/-/,$input{$x}); &send_broadcast_packet(inet_aton($brc),$mac); } } print '


'; print '
'; } # Close the document cleanly. print &HtmlBot; exit; } sub print_form { print &PrintHeader(); print &HtmlTop("Form for sending magic packets to multiple PCs"); # Print out the body of the form print <

Select the destination mac addresses:

ENDOFTEXT # build up table with mac addresses &process_file; # print rest of the form print <

Press to send the magic packets to your selections. Press to reset the form.


Enter new destination mac address:

Broadcast: MAC: Name:

Press to add the entry to your maclist. Press to reset the form.


$ver ENDOFTEXT # Close the document cleanly. print &HtmlBot; } sub send_broadcast_packet { my ($nbc,$mac) = @_; 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"; return; } printf("Sending wakeup packet to %04X:%08X-%s
\n", $port, unpack('N',$nbc), $mac); # 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"; # Enable broadcast setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt: $!\n"; # Send the wakeup packet defined(send(S, $magic, 0, sockaddr_in($port, $nbc))) or print "send: $!\n"; close(S); } sub process_file { unless (open(F, $maclist)) { print "Error reading $maclist: $!\n"; } else { while () { next if (/^\s*#|^\s*;/); # skip comments my ($mac, $ip) = split; next if (!defined($mac) or $mac eq ''); $mac = uc($mac); my $bc = $defbc; ($bc,$mac) = split(/-/,$mac) if ($mac =~ /-/); my $nbc = inet_aton($bc); if ($_[0] eq 'S') { &send_broadcast_packet($nbc, $mac); } else { my $hbc = sprintf("0x%08X", unpack('N',$nbc)); print "
"; print ""; $ip = ' ' if (!defined($ip) or $ip eq ''); print ""; print "\n"; } } close(F); } } sub add_entry { if (param("brc") !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i) { print "Malformed broadcast address ",param("brc"),"\n"; return; } if (param("mac") !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) { print "Malformed MAC address ",param("mac"),"\n"; return; } unless (open(F, ">> $maclist")) { print "Error writing $maclist: $!\n"; } else { #my $nbc = inet_aton(param("brc")); #my $hbc = sprintf("0x%8X", unpack('N',$nbc)); #print F $hbc."-".uc(param("mac"))." ".param("ip")."\n"; print F param("brc")."-".uc(param("mac"))." ".param("ip")."\n"; close(F); print "Saved entry to maclist: ".param("brc")."-".uc(param("mac"))." ".param("ip")."\n"; } }
Broadcast address
MAC address
Name
Select all
$hbc
$mac
$ip
WakeUp