line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Tools::Hostname; |
2
|
|
|
|
|
|
|
|
3
|
23
|
|
|
23
|
|
6951604
|
use strict; |
|
23
|
|
|
|
|
47
|
|
|
23
|
|
|
|
|
800
|
|
4
|
23
|
|
|
23
|
|
96
|
use warnings; |
|
23
|
|
|
|
|
30
|
|
|
23
|
|
|
|
|
644
|
|
5
|
23
|
|
|
23
|
|
103
|
use base 'Exporter'; |
|
23
|
|
|
|
|
69
|
|
|
23
|
|
|
|
|
1657
|
|
6
|
|
|
|
|
|
|
|
7
|
23
|
|
|
23
|
|
122
|
use UNIVERSAL::require(); |
|
23
|
|
|
|
|
27
|
|
|
23
|
|
|
|
|
401
|
|
8
|
23
|
|
|
23
|
|
662
|
use Encode; |
|
23
|
|
|
|
|
12362
|
|
|
23
|
|
|
|
|
1942
|
|
9
|
23
|
|
|
23
|
|
116
|
use English qw(-no_match_vars); |
|
23
|
|
|
|
|
25
|
|
|
23
|
|
|
|
|
134
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw( |
12
|
|
|
|
|
|
|
getHostname |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
23
|
50
|
|
23
|
|
5410
|
if ($OSNAME eq 'MSWin32') { |
17
|
23
|
|
|
23
|
|
8933
|
no warnings 'redefine'; ## no critic (ProhibitNoWarnings) |
|
23
|
|
|
|
|
38
|
|
|
23
|
|
|
|
|
1714
|
|
18
|
0
|
|
|
|
|
|
Win32::API->require(); |
19
|
|
|
|
|
|
|
# Kernel32.dll is used more or less everywhere. |
20
|
|
|
|
|
|
|
# Without this, Win32::API will release the DLL even |
21
|
|
|
|
|
|
|
# if it's a very bad idea |
22
|
0
|
|
|
|
|
|
*Win32::API::DESTROY = sub {}; |
|
0
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub getHostname { |
27
|
0
|
|
|
0
|
1
|
|
my (%params) = @_; |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
my $hostname = $OSNAME eq 'MSWin32' ? |
30
|
|
|
|
|
|
|
_getHostnameWindows() : |
31
|
|
|
|
|
|
|
_getHostnameUnix() ; |
32
|
|
|
|
|
|
|
|
33
|
0
|
0
|
|
|
|
|
if ($params{short}) { |
34
|
0
|
|
|
|
|
|
$hostname =~ s/\..*$//; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
return $hostname; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _getHostnameUnix { |
41
|
0
|
|
|
0
|
|
|
Sys::Hostname->require(); |
42
|
0
|
|
|
|
|
|
return Sys::Hostname::hostname(); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _getHostnameWindows { |
46
|
0
|
|
|
0
|
|
|
my $getComputerName = Win32::API->new( |
47
|
|
|
|
|
|
|
"kernel32", "GetComputerNameExW", ["I", "P", "P"], "N" |
48
|
|
|
|
|
|
|
); |
49
|
0
|
|
|
|
|
|
my $buffer = "\x00" x 1024; |
50
|
0
|
|
|
|
|
|
my $n = 1024; #pack ("c4", 160,0,0,0); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$getComputerName->Call(3, $buffer, $n); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# convert from UTF16 to UTF8 |
55
|
0
|
|
|
|
|
|
my $hostname = substr(decode("UCS-2le", $buffer), 0, ord $n); |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
0
|
|
|
|
return $hostname || $ENV{COMPUTERNAME}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |