選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mp-form.pl 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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
  7. #
  8. use CGI qw(:standard); # import shortcuts
  9. use Socket;
  10. $ver = 'v0.52 &copy; gk 2003-Apr-24 11:00:00';
  11. # Defaults - Modify to point to the location of your mac file
  12. $www = "$ENV{'DOCUMENT_ROOT'}/perldemo";
  13. $maclist = "$www/maclist.txt";
  14. # Defaults - Modify to fit to your network
  15. $defbc = '255.255.255.255';
  16. $port = 60000;
  17. MAIN:
  18. {
  19. # Read in all the variables set by the form
  20. if (param()) {
  21. &process_form();
  22. } else {
  23. &print_form();
  24. }
  25. }
  26. sub process_form {
  27. # Print the header
  28. print header();
  29. print start_html("Mp-Form - send Magic Packets");
  30. # If defined new mac save it
  31. if (defined(param("mac"))) {
  32. print h1("Result of adding an entry to the maclist");
  33. print '<HR><H3><TT>';
  34. &add_entry();
  35. print '</TT></H3><HR>';
  36. print '<FORM method="POST"><input type="submit" value="ok"></FORM>';
  37. } else {
  38. # send magic packets to selected macs
  39. print h1("Result of sending magic packets to multiple PCs");
  40. print '<HR><H3><TT>';
  41. if (param("all")) {
  42. &process_file(S);
  43. } else {
  44. for (param()) {
  45. my ($brc,$mac) = split(/-/,param($_));
  46. &send_broadcast_packet(inet_aton($brc),$mac);
  47. }
  48. }
  49. print '</TT></H3><HR>';
  50. print '<FORM><input type="button" value="back" onClick="history.back()"></FORM>';
  51. }
  52. # Close the document cleanly.
  53. print end_html;
  54. }
  55. sub print_form {
  56. # Print the header
  57. print header();
  58. print start_html("Mp-Form - send Magic Packets");
  59. print h1("Form for sending magic packets to multiple PCs");
  60. print <<ENDOFTEXT;
  61. <HR>
  62. <FORM method="POST">
  63. <H2>Select the destination mac addresses:</H2>
  64. <TABLE BORDER COLS=3 WIDTH="80%">
  65. <TR>
  66. <TD><CENTER><B><FONT SIZE="+1">Broadcast address</FONT></B></CENTER></TD>
  67. <TD><CENTER><B><FONT SIZE="+1">MAC address</FONT></B></CENTER></TD>
  68. <TD><CENTER><B><FONT SIZE="+1">Name</FONT></B></CENTER></TD>
  69. <TD><CENTER><B><FONT SIZE="+1"><input type=checkbox name="all" value="all">Select all</FONT></B></CENTER></TD>
  70. </TR>
  71. ENDOFTEXT
  72. # build up table with mac addresses
  73. &process_file(R);
  74. # print rest of the form
  75. print <<ENDOFTEXT;
  76. </TABLE>
  77. <P><B><FONT SIZE="+1">
  78. Press <input type="submit" value="wakeup"> to send the magic packets to your selections.
  79. Press <input type="reset" value="clear"> to reset the form.
  80. </FONT></B>
  81. </FORM>
  82. <HR>
  83. <FORM method="POST">
  84. <H2>Enter new destination mac address:</H2>
  85. <B><FONT SIZE="+1">Broadcast: </FONT></B><input name="brc" size="15" maxlength="15" value="$defbc">
  86. <B><FONT SIZE="+1">MAC: </FONT></B><input name="mac" size="17" maxlength="17">
  87. <B><FONT SIZE="+1">Name: </FONT></B><input name="ip" size="40" maxlength="40">
  88. <P><B><FONT SIZE="+1">
  89. Press <input type="submit" value="save"> to add the entry to your maclist.
  90. Press <input type="reset" value="clear"> to reset the form.
  91. </FONT></B>
  92. </FORM>
  93. <HR>
  94. $ver
  95. ENDOFTEXT
  96. # Close the document cleanly.
  97. print end_html;
  98. }
  99. sub send_broadcast_packet {
  100. my ($nbc,$mac) = @_;
  101. if ($mac !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) {
  102. print "Malformed MAC address $mac<BR>\n";
  103. return;
  104. }
  105. printf("Sending wakeup packet to %04X:%08X-%s<BR>\n", $port, unpack('N',$nbc), $mac);
  106. # Remove colons
  107. $mac =~ tr/://d;
  108. # Magic packet is 6 bytes of FF followed by the MAC address 16 times
  109. $magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
  110. # Create socket
  111. socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die "socket: $!\n";
  112. # Enable broadcast
  113. setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt: $!\n";
  114. # Send the wakeup packet
  115. defined(send(S, $magic, 0, sockaddr_in($port, $nbc))) or print "send: $!\n";
  116. close(S);
  117. }
  118. sub process_file {
  119. unless (open(F, $maclist)) {
  120. print "Error reading $maclist: $!\n";
  121. } else {
  122. while (<F>) {
  123. next if (/^\s*#|^\s*;/); # skip comments
  124. my ($mac, $ip) = split;
  125. next if (!defined($mac) or $mac eq '');
  126. $mac = uc($mac);
  127. my $bc = $defbc;
  128. ($bc,$mac) = split(/-/,$mac) if ($mac =~ /-/);
  129. my $nbc = inet_aton($bc);
  130. if ($_[0] eq 'S') {
  131. &send_broadcast_packet($nbc, $mac);
  132. } else {
  133. my $hbc = sprintf("0x%08X", unpack('N',$nbc));
  134. print "<TD WIDTH=20%><CENTER><TT>$hbc</TT></CENTER></TD>";
  135. print "<TD WIDTH=30%><CENTER><TT>$mac</TT></CENTER></TD>";
  136. $ip = '&nbsp;' if (!defined($ip) or $ip eq '');
  137. print "<TD WIDTH=30%><CENTER><TT>$ip</TT></CENTER></TD>";
  138. print "<TD WIDTH=20%><CENTER><input type=checkbox name=mac$. value=$hbc-$mac><TT>WakeUp</TT></CENTER></TD></TR>\n";
  139. }
  140. }
  141. close(F);
  142. }
  143. }
  144. sub add_entry {
  145. if (param("brc") !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i) {
  146. print "Malformed broadcast address ",param("brc"),"\n";
  147. return;
  148. }
  149. if (param("mac") !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) {
  150. print "Malformed MAC address ",param("mac"),"\n";
  151. return;
  152. }
  153. unless (open(F, ">> $maclist")) {
  154. print "Error writing $maclist: $!\n";
  155. } else {
  156. #my $nbc = inet_aton(param("brc"));
  157. #my $hbc = sprintf("0x%8X", unpack('N',$nbc));
  158. #print F $hbc."-".uc(param("mac"))." ".param("ip")."\n";
  159. print F param("brc")."-".uc(param("mac"))." ".param("ip")."\n";
  160. close(F);
  161. print "Saved entry to maclist: ".param("brc")."-".uc(param("mac"))." ".param("ip")."\n";
  162. }
  163. }