X-Git-Url: http://lab.mitty.jp/git/?a=blobdiff_plain;f=bin%2Fwake;fp=bin%2Fwake;h=538e100763bc9f34d05bfe1c4a80657504d65d91;hb=46dc3f4565693891ba3efd219b49329229ec669a;hp=0000000000000000000000000000000000000000;hpb=99e75c7d84fb9c6ec898533945fde998d2a9e005;p=lab.git diff --git a/bin/wake b/bin/wake new file mode 100644 index 0000000..538e100 --- /dev/null +++ b/bin/wake @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +use Cwd qw( realpath ); +use File::Basename qw( fileparse ); + +@cfg = ('.wake', ( (fileparse( realpath($0) ))[1] ) . '.wake'); +$mac = {}; + +$x = '[0-9a-fA-F]'; +$macre = "$x$x:$x$x:$x$x:$x$x:$x$x:$x$x"; +$ipre = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"; + +foreach my $cfg (@cfg) { + open(CFG, $cfg); + while () { + next if /^\s*#/; + next if /^\s*$/; + + chomp; + my($host, $hw, $ip) = split; + push @{$mac->{lc($host)}}, { MAC => $hw, IP => $ip }; + } +} + +if (@ARGV) { + foreach $host (@ARGV) { + if ($host =~ /$macre/) { + print qx(wakeonlan $host); + } + elsif (@physical = @{$mac->{lc($host)}}) { + foreach my $mac (@physical) { + if ($mac->{IP}) { + print qx(wakeonlan -i $mac->{IP} $mac->{MAC}); + } + else { + print qx(wakeonlan $mac->{MAC}); + } + } + } + else { + print lc($host) . ": no such host in the list.\n"; + } + } +} +else { + foreach $host (sort keys %$mac) { + $physical = $mac->{lc($host)}; + foreach my $mac (@$physical) { + print "$host -> $mac->{MAC} -> $mac->{IP}\n"; + } + } +} +