Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/lab001.wol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
# - comment lines are ignored (lines starting with a hash mark '#')
# - other lines are considered valid records and can have 3 columns:
#
# Hardware address, IP address, destination port
# Hardware address, IP address, destination port, DDNS domain
#
# the last two are optional, in which case the following defaults
# the last three are optional, in which case the following defaults
# are used:
#
# IP address: 255.255.255.255 (the limited broadcast address)
# port: 9 (the discard port)
# DDNS domain: '' (this will replace IP address)
#

07:08:09:0A:0B:0C 255.255.255.255 9 amazon.com
01:02:03:04:05:06 192.168.1.255 9
07:09:09:0A:0B:0C 255.255.255.255
0D:0E:0F:00:10:11
Expand Down
50 changes: 45 additions & 5 deletions wakeonlan
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w
#
# $Id: wakeonlan,v 1.4.2.3 2005/01/27 16:03:54 jpo Exp $
# $Id: wakeonlan,v 1.4.2.5 2015/03/21 14:04:34 jpo Exp $
#
######################################################################

Expand All @@ -9,7 +9,7 @@ use Socket;
use Getopt::Long;
use Pod::Usage;

our $VERSION = '0.41_90';
our $VERSION = '0.41_92';

use constant {
PORT_MIN => 0,
Expand Down Expand Up @@ -38,6 +38,7 @@ my $DEFAULT_PORT = getservbyname('discard', 'udp');

my $verbose = 1;
my $dryrun = 0;
my $ddns = '';
my $filename = '';

my @queue = ();
Expand Down Expand Up @@ -69,6 +70,19 @@ sub isValidIPAddress {
}


sub isValidDDNS {
my $hostaddr = shift;

return 0 if ! ($hostaddr =~ m/^(https?:\/\/)?\w+(\.\w+)+\/?$/i);

my $t = gethostbyname($hostaddr);
return 0 if ! isValidIPAddress(inet_ntoa($t));
$DEFAULT_IP = inet_ntoa($t);

return 1;
}


sub isValidHardwareAddress {
my $hwaddr = shift;

Expand Down Expand Up @@ -100,7 +114,7 @@ sub loadFromCommandLine {

sub loadFromFile {
my $filename = shift;
my ($hwaddr, $ipaddr, $port);
my ($hwaddr, $ipaddr, $port, $ddns);

open (my $FILE, '<', $filename) or die "open : $!";
while(<$FILE>) {
Expand All @@ -110,7 +124,7 @@ sub loadFromFile {
$stats{total}++;

chomp;
($hwaddr, $ipaddr, $port) = split;
($hwaddr, $ipaddr, $port, $ddns) = split;

if (! isValidHardwareAddress($hwaddr) ) {
warn "Invalid hardware address: $hwaddr\n";
Expand All @@ -132,6 +146,17 @@ sub loadFromFile {
next;
}

if (defined($ddns)){
my $t = $DEFAULT_IP;
if (! isValidDDNS($ddns)){
warn "Invalid DDNS Domain: $ddns\n";;
$stats{invalid}++;
next;
}
$ipaddr = $DEFAULT_IP;
$DEFAULT_IP = $t;
}

$stats{valid}++;
push @queue, [ $hwaddr, $ipaddr, $port ];
}
Expand Down Expand Up @@ -221,6 +246,7 @@ GetOptions(
"q|quiet" => sub { $verbose = 0; },
"i|ip=s" => \$DEFAULT_IP,
"p|port=i" => \$DEFAULT_PORT,
"d|ddns=s" => \$ddns,
"f|file=s" => \$filename,
"n|dry-run" => sub { $dryrun = 1; },
) or pod2usage( -exitval => 1, -verbose => 1);
Expand All @@ -245,6 +271,11 @@ if ($filename and ! -f $filename) {
exit(4);
}

if ($ddns and ! isValidDDNS($ddns)) {
warn "Invalid host address: $ddns\n";
exit(5);
}

#
# Nothing to do ?
#
Expand Down Expand Up @@ -288,7 +319,7 @@ wakeonlan - Perl script to wake up computers

=head1 SYNOPSIS

wakeonlan [-h|--help] [-v|--version] [-q|--quiet] [-n|--dry-run] [-i|--ip IP_address] [-p|--port port] [-f|--file file_name] [[hardware_address] ...]
wakeonlan [-h|--help] [-v|--version] [-q|--quiet] [-n|--dry-run] [-i|--ip IP_address] [-d|--ddns host] [-p|--port port] [-f|--file file_name] [[hardware_address] ...]

=head1 DESCRIPTION

Expand All @@ -313,6 +344,10 @@ Displays the script version.
Destination IP address. Unless you have static ARP tables you should
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).

=item B<-d, --ddns=host>

Destination host. Unless you have DDNS Domain. Default: '' (This will change the IP Address).

=item B<-p, --port=port>

Destination port. Default: 9 (the discard port).
Expand Down Expand Up @@ -347,6 +382,11 @@ Using another destination port:

$ wakeonlan -i 192.168.1.255 -p 1234 01:02:03:04:05:06

Using another DDNS:

$ wakeonlan -d www.google.com 01:02:03:04:05:06
$ wakeonlan -i 192.168.1.255 -d www.google.com 01:02:03:04:05:06 # -i won't work!

Using a file as source of hardware and IP addresses:

$ wakeonlan -f examples/lab001.wol
Expand Down