/usr/share/fusioninventory/lib/FusionInventory/Agent/Task/WakeOnLan.pm is in fusioninventory-agent 1:2.3.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | package FusionInventory::Agent::Task::WakeOnLan;
use strict;
use warnings;
use base 'FusionInventory::Agent::Task';
use English qw(-no_match_vars);
use List::Util qw(first);
use Socket;
use UNIVERSAL::require;
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Network;
our $VERSION = '2.0';
sub isEnabled {
my ($self, $response) = @_;
return unless
$self->{target}->isa('FusionInventory::Agent::Target::Server');
my $options = $self->getOptionsFromServer(
$response, 'WAKEONLAN', 'WakeOnLan'
);
return unless $options;
my @addresses;
foreach my $param (@{$options->{PARAM}}) {
my $address = $param->{MAC};
if ($address !~ /^$mac_address_pattern$/) {
$self->{logger}->error("invalid MAC address $address, skipping");
next;
}
$address =~ s/://g;
push @addresses, $address;
}
if (!@addresses) {
$self->{logger}->error("No mac address defined in the prolog response");
return;
}
$self->{addresses} = \@addresses;
return 1;
}
sub run {
my ($self, %params) = @_;
$self->{logger}->debug("FusionInventory WakeOnLan task $VERSION");
my @methods = $params{methods} ? @{$params{methods}} : qw/ethernet udp/;
METHODS: foreach my $method (@methods) {
my $function = '_send_magic_packet_' . $method;
ADDRESSES: foreach my $address (@{$self->{addresses}}) {
eval {
$self->$function($address);
};
if ($EVAL_ERROR) {
$self->{logger}->error(
"Impossible to use $method method: $EVAL_ERROR"
);
# this method doesn't work, skip remaining addresses
last ADDRESSES;
}
}
# all addresses have been processed, skip other methods
last METHODS;
}
}
sub _send_magic_packet_ethernet {
my ($self, $target) = @_;
die "root privileges needed\n" unless $UID == 0;
die "Net::Write module needed\n" unless Net::Write::Layer2->require();
my $interface = $self->_getInterface();
my $source = $interface->{MACADDR};
$source =~ s/://g;
my $packet =
pack('H12', $target) .
pack('H12', $source) .
pack('H4', "0842") .
$self->_getPayload($target);
$self->{logger}->debug(
"Sending magic packet to $target as ethernet frame"
);
my $writer = Net::Write::Layer2->new(
dev => $interface->{DESCRIPTION}
);
$writer->open();
$writer->send($packet);
$writer->close();
}
sub _send_magic_packet_udp {
my ($self, $target) = @_;
socket(my $socket, PF_INET, SOCK_DGRAM, getprotobyname('udp'))
or die "can't open socket: $ERRNO\n";
setsockopt($socket, SOL_SOCKET, SO_BROADCAST, 1)
or die "can't do setsockopt: $ERRNO\n";
my $packet = $self->_getPayload($target);
my $destination = sockaddr_in("9", inet_aton("255.255.255.255"));
$self->{logger}->debug(
"Sending magic packet to $target as UDP packet"
);
send($socket, $packet, 0, $destination)
or die "can't send packet: $ERRNO\n";
close($socket);
}
sub _getInterface {
my ($self) = @_;
my @interfaces;
SWITCH: {
if ($OSNAME eq 'linux') {
FusionInventory::Agent::Tools::Linux->require();
@interfaces = FusionInventory::Agent::Tools::Linux::getInterfacesFromIfconfig(
logger => $self->{logger}
);
last;
}
if ($OSNAME =~ /freebsd|openbsd|netbsd|gnukfreebsd|gnuknetbsd|dragonfly/) {
FusionInventory::Agent::Tools::BSD->require();
@interfaces = FusionInventory::Agent::Tools::BSD::getInterfacesFromIfconfig(
logger => $self->{logger}
);
last;
}
if ($OSNAME eq 'MSWin32') {
FusionInventory::Agent::Tools::Win32->require();
@interfaces = FusionInventory::Agent::Tools::Win32::getInterfaces(
logger => $self->{logger}
);
last;
}
}
# let's take the first interface with an IP adress, a MAC address
# different from the loopback
my $interface =
first { $_->{DESCRIPTION} ne 'lo' }
grep { $_->{IPADDRESS} }
grep { $_->{MACADDR} }
@interfaces;
# on Windows, we have to use internal device name instead of litteral name
$interface->{DESCRIPTION} =
$self->_getWin32InterfaceId($interface->{PNPDEVICEID})
if $OSNAME eq 'MSWin32';
return $interface;
}
sub _getPayload {
my ($self, $target) = @_;
return
pack('H12', 'FF' x 6) .
pack('H12', $target) x 16;
}
sub _getWin32InterfaceId {
my ($self, $pnpid) = @_;
FusionInventory::Agent::Tools::Win32->require();
my $key = FusionInventory::Agent::Tools::Win32::getRegistryKey(
path => "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Network",
);
foreach my $subkey_id (keys %$key) {
# we're only interested in GUID subkeys
next unless $subkey_id =~ /^\{\S+\}\/$/;
my $subkey = $key->{$subkey_id};
foreach my $subsubkey_id (keys %$subkey) {
my $subsubkey = $subkey->{$subsubkey_id};
next unless $subsubkey->{'Connection/'};
next unless $subsubkey->{'Connection/'}->{'/PnpInstanceID'};
next unless $subsubkey->{'Connection/'}->{'/PnpInstanceID'} eq $pnpid;
my $device_id = $subsubkey_id;
$device_id =~ s{/$}{};
return '\Device\NPF_' . $device_id;
}
}
}
1;
__END__
=head1 NAME
FusionInventory::Agent::Task::WakeOnLan - Wake-on-lan task for FusionInventory
=head1 DESCRIPTION
This task send a wake-on-lan packet to another host on the same network as the
agent host.
|