#!/usr/bin/perl # 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 guenter.knauf@dialup.soco.de # Released under GNU Public License # require "cgi-lib.pl"; use Socket; $ver = 'v0.32 © gk 2000-05-20 15:00:00'; # Defaults - Modify to point to the location of your maclist $www = "$ENV{'DOCUMENT_ROOT'}/perldemo"; $maclist = "$www/wakeup.txt"; #$scrname = $ENV{'SCRIPT_NAME'}; $scrname = $0; MAIN: { # Read in all the variables set by the form if (&ReadParse(*input)) { &ProcessForm; } else { &PrintForm; } } sub ProcessForm { # 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) { &send_broadcast_packet($input{$x}); } } print '


'; print '
'; } # Close the document cleanly. print &HtmlBot; exit; } sub PrintForm { 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:

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 { ($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; } print "Sending wakeup packet to MAC address $mac
\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"; # Enable broadcast setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt: $!\n"; # Send the wakeup packet defined(send(S, $magic, 0, sockaddr_in(0x2fff, INADDR_BROADCAST))) or print "send: $!\n"; close(S); } sub process_file { unless (open(F, $maclist)) { print "Error reading $maclist: $!\n"; } else { while () { next if /^\s*#/; # skip comments ($mac, $ip) = split; next if (!defined($mac) or $mac eq ''); if ($_[0] eq 'S') { &send_broadcast_packet($mac); } else { print "
"; $ip = ' ' if (!defined($ip) or $ip eq ''); print ""; print "\n"; } } close(F); } } sub add_entry { if ($input{'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; } unless (open(F, ">> $maclist")) { print "Error writing $maclist: $!\n"; } else { print F "$input{'mac'} $input{'ip'}\n"; close(F); print "Saved entry to maclist: $input{'mac'} $input{'ip'}\n"; } }
MAC address
Name
Select all
$mac
$ip
WakeUp