line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Tools::Hostname; |
2
|
|
|
|
|
|
|
|
3
|
33
|
|
|
33
|
|
8480067
|
use strict; |
|
33
|
|
|
|
|
66
|
|
|
33
|
|
|
|
|
952
|
|
4
|
33
|
|
|
33
|
|
191
|
use warnings; |
|
33
|
|
|
|
|
70
|
|
|
33
|
|
|
|
|
977
|
|
5
|
33
|
|
|
33
|
|
176
|
use base 'Exporter'; |
|
33
|
|
|
|
|
180
|
|
|
33
|
|
|
|
|
2674
|
|
6
|
|
|
|
|
|
|
|
7
|
33
|
|
|
33
|
|
181
|
use UNIVERSAL::require(); |
|
33
|
|
|
|
|
72
|
|
|
33
|
|
|
|
|
616
|
|
8
|
33
|
|
|
33
|
|
1893
|
use Encode; |
|
33
|
|
|
|
|
14164
|
|
|
33
|
|
|
|
|
3095
|
|
9
|
33
|
|
|
33
|
|
169
|
use English qw(-no_match_vars); |
|
33
|
|
|
|
|
79
|
|
|
33
|
|
|
|
|
215
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw( |
12
|
|
|
|
|
|
|
getHostname |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
33
|
50
|
|
33
|
|
8594
|
if ($OSNAME eq 'MSWin32') { |
17
|
33
|
|
|
33
|
|
16899
|
no warnings 'redefine'; ## no critic (ProhibitNoWarnings) |
|
33
|
|
|
|
|
66
|
|
|
33
|
|
|
|
|
2634
|
|
18
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
*Win32::API::DESTROY = sub {}; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub getHostname { |
27
|
2
|
|
|
2
|
1
|
6
|
my (%params) = @_; |
28
|
|
|
|
|
|
|
|
29
|
2
|
50
|
|
|
|
18
|
my $hostname = $OSNAME eq 'MSWin32' ? |
30
|
|
|
|
|
|
|
_getHostnameWindows() : |
31
|
|
|
|
|
|
|
_getHostnameUnix() ; |
32
|
|
|
|
|
|
|
|
33
|
2
|
50
|
|
|
|
21
|
if ($params{short}) { |
34
|
2
|
|
|
|
|
5
|
$hostname =~ s/\..*$//; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
|
|
7
|
return $hostname; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _getHostnameUnix { |
41
|
2
|
|
|
2
|
|
20
|
Sys::Hostname->require(); |
42
|
2
|
|
|
|
|
1219
|
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__ |