From 9d6acf2a16d4213b3fc980544f0219b875061ef5 Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 11:25:24 +0800 Subject: [PATCH 1/6] Add DDNS support. --- wakeonlan | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/wakeonlan b/wakeonlan index 5003a24..829431b 100755 --- a/wakeonlan +++ b/wakeonlan @@ -38,6 +38,7 @@ my $DEFAULT_PORT = getservbyname('discard', 'udp'); my $verbose = 1; my $dryrun = 0; +my $ddns = ''; my $filename = ''; my @queue = (); @@ -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; @@ -221,6 +235,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); @@ -245,6 +260,11 @@ if ($filename and ! -f $filename) { exit(4); } +if ($ddns and ! isValidDDNS($ddns)) { + warn "Invalid host address: $ddns\n"; + exit(5); +} + # # Nothing to do ? # @@ -288,7 +308,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 @@ -313,6 +333,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). From 8b0bcc6440014e17f5025ec027b90bed5e2c5c30 Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 11:30:21 +0800 Subject: [PATCH 2/6] update virsion --- wakeonlan | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wakeonlan b/wakeonlan index 829431b..7542c99 100755 --- a/wakeonlan +++ b/wakeonlan @@ -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 2005/03/21 11:29:54 jpo Exp $ # ###################################################################### @@ -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, From 8444a071f5897e39d8323403bab7f9b9a967926b Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 13:55:37 +0800 Subject: [PATCH 3/6] DDNS read file support. --- wakeonlan | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wakeonlan b/wakeonlan index 7542c99..ed26cf9 100755 --- a/wakeonlan +++ b/wakeonlan @@ -114,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>) { @@ -124,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"; @@ -146,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 ]; } From 856ed0fba57e294213656ef0a16c1a4dc512a2a2 Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 14:04:26 +0800 Subject: [PATCH 4/6] update usage --- wakeonlan | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wakeonlan b/wakeonlan index ed26cf9..71a9410 100755 --- a/wakeonlan +++ b/wakeonlan @@ -1,6 +1,6 @@ #!/usr/bin/perl -w # -# $Id: wakeonlan,v 1.4.2.5 2005/03/21 11:29:54 jpo Exp $ +# $Id: wakeonlan,v 1.4.2.5 2015/03/21 14:04:34 jpo Exp $ # ###################################################################### @@ -382,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 From 41d6dced1e453593958993d6dbcddd500ce1d93e Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 14:14:28 +0800 Subject: [PATCH 5/6] fixed --- wakeonlan | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wakeonlan b/wakeonlan index 71a9410..5002ef1 100755 --- a/wakeonlan +++ b/wakeonlan @@ -146,7 +146,7 @@ sub loadFromFile { next; } - if defined($ddns){ + if (defined($ddns)){ my $t = $DEFAULT_IP; if (! isValidDDNS($ddns)){ warn "Invalid DDNS Domain: $ddns\n";; From b666947614ced6eae02d25e1aa79a669d8d47e68 Mon Sep 17 00:00:00 2001 From: gshmu Date: Sat, 21 Mar 2015 14:27:54 +0800 Subject: [PATCH 6/6] update lab001.wol --- examples/lab001.wol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/lab001.wol b/examples/lab001.wol index cbbcca3..ef16c6f 100644 --- a/examples/lab001.wol +++ b/examples/lab001.wol @@ -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