2 # Magic Packet for the Web
3 # Perl version by ken.yap@acm.org after DOS/Windows C version posted by
4 # Steve_Marfisi@3com.com on the Netboot mailing list
5 # modified to work with web by G. Knauf <info@gknw.de>
6 # Released under GNU Public License
8 use CGI
qw(:standard); # import shortcuts
11 $ver = 'v0.52 © gk 2003-Apr-24 11:00:00';
13 # Defaults - Modify to point to the location of your mac file
14 $www = "$ENV{'DOCUMENT_ROOT'}/perldemo";
15 $maclist = "$www/maclist.txt";
16 # Defaults - Modify to fit to your network
17 $defbc = '255.255.255.255';
22 # Read in all the variables set by the form
33 print start_html
("Mp-Form - send Magic Packets");
34 # If defined new mac save it
35 if (defined(param
("mac"))) {
36 print h1
("Result of adding an entry to the maclist");
39 print '</TT></H3><HR>';
40 print '<FORM method="POST"><input type="submit" value="ok"></FORM>';
42 # send magic packets to selected macs
43 print h1
("Result of sending magic packets to multiple PCs");
49 my ($brc,$mac) = split(/-/,param
($_));
50 &send_broadcast_packet
(inet_aton
($brc),$mac);
53 print '</TT></H3><HR>';
54 print '<FORM><input type="button" value="back" onClick="history.back()"></FORM>';
56 # Close the document cleanly.
63 print start_html
("Mp-Form - send Magic Packets");
64 print h1
("Form for sending magic packets to multiple PCs");
68 <H2>Select the destination mac addresses:</H2>
69 <TABLE BORDER COLS=3 WIDTH="80%">
71 <TD><CENTER><B><FONT SIZE="+1">Broadcast address</FONT></B></CENTER></TD>
72 <TD><CENTER><B><FONT SIZE="+1">MAC address</FONT></B></CENTER></TD>
73 <TD><CENTER><B><FONT SIZE="+1">Name</FONT></B></CENTER></TD>
74 <TD><CENTER><B><FONT SIZE="+1"><input type=checkbox name="all" value="all">Select all</FONT></B></CENTER></TD>
77 # build up table with mac addresses
79 # print rest of the form
82 <P><B><FONT SIZE="+1">
83 Press <input type="submit" value="wakeup"> to send the magic packets to your selections.
84 Press <input type="reset" value="clear"> to reset the form.
89 <H2>Enter new destination mac address:</H2>
90 <B><FONT SIZE="+1">Broadcast: </FONT></B><input name="brc" size="15" maxlength="15" value="$defbc">
91 <B><FONT SIZE="+1">MAC: </FONT></B><input name="mac" size="17" maxlength="17">
92 <B><FONT SIZE="+1">Name: </FONT></B><input name="ip" size="40" maxlength="40">
93 <P><B><FONT SIZE="+1">
94 Press <input type="submit" value="save"> to add the entry to your maclist.
95 Press <input type="reset" value="clear"> to reset the form.
101 # Close the document cleanly.
105 sub send_broadcast_packet
{
107 if ($mac !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) {
108 print "Malformed MAC address $mac<BR>\n";
111 printf("Sending wakeup packet to %04X:%08X-%s<BR>\n", $port, unpack('N',$nbc), $mac);
114 # Magic packet is 6 bytes of FF followed by the MAC address 16 times
115 $magic = ("\xff" x
6) . (pack('H12', $mac) x
16);
117 socket(S
, PF_INET
, SOCK_DGRAM
, getprotobyname('udp')) or die "socket: $!\n";
119 setsockopt(S
, SOL_SOCKET
, SO_BROADCAST
, 1) or die "setsockopt: $!\n";
120 # Send the wakeup packet
121 defined(send(S
, $magic, 0, sockaddr_in
($port, $nbc))) or print "send: $!\n";
126 unless (open(F
, $maclist)) {
127 print "Error reading $maclist: $!\n";
130 next if (/^\s*#|^\s*;/); # skip comments
131 my ($mac, $ip) = split;
132 next if (!defined($mac) or $mac eq '');
135 ($bc,$mac) = split(/-/,$mac) if ($mac =~ /-/);
136 my $nbc = inet_aton
($bc);
138 &send_broadcast_packet
($nbc, $mac);
140 my $hbc = sprintf("0x%08X", unpack('N',$nbc));
141 print "<TD WIDTH=20%><CENTER><TT>$hbc</TT></CENTER></TD>";
142 print "<TD WIDTH=30%><CENTER><TT>$mac</TT></CENTER></TD>";
143 $ip = ' ' if (!defined($ip) or $ip eq '');
144 print "<TD WIDTH=30%><CENTER><TT>$ip</TT></CENTER></TD>";
145 print "<TD WIDTH=20%><CENTER><input type=checkbox name=mac$. value=$hbc-$mac><TT>WakeUp</TT></CENTER></TD></TR>\n";
153 if (param
("brc") !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i) {
154 print "Malformed broadcast address ",param
("brc"),"\n";
157 if (param
("mac") !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) {
158 print "Malformed MAC address ",param
("mac"),"\n";
161 unless (open(F
, ">> $maclist")) {
162 print "Error writing $maclist: $!\n";
164 #my $nbc = inet_aton(param("brc"));
165 #my $hbc = sprintf("0x%8X", unpack('N',$nbc));
166 #print F $hbc."-".uc(param("mac"))." ".param("ip")."\n";
167 print F param
("brc")."-".uc(param
("mac"))." ".param
("ip")."\n";
169 print "Saved entry to maclist: ".param
("brc")."-".uc(param
("mac"))." ".param
("ip")."\n";