File Coverage

blib/lib/Net/Wake.pm
Criterion Covered Total %
statement 9 23 39.1
branch 0 4 0.0
condition 0 5 0.0
subroutine 3 4 75.0
pod 0 1 0.0
total 12 37 32.4


line stmt bran cond sub pod time code
1             package Net::Wake;
2              
3 1     1   1069 use strict;
  1         3  
  1         35  
4 1     1   1382 use IO::Socket;
  1         29243  
  1         8  
5              
6 1     1   714 use vars qw($VERSION);
  1         7  
  1         432  
7             $VERSION = '0.02';
8              
9             =head1 NAME
10              
11             Net::Wake - A package to send packets to power on computers.
12              
13             =head1 SYNOPSIS
14              
15             To send a wake-on-lan packet via UDP:
16             Net::Wake::by_udp('255.255.255.255', '00:00:87:A0:8A:D2');
17              
18             Or directly from the command line:
19              
20             perl -MNet::Wake -e "Net::Wake::by_udp(undef,'00:00:87:A0:8A:D2')"
21              
22              
23             =head1 DESCRIPTION
24              
25             This package sends wake-on-lan (AKA magic) packets to turn on machines that
26             are wake-on-lan capable.
27              
28             For now there is only one function in this package:
29             Net::Wake::by_udp([$host], $mac_address, [$port]);
30              
31             You can omit the colons in the $mac_address, but not leading zeros.
32              
33             Generally speaking, you should use a broadcast address for $host.
34             Using the host's last known IP address is usually not sufficient
35             since the IP address may no longer be in the ARP cache.
36             A $host value of '255.255.255.255' is implied if $host is undef.
37             If you wish to send a magic packet to a remote subnet,
38             you can use a variation of '192.168.0.255', given that you know
39             the subnet mask to generate the proper broadcast address.
40              
41             =head1 SEE ALSO
42              
43             http://gsd.di.uminho.pt/jpo/software/wakeonlan/mini-howto/
44              
45             =head1 COPYRIGHT
46              
47             Copyright 1999-2003 Clinton Wong
48              
49             This program is free software; you can redistribute it
50             and/or modify it under the same terms as Perl itself.
51              
52             =cut
53              
54             sub by_udp {
55 0     0 0   my ($host, $mac_addr, $port) = @_;
56              
57             # use the discard service if $port not passed in
58 0 0         if (! defined $host) { $host = '255.255.255.255' }
  0            
59 0 0 0       if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 }
  0            
60              
61 0   0       my $sock = new IO::Socket::INET(Proto=>'udp') || return undef;
62              
63 0           my $ip_addr = inet_aton($host);
64 0           my $sock_addr = sockaddr_in($port, $ip_addr);
65 0           $mac_addr =~ s/://g;
66 0           my $packet = pack('C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16);
67              
68 0           setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1);
69 0           send($sock, $packet, 0, $sock_addr);
70 0           close ($sock);
71              
72 0           return 1;
73             }
74              
75             1;
76