| 1 | package Net::Wake; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | $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 | 0 | ▶ | sub by_udp { | ||||||||||||||||||||||||||
|
Subroutine
by_udp: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 0 | my ($host, $mac_addr, $port) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # use the discard service if $port not passed in | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (! defined $host) { $host = '255.255.255.255' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $port and $port =~ /^\d+$/
MC/DC0%0/2
defined $port and $port =~ /^\d+$/
defined $port
$port =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $portB= $port =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $sock = new IO::Socket::INET(Proto=>'udp') || return undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
'IO::Socket::INET'->new('Proto', 'udp') || (return undef)
MC/DC0%0/1
'IO::Socket::INET'->new('Proto', 'udp') || (return undef)
'IO::Socket::INET'->new('Proto', 'udp')
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Proto', 'udp')
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||