You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wakeup.pl 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/perl
  2. #!/usr/bin/perl -w
  3. #
  4. # If called as wakeup.pl -f file it reads lines of the form
  5. #
  6. # aa:bb:cc:dd:ee;ff 12.34.56.78 or
  7. # aa:bb:cc:dd:ee:ff foo.bar.com
  8. # aa:bb:cc:dd:ee:ff
  9. #
  10. # which are MAC addresses and hostnames of NICs to send a wakeup packet.
  11. # Broadcast is used to send the magic packets, so anybody can run the command.
  12. # Notice that many routers do NOT forward broadcasts automatically!!
  13. # Comments in the file start with #.
  14. #
  15. # Or MAC addresses can be specified on the command line
  16. #
  17. # wakeup.pl aa.bb.cc.dd.ee.ff
  18. #
  19. # Or both can be used:
  20. #
  21. # wakeup.pl -f addresses.cfg 11:22:33:44:55:66
  22. #
  23. # Use option -b to specify broadcast mask.
  24. # Use option -d for screen output.
  25. #
  26. # Perl version by ken.yap@acm.org after DOS/Windows C version posted by
  27. # Steve_Marfisi@3com.com on the Netboot mailing list
  28. # Released under GNU Public License, 2000-01-08
  29. # Modified for use with NetWare by gk@gknw.de, 2000-09-18
  30. # With NetWare you have to use Socket.NLP from NetWare Perl #334 or higher!
  31. # You could download Socket.NLP #334 from: http://www.gknw.de/mpform.html
  32. #
  33. use Getopt::Std;
  34. use Socket;
  35. getopts('b:df:p:q');
  36. $brc = $opt_b || '255.255.255.255';
  37. $port = $opt_p || 60000;
  38. die "Malformed broadcast address: $brc!\n" if ($brc !~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)/);
  39. if (defined($opt_f)) {
  40. unless (open(F, $opt_f)) {
  41. print "open: $opt_f: $!\n";
  42. } else {
  43. print "Using file $opt_f...\n" if ($opt_d);
  44. while (<F>) {
  45. next if /^\s*#/; # skip comments
  46. my ($mac, $ip) = split;
  47. next if !defined($mac) or $mac eq '';
  48. &send_broadcast_packet($mac,$ip);
  49. }
  50. close(F);
  51. }
  52. }
  53. while (@ARGV) {
  54. send_broadcast_packet(shift(@ARGV));
  55. }
  56. sub send_broadcast_packet {
  57. my ($mac,$ip) = @_;
  58. if ($mac =~ /-/) {
  59. ($bc,$mac) = split(/-/,$mac);
  60. } else {
  61. $bc = $brc;
  62. }
  63. if ($mac !~ /^[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}$/i) {
  64. print "Malformed MAC address $mac\n";
  65. return;
  66. }
  67. my $nbc = inet_aton($bc);
  68. # Remove colons
  69. $mac =~ tr/://d;
  70. # Magic packet is 6 bytes of FF followed by the MAC address 16 times
  71. $magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
  72. # Create socket
  73. socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die "socket: $!\n";
  74. # Enable broadcast
  75. setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt: $!\n";
  76. # Send the wakeup packet
  77. printf("$0: Sending wakeup packet to %04X:%08X-%s %s\n",$port,unpack('N',$nbc),uc($mac),$ip) if ($opt_d);
  78. defined(send(S, $magic, 0, sockaddr_in($port, $nbc)))
  79. or print "send: $!\n";
  80. close(S);
  81. }