lib/Rex/Hardware/Host.pm

filter: Statementstmt 38.4% 73 / 190 Branchbran 20.8% 20 / 96 Conditioncond 17.7% 8 / 45 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 30.8% 114 / 370 CC 68 SCAR 55.0
CC
68
Cov
31%
get_operating_system_version
422.3
get_operating_system
266.1
get
139.9
CRAP 243.6
« lib/Rex/Helper/SSH2.pm ↑ Summary blib/lib/Rex/Commands/Rsync.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Hardware::Host;
6  
7 39 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 our $VERSION = '1.16.1'; # VERSION
11  
12 39 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use Rex;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 39 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 39 ▶use Rex::Inventory::Bios;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 39 require Rex::Hardware;
23  
24 58 ▶sub get {
Subroutine get: called CC=23 SCAR=49.4
Pod undocumented
25  
26 58 my $cache = Rex::get_cache();
27 58 my $cache_key_name = $cache->gen_key_name("hardware.host");
28  
29 58 ▶ if ( $cache->valid($cache_key_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid($cache_key_name)) 7 51 58
30 7 return $cache->get($cache_key_name);
31 }
32  
33 51 my $bios = Rex::Inventory::Bios::get();
34  
35 51 my $os = get_operating_system();
36  
37 51 my ( $domain, $hostname );
38 51 ▶ if ( $os eq "Windows" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($os eq 'Windows') { } 0 51 51
elsif ($os eq 'NetBSD' or $os eq 'OpenBSD' or $os eq 'FreeBSD') { } 0 51 51
elsif ($os eq 'SunOS') { } 0 51 51
elsif ($os eq 'OpenWrt') { } 0 51 51
39 0 my @env = i_run('set');
40 0 ($hostname) = map { /^COMPUTERNAME=(.*)$/ } @env;
41 0 ($domain) = map { /^USERDOMAIN=(.*)$/ } @env;
42 }
43 ▶ elsif ( $os eq "NetBSD" || $os eq "OpenBSD" || $os eq 'FreeBSD' ) {
Condition33%2/6
$os eq "NetBSD" or $os eq "OpenBSD" or $os eq "FreeBSD"
l!l&&r!l&&!r
0051
$os eq "NetBSD" or $os eq "OpenBSD"
l!l&&r!l&&!r
0051
MC/DC0%0/3
$os eq "NetBSD" or $os eq "OpenBSD" or $os eq "FreeBSD"
$os eq "NetBSD" $os eq "OpenBSD" $os eq "FreeBSD"
Truth table25%1/4
A or B or C
A= $os eq "NetBSD"B= $os eq "OpenBSD"C= $os eq "FreeBSD"
ABCresult
1XX1
01X1
0011
0000
44 ( $hostname, $domain ) =
45 0 ▶ split( /\./, ( eval { i_run("hostname") } || "unknown.nodomain" ), 2 );
Condition0%0/2
eval { do { i_run('hostname') } } || 'unknown.nodomain'
l!l
00
MC/DC0%0/1
eval { do { i_run('hostname') } } || 'unknown.nodomain'
eval { do { i_run('hostname') } }
Truth table0%0/2
A
A= eval { do { i_run('hostname') } }
Aresult
11
00
46 }
47 elsif ( $os eq "SunOS" ) {
48 ($hostname) =
49 0 ▶ map { /^([^\.]+)$/ } ( eval { i_run("hostname"); } || "unknown" );
Condition0%0/2
eval { do { i_run('hostname') } } || 'unknown'
l!l
00
MC/DC0%0/1
eval { do { i_run('hostname') } } || 'unknown'
eval { do { i_run('hostname') } }
Truth table0%0/2
A
A= eval { do { i_run('hostname') } }
Aresult
11
00
50 0 ▶ ($domain) = eval { i_run("domainname"); } || ("nodomain");
Condition0%0/2
eval { do { i_run('domainname') } } || 'nodomain'
l!l
00
MC/DC0%0/1
eval { do { i_run('domainname') } } || 'nodomain'
eval { do { i_run('domainname') } }
Truth table0%0/2
A
A= eval { do { i_run('domainname') } }
Aresult
11
00
51 }
52 elsif ( $os eq "OpenWrt" ) {
53 0 ▶ ($hostname) = eval { i_run("uname -n"); } || ("unknown");
Condition0%0/2
eval { do { i_run('uname -n') } } || 'unknown'
l!l
00
MC/DC0%0/1
eval { do { i_run('uname -n') } } || 'unknown'
eval { do { i_run('uname -n') } }
Truth table0%0/2
A
A= eval { do { i_run('uname -n') } }
Aresult
11
00
54 ($domain) =
55 0 ▶ eval { i_run("cat /proc/sys/kernel/domainname"); } || ("unknown");
Condition0%0/2
eval { do { i_run('cat /proc/sys/kernel/domainname') } } || 'unknown'
l!l
00
MC/DC0%0/1
eval { do { i_run('cat /proc/sys/kernel/domainname') } } || 'unknown'
eval { do { i_run('cat /proc/sys/kernel/domainname') } }
Truth table0%0/2
A
A= eval { do { i_run('cat /proc/sys/kernel/domainname') } }
Aresult
11
00
56 }
57 else {
58 51 my @out = i_run "hostname -f 2>/dev/null", fail_ok => 1;
59  
60 51 ▶ if ( $? == 0 && defined $out[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0 and defined $out[0]) { } 51 0 51
Condition33%1/3
$? == 0 and defined $out[0]
!ll&&!rl&&r
0051
MC/DC0%0/2
$? == 0 and defined $out[0]
$? == 0 defined $out[0]
Truth table33%1/3
A and B
A= $? == 0B= defined $out[0]
ABresult
0X0
100
111
61 51 ( $hostname, $domain ) = split( /\./, $out[0], 2 );
62 }
63 else {
64 0 Rex::Logger::debug(
65 "Error getting hostname and domainname. There is something wrong with your /etc/hosts file."
66 );
67 0 ▶ ($hostname) = eval { i_run("hostname -s"); } || ("unknown");
Condition0%0/2
eval { do { i_run('hostname -s') } } || 'unknown'
l!l
00
MC/DC0%0/1
eval { do { i_run('hostname -s') } } || 'unknown'
eval { do { i_run('hostname -s') } }
Truth table0%0/2
A
A= eval { do { i_run('hostname -s') } }
Aresult
11
00
68 0 ▶ ($domain) = eval { i_run("hostname -d"); } || ("nodomain");
Condition0%0/2
eval { do { i_run('hostname -d') } } || 'nodomain'
l!l
00
MC/DC0%0/1
eval { do { i_run('hostname -d') } } || 'nodomain'
eval { do { i_run('hostname -d') } }
Truth table0%0/2
A
A= eval { do { i_run('hostname -d') } }
Aresult
11
00
69 }
70 }
71  
72 51 my $kernelname = q();
73  
74 51 ▶ if ( can_run('uname') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run("uname")) 51 0 51
75 51 $kernelname = i_run 'uname -s';
76 }
77  
78 51 my $operating_system_version = get_operating_system_version();
79  
80 51 ▶ my $data = {
Condition50%5/10
$bios->get_system_information->get_manufacturer || ''
l!l
051
$hostname || ''
l!l
510
$domain || ''
l!l
051
$os || ''
l!l
510
$os || ''
l!l
510
MC/DC0%0/1
$bios->get_system_information->get_manufacturer || ''
$bios->get_system_information->get_manufacturer
MC/DC0%0/1
$hostname || ''
$hostname
MC/DC0%0/1
$domain || ''
$domain
MC/DC0%0/1
$os || ''
$os
MC/DC0%0/1
$os || ''
$os
Truth table50%1/2
A
A= $bios->get_system_information->get_manufacturer
Aresult
11
00
Truth table50%1/2
A
A= $hostname
Aresult
11
00
Truth table50%1/2
A
A= $domain
Aresult
11
00
Truth table50%1/2
A
A= $os
Aresult
11
00
Truth table50%1/2
A
A= $os
Aresult
11
00
81  
82 manufacturer => $bios->get_system_information()->get_manufacturer() || "",
83 hostname => $hostname || "",
84 domain => $domain || "",
85 operatingsystem => $os || "",
86 operating_system => $os || "",
87 operatingsystemrelease => $operating_system_version,
88 operating_system_release => $operating_system_version,
89 kernelname => $kernelname,
90  
91 };
92  
93 51 $cache->set( $cache_key_name, $data );
94  
95 51 return $data;
96  
97 }
98  
99 234 ▶sub get_operating_system {
Subroutine get_operating_system: called CC=23 SCAR=55.8
Pod undocumented
100  
101 234 my $cache = Rex::get_cache();
102 234 ▶ if ( $cache->valid("hardware.host") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid("hardware.host")) 2 232 234
103 2 my $host_cache = $cache->get("hardware.host");
104 2 ▶ if ( exists $host_cache->{operatingsystem} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $host_cache->{"operatingsystem"}) 2 0 2
105 2 return $host_cache->{operatingsystem};
106 }
107 }
108  
109 # use lsb_release if available
110 232 my $is_lsb = can_run("lsb_release");
111  
112 232 ▶ if ($is_lsb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_lsb) 4 228 232
113 4 ▶ if ( my $ret = i_run "lsb_release -s -i" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = i_run("lsb_release -s -i")) 0 4 4
114 0 ▶ if ( $ret =~ m/SUSE/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret =~ /SUSE/iu) { } 0 0 0
elsif ($ret eq 'ManjaroLinux') { } 0 0 0
115 0 $ret = "SuSE";
116 }
117 elsif ( $ret eq "ManjaroLinux" ) {
118 0 $ret = "Manjaro";
119 }
120 0 return $ret;
121 }
122 }
123  
124 232 ▶ if ( is_dir("c:/") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir("c:/")) 0 232 232
125  
126 # windows
127 0 return "Windows";
128 }
129  
130 232 ▶ if ( is_file("/etc/system-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/system-release")) 0 232 232
131 0 my $content = cat "/etc/system-release";
132 0 ▶ if ( $content =~ m/Amazon/sm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /Amazon/msu) 0 0 0
133 0 return "Amazon";
134 }
135 }
136  
137 232 ▶ if ( is_file("/etc/debian_version") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/debian_version")) 232 0 232
138 232 return "Debian";
139 }
140  
141 0 ▶ if ( is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand")) 0 0 0
Condition0%0/3
is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand")
l!l&&r!l&&!r
000
MC/DC0%0/2
is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand")
is_file("/etc/SuSE-release") is_file("/etc/SUSE-brand")
Truth table0%0/3
A or B
A= is_file("/etc/SuSE-release")B= is_file("/etc/SUSE-brand")
ABresult
1X1
011
000
142 0 return "SuSE";
143 }
144  
145 0 ▶ if ( is_file("/etc/mageia-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/mageia-release")) 0 0 0
146 0 return "Mageia";
147 }
148  
149 0 ▶ if ( is_file("/etc/fedora-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/fedora-release")) 0 0 0
150 0 return "Fedora";
151 }
152  
153 0 ▶ if ( is_file("/etc/gentoo-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/gentoo-release")) 0 0 0
154 0 return "Gentoo";
155 }
156  
157 0 ▶ if ( is_file("/etc/altlinux-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/altlinux-release")) 0 0 0
158 0 return "ALT";
159 }
160  
161 0 ▶ if ( is_file("/etc/redhat-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/redhat-release")) 0 0 0
162 0 my $fh = file_read("/etc/redhat-release");
163 0 my $content = $fh->read_all;
164 0 $fh->close;
165 0 chomp $content;
166  
167 0 ▶ if ( $content =~ m/CentOS/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /CentOS/u) { } 0 0 0
elsif ($content =~ /Scientific/u) { } 0 0 0
168 0 return "CentOS";
169 }
170 elsif ( $content =~ m/Scientific/ ) {
171 0 return "Scientific";
172 }
173 else {
174 0 return "Redhat";
175 }
176 }
177  
178 0 ▶ if ( is_file("/etc/openwrt_release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/openwrt_release")) 0 0 0
179 0 return "OpenWrt";
180 }
181  
182 0 ▶ if ( is_file("/etc/arch-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/arch-release")) 0 0 0
183 0 return "Arch";
184 }
185  
186 0 ▶ if ( is_file("/etc/manjaro-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file("/etc/manjaro-release")) 0 0 0
187 0 return "Manjaro";
188 }
189  
190 0 my $os_string = i_run("uname -s");
191 0 return $os_string; # return the plain os
192  
193 }
194  
195 51 ▶sub get_operating_system_version {
Subroutine get_operating_system_version: called CC=24 SCAR=60.5
Pod undocumented
196  
197 51 my $cache = Rex::get_cache();
198 51 ▶ if ( $cache->valid("hardware.host") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid("hardware.host")) 0 51 51
199 0 my $host_cache = $cache->get("hardware.host");
200 0 ▶ if ( exists $host_cache->{operatingsystemrelease} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $host_cache->{"operatingsystemrelease"}) 0 0 0
201 0 return $host_cache->{operatingsystemrelease};
202 }
203 }
204  
205 51 my $op = get_operating_system();
206  
207 51 my $is_lsb = can_run("lsb_release");
208  
209 # use lsb_release if available
210 51 ▶ if ($is_lsb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_lsb) 0 51 51
211 0 ▶ if ( my $ret = i_run "lsb_release -r -s" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = i_run("lsb_release -r -s")) 0 0 0
212 0 my $os_check = i_run "lsb_release -d";
213 0 ▶ unless ( $os_check =~ m/SUSE\sLinux\sEnterprise/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($os_check =~ /SUSE\sLinux\sEnterprise/u) 0 0 0
214 0 return $ret;
215 }
216 }
217 }
218  
219 51 ▶ if ( $op eq "Debian" ) {
Branch: 11 branches
BranchTrueFalseTotal
if ($op eq 'Debian') { } 51 0 51
elsif ($op eq 'Ubuntu') { } 0 0 0
elsif (lc $op eq 'redhat' or lc $op eq 'centos' or lc $op eq 'scientific' or lc $op eq 'fedora') { } 0 0 0
elsif ($op eq 'Mageia') { } 0 0 0
elsif ($op eq 'Gentoo') { } 0 0 0
elsif ($op eq 'SuSE') { } 0 0 0
elsif ($op eq 'ALT') { } 0 0 0
elsif ($op =~ /BSD/u) { } 0 0 0
elsif ($op eq 'OpenWrt') { } 0 0 0
elsif ($op eq 'Arch') { } 0 0 0
elsif ($op eq 'Windows') { } 0 0 0
220  
221 51 my $fh = file_read("/etc/debian_version");
222 51 my $content = $fh->read_all;
223 51 $fh->close;
224  
225 51 chomp $content;
226  
227 51 return $content;
228  
229 }
230 elsif ( $op eq "Ubuntu" ) {
231 0 my @l = i_run "lsb_release -r -s", fail_ok => 1;
232 0 return $l[0];
233 }
234 ▶ elsif ( lc($op) eq "redhat"
Condition0%0/9
lc $op eq "redhat" or lc $op eq "centos" or lc $op eq "scientific" or lc $op eq "fedora"
l!l&&r!l&&!r
000
lc $op eq "redhat" or lc $op eq "centos" or lc $op eq "scientific"
l!l&&r!l&&!r
000
lc $op eq "redhat" or lc $op eq "centos"
l!l&&r!l&&!r
000
MC/DC0%0/4
lc $op eq "redhat" or lc $op eq "centos" or lc $op eq "scientific" or lc $op eq "fedora"
lc $op eq "redhat" lc $op eq "centos" lc $op eq "scientific" lc $op eq "fedora"
Truth table0%0/5
A or B or C or D
A= lc $op eq "redhat"B= lc $op eq "centos"C= lc $op eq "scientific"D= lc $op eq "fedora"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
235 or lc($op) eq "centos"
236 or lc($op) eq "scientific"
237 or lc($op) eq "fedora" )
238 {
239  
240 0 my $fh = file_read("/etc/redhat-release");
241 0 my $content = $fh->read_all;
242 0 $fh->close;
243  
244 0 chomp $content;
245  
246 0 $content =~ m/(\d+(\.\d+)?)/;
247  
248 0 return $1;
249  
250 }
251 elsif ( $op eq "Mageia" ) {
252 0 my $fh = file_read("/etc/mageia-release");
253 0 my $content = $fh->read_all;
254 0 $fh->close;
255  
256 0 chomp $content;
257  
258 0 $content =~ m/(\d+)/;
259  
260 0 return $1;
261 }
262  
263 elsif ( $op eq "Gentoo" ) {
264 0 my $fh = file_read("/etc/gentoo-release");
265 0 my $content = $fh->read_all;
266 0 $fh->close;
267  
268 0 chomp $content;
269  
270 0 return [ split( /\s+/, $content ) ]->[-1];
271 }
272  
273 elsif ( $op eq "SuSE" ) {
274  
275 0 my ( $version, $release );
276  
277 0 my $release_file;
278 0 ▶ if ( is_file("/etc/os-release") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_file('/etc/os-release')) { } 0 0 0
279 0 $release_file = "/etc/os-release";
280 }
281 else {
282 0 $release_file = "/etc/SuSE-release";
283 }
284  
285 0 my $fh = file_read($release_file);
286 0 my $content = $fh->read_all;
287 0 $fh->close;
288  
289 0 chomp $content;
290  
291 0 ▶ if ( $content =~ m/VERSION_ID/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /VERSION_ID/mu) { } 0 0 0
292 0 ($version) = $content =~ m/VERSION_ID="(\d+(?:\.)?\d+)"/m;
293 }
294 else {
295 0 ($version) = $content =~ m/VERSION = (\d+\.\d+)/m;
296 }
297  
298 0 return $version;
299  
300 }
301 elsif ( $op eq "ALT" ) {
302 0 my $fh = file_read("/etc/altlinux-release");
303 0 my $content = $fh->read_all;
304 0 $fh->close;
305  
306 0 chomp $content;
307  
308 0 $content =~ m/(\d+(\.\d+)*)/;
309  
310 0 return $1;
311  
312 }
313 elsif ( $op =~ /BSD/ ) {
314 0 my ($version) = map { /(\d+\.\d+)/ } i_run "uname -r";
315 0 return $version;
316 }
317 elsif ( $op eq "OpenWrt" ) {
318 0 my $fh = file_read("/etc/openwrt_version");
319 0 my $content = $fh->read_all;
320 0 $fh->close;
321  
322 0 chomp $content;
323  
324 0 return $content;
325 }
326 elsif ( $op eq "Arch" ) {
327 0 my $available_updates = i_run "checkupdates", fail_ok => 1;
328 0 ▶ if ( $available_updates eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($available_updates eq '') { } 0 0 0
329 0 return "latest";
330 }
331 else {
332 0 return "outdated";
333 }
334 }
335 elsif ( $op eq 'Windows' ) {
336 0 my $version = i_run 'ver', fail_ok => 1;
337  
338 0 ▶ if ( $CHILD_ERROR == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Hardware::Host::CHILD_ERROR == 0) 0 0 0
339 0 return $version;
340 }
341 }
342  
343 0 return [ i_run("uname -r") ]->[0];
344  
345 }
346  
347 39 1;
« lib/Rex/Helper/SSH2.pm ↑ Summary blib/lib/Rex/Commands/Rsync.pm »