[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-wpkg/sources/scripts/ -> wakeonlan (source)

   1  #!/usr/bin/perl -w
   2  #
   3  # $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
   4  #
   5  #########################################################################       
   6  
   7  use strict;
   8  use Socket;
   9  use Getopt::Std;
  10  use vars qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f);
  11  $VERSION = '0.41';
  12  
  13  my $DEFAULT_IP      = '255.255.255.255';
  14  my $DEFAULT_PORT    = getservbyname('discard', 'udp');
  15  
  16  #
  17  # Process the command line
  18  #
  19  
  20  getopts("hvp:i:f:");
  21  
  22  if ($opt_h) { usage(); exit(0); }
  23  if ($opt_v) { print "wakeonlan version $VERSION\n"; exit(0); }
  24  if (!$opt_f and !@ARGV) { usage(); exit(0); }
  25  if ($opt_i) { $DEFAULT_IP = $opt_i; }        # override default value
  26  if ($opt_p) { $DEFAULT_PORT = $opt_p; }        # override default value
  27  
  28  if ($opt_f) { process_file($opt_f); }
  29  
  30  # The rest of the command line is a list of hardware addresses 
  31  
  32  foreach (@ARGV) {
  33      wake($_, $opt_i, $opt_p);
  34  } 
  35  
  36  #
  37  # wake
  38  #
  39  # The 'magic packet' consists of 6 times 0xFF followed by 16 times
  40  # the hardware address of the NIC. This sequence can be encapsulated
  41  # in any kind of packet, in this case an UDP packet targeted at the
  42  # discard port (9).
  43  #                                                                               
  44  
  45  sub wake
  46  {
  47      my $hwaddr  = shift;
  48      my $ipaddr  = shift || $DEFAULT_IP;
  49      my $port    = shift || $DEFAULT_PORT;
  50  
  51      my ($raddr, $them, $proto);
  52      my ($hwaddr_re, $pkt);
  53      
  54      # Validate hardware address (ethernet address)
  55  
  56      $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
  57      if ($hwaddr !~ m/^$hwaddr_re$/) {
  58          warn "Invalid hardware address: $hwaddr\n";
  59          return undef;
  60      }
  61  
  62      # Generate magic sequence
  63  
  64      foreach (split /:/, $hwaddr) {
  65          $pkt .= chr(hex($_));
  66      }
  67      $pkt = chr(0xFF) x 6 . $pkt x 16;
  68  
  69      # Allocate socket and send packet
  70  
  71      $raddr = gethostbyname($ipaddr);
  72      $them = pack_sockaddr_in($port, $raddr);
  73      $proto = getprotobyname('udp');
  74  
  75      socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
  76      setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
  77  
  78      print "Sending magic packet to $ipaddr:$port with $hwaddr\n";
  79  
  80      send(S, $pkt, 0, $them) or die "send : $!";
  81      close S;
  82  }
  83  
  84  #
  85  # process_file
  86  #
  87  
  88  sub process_file {
  89      my $filename = shift;
  90      my ($hwaddr, $ipaddr, $port);
  91  
  92      open (F, "<$filename") or die "open : $!";
  93      while(<F>) {
  94          next if /^\s*#/;        # ignore comments
  95          next if /^\s*$/;        # ignore empty lines
  96  
  97          chomp;
  98          ($hwaddr, $ipaddr, $port) = split;
  99  
 100          wake($hwaddr, $ipaddr, $port);
 101      }
 102      close F;
 103  }
 104  
 105  
 106  #
 107  # Usage
 108  #
 109  
 110  sub usage {
 111  print <<__USAGE__;
 112  Usage
 113      wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
 114  
 115  Options
 116      -h
 117          this information
 118      -v
 119          displays the script version
 120      -i ip_address
 121          set the destination IP address
 122          default: 255.255.255.255 (the limited broadcast address)
 123      -p port
 124          set the destination port
 125          default: 9 (the discard port)
 126      -f file 
 127          uses file as a source of hardware addresses
 128  
 129  See also
 130      wakeonlan(1)    
 131  
 132  __USAGE__
 133  }
 134  
 135  
 136  __END__
 137  
 138  # Script documentation
 139  
 140  =head1 NAME
 141  
 142  wakeonlan - Perl script to wake up computers
 143  
 144  =head1 SYNOPSIS
 145  
 146  wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware_address] ...]
 147  
 148  =head1 DESCRIPTION
 149  
 150  This script sends 'magic packets' to wake-on-lan enabled ethernet adapters and motherboards, in order to switch on the called PC. Be sure to connect the NIC with the motherboard if neccesary, and enable the WOL function in the BIOS.
 151  
 152  The 'magic packet' consists of 6 times 0xFF followed by 16 times the hardware address of the NIC. This sequence can be encapsulated in any kind of packet. This script uses UDP packets.
 153  
 154  =head1 OPTIONS
 155  
 156  =over
 157  
 158  =item B<-h>
 159  
 160  Displays the help information.
 161  
 162  =item B<-v>
 163  
 164  Displays the script version.
 165  
 166  =item B<-i ip_address>
 167  
 168  Destination IP address. Unless you have static ARP tables you should
 169  use some kind of broadcast address (the broadcast address of the network where the computer resides or the limited broadcast address). Default: 255.255.255.255 (the limited broadcast address).
 170  
 171  =item B<-p port>
 172  
 173  Destination port. Default: 9 (the discard port).
 174  
 175  =item B<-f file>
 176  
 177  File with hardware addresses of wakeable computers. For an example check
 178  the file lab001.wol in the examples subdirectory.
 179  
 180  =back
 181  
 182  =head1 EXAMPLES
 183  
 184  Using the limited broadcast address (255.255.255.255):
 185  
 186      $ wakeonlan 01:02:03:04:05:06
 187      $ wakeonlan 01:02:03:04:05:06 01:02:03:04:05:07
 188  
 189  Using a subnet broadcast address:
 190  
 191      $ wakeonlan -i 192.168.1.255 01:02:03:04:05:06
 192  
 193  Using another destination port:
 194  
 195      $ wakeonlan -i 192.168.1.255 -p 1234 01:02:03:04:05:06
 196  
 197  Using a file as source of hardware and IP addresses:
 198  
 199      $ wakeonlan -f examples/lab001.wol
 200      $ wakeonlan -f examples/lab001.wol 01:02:03:04:05:06
 201  
 202  =head1 AUTHOR
 203  
 204  José Pedro Oliveira <jpo@di.uminho.pt> maintaining and expanding original work done by Ico Doornekamp <ico@edd.dhs.org>.
 205  
 206  =head1 COPYRIGHT
 207  
 208  Copyright (c) 2000-2005 José Pedro Oliveira.
 209  
 210  This is free software.  You may modify it and distribute it under Perl's Artistic Licence.  Modified versions must be clearly indicated.                                                    
 211  
 212  =head1 SEE ALSO
 213  
 214  For more information regarding this script and Wakeonlan technology just check the following address http://gsd.di.uminho.pt/jpo/software/wakeonlan/.
 215  
 216  =cut


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1