Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mp-form1.pl 5.6KB

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