Remove obsolete files (INSTALL, RELNOTES)
[gpxe.git] / contrib / wakeonlan / mp-form.pl
blob144b507fc0bb81d6dbf6c30d84bc88082e6c786e
1 #!/perl/bin/perl -w
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
9 use Socket;
11 $ver = 'v0.52 &copy; 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';
18 $port = 60000;
20 MAIN:
22 # Read in all the variables set by the form
23 if (param()) {
24 &process_form();
25 } else {
26 &print_form();
30 sub process_form {
31 # Print the header
32 print header();
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");
37 print '<HR><H3><TT>';
38 &add_entry();
39 print '</TT></H3><HR>';
40 print '<FORM method="POST"><input type="submit" value="ok"></FORM>';
41 } else {
42 # send magic packets to selected macs
43 print h1("Result of sending magic packets to multiple PCs");
44 print '<HR><H3><TT>';
45 if (param("all")) {
46 &process_file(S);
47 } else {
48 for (param()) {
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.
57 print end_html;
60 sub print_form {
61 # Print the header
62 print header();
63 print start_html("Mp-Form - send Magic Packets");
64 print h1("Form for sending magic packets to multiple PCs");
65 print <<ENDOFTEXT;
66 <HR>
67 <FORM method="POST">
68 <H2>Select the destination mac addresses:</H2>
69 <TABLE BORDER COLS=3 WIDTH="80%">
70 <TR>
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>
75 </TR>
76 ENDOFTEXT
77 # build up table with mac addresses
78 &process_file(R);
79 # print rest of the form
80 print <<ENDOFTEXT;
81 </TABLE>
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.
85 </FONT></B>
86 </FORM>
87 <HR>
88 <FORM method="POST">
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.
96 </FONT></B>
97 </FORM>
98 <HR>
99 $ver
100 ENDOFTEXT
101 # Close the document cleanly.
102 print end_html;
105 sub send_broadcast_packet {
106 my ($nbc,$mac) = @_;
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";
109 return;
111 printf("Sending wakeup packet to %04X:%08X-%s<BR>\n", $port, unpack('N',$nbc), $mac);
112 # Remove colons
113 $mac =~ tr/://d;
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);
116 # Create socket
117 socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die "socket: $!\n";
118 # Enable broadcast
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";
122 close(S);
125 sub process_file {
126 unless (open(F, $maclist)) {
127 print "Error reading $maclist: $!\n";
128 } else {
129 while (<F>) {
130 next if (/^\s*#|^\s*;/); # skip comments
131 my ($mac, $ip) = split;
132 next if (!defined($mac) or $mac eq '');
133 $mac = uc($mac);
134 my $bc = $defbc;
135 ($bc,$mac) = split(/-/,$mac) if ($mac =~ /-/);
136 my $nbc = inet_aton($bc);
137 if ($_[0] eq 'S') {
138 &send_broadcast_packet($nbc, $mac);
139 } else {
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 = '&nbsp;' 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";
148 close(F);
152 sub add_entry {
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";
155 return;
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";
159 return;
161 unless (open(F, ">> $maclist")) {
162 print "Error writing $maclist: $!\n";
163 } else {
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";
168 close(F);
169 print "Saved entry to maclist: ".param("brc")."-".uc(param("mac"))." ".param("ip")."\n";