File Coverage

lib/Rex/Hardware/Host.pm
Criterion Covered Total %
statement 70 180 38.8
branch 20 96 20.8
condition 7 42 16.6
subroutine 13 13 100.0
pod 0 3 0.0
total 110 334 32.9


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Hardware::Host;
6              
7 38     38   500 use v5.12.5;
  38         122  
8 38     38   243 use warnings;
  38         80  
  38         1856  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 38     38   270 use English qw(-no_match_vars);
  38         86  
  38         299  
13 38     38   14708 use Rex;
  38         89  
  38         352  
14 38     38   330 use Rex::Commands::Run;
  38         112  
  38         351  
15 38     38   239 use Rex::Helper::Run;
  38         99  
  38         2399  
16 38     38   269 use Rex::Commands::Fs;
  38         106  
  38         306  
17 38     38   311 use Rex::Commands::File;
  38         87  
  38         276  
18 38     38   339 use Rex::Logger;
  38         80  
  38         279  
19              
20 38     38   1432 use Rex::Inventory::Bios;
  38         106  
  38         404  
21              
22             require Rex::Hardware;
23              
24             sub get {
25              
26 58     58 0 398 my $cache = Rex::get_cache();
27 58         466 my $cache_key_name = $cache->gen_key_name("hardware.host");
28              
29 58 100       521 if ( $cache->valid($cache_key_name) ) {
30 7         25 return $cache->get($cache_key_name);
31             }
32              
33 51         755 my $bios = Rex::Inventory::Bios::get();
34              
35 51         1037 my $os = get_operating_system();
36              
37 51         544 my ( $domain, $hostname );
38 51 50 33     2283 if ( $os eq "Windows" ) {
    50 33        
    50          
    50          
39 0         0 my @env = i_run('set');
40 0         0 ($hostname) = map { /^COMPUTERNAME=(.*)$/ } @env;
  0         0  
41 0         0 ($domain) = map { /^USERDOMAIN=(.*)$/ } @env;
  0         0  
42             }
43             elsif ( $os eq "NetBSD" || $os eq "OpenBSD" || $os eq 'FreeBSD' ) {
44             ( $hostname, $domain ) =
45 0   0     0 split( /\./, ( eval { i_run("hostname") } || "unknown.nodomain" ), 2 );
46             }
47             elsif ( $os eq "SunOS" ) {
48             ($hostname) =
49 0   0     0 map { /^([^\.]+)$/ } ( eval { i_run("hostname"); } || "unknown" );
  0         0  
50 0   0     0 ($domain) = eval { i_run("domainname"); } || ("nodomain");
51             }
52             elsif ( $os eq "OpenWrt" ) {
53 0   0     0 ($hostname) = eval { i_run("uname -n"); } || ("unknown");
54             ($domain) =
55 0   0     0 eval { i_run("cat /proc/sys/kernel/domainname"); } || ("unknown");
56             }
57             else {
58 51         758 my @out = i_run "hostname -f 2>/dev/null", fail_ok => 1;
59              
60 51 50       631 if ( $? == 0 ) {
61 51         774 ( $hostname, $domain ) = split( /\./, $out[0], 2 );
62             }
63             else {
64 0         0 Rex::Logger::debug(
65             "Error getting hostname and domainname. There is something wrong with your /etc/hosts file."
66             );
67 0   0     0 ($hostname) = eval { i_run("hostname -s"); } || ("unknown");
68 0   0     0 ($domain) = eval { i_run("hostname -d"); } || ("nodomain");
69             }
70             }
71              
72 51         683 my $kernelname = q();
73              
74 51 50       1188 if ( can_run('uname') ) {
75 51         802 $kernelname = i_run 'uname -s';
76             }
77              
78 51         1686 my $operating_system_version = get_operating_system_version();
79              
80 51   50     1382 my $data = {
      50        
      50        
      50        
      50        
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         1075 $cache->set( $cache_key_name, $data );
94              
95 51         1670 return $data;
96              
97             }
98              
99             sub get_operating_system {
100              
101 234     234 0 1497 my $cache = Rex::get_cache();
102 234 100       1925 if ( $cache->valid("hardware.host") ) {
103 2         26 my $host_cache = $cache->get("hardware.host");
104 2 50       23 if ( exists $host_cache->{operatingsystem} ) {
105 2         25 return $host_cache->{operatingsystem};
106             }
107             }
108              
109             # use lsb_release if available
110 232         2707 my $is_lsb = can_run("lsb_release");
111              
112 232 100       2700 if ($is_lsb) {
113 4 50       15 if ( my $ret = i_run "lsb_release -s -i" ) {
114 0 0       0 if ( $ret =~ m/SUSE/i ) {
    0          
115 0         0 $ret = "SuSE";
116             }
117             elsif ( $ret eq "ManjaroLinux" ) {
118 0         0 $ret = "Manjaro";
119             }
120 0         0 return $ret;
121             }
122             }
123              
124 232 50       4426 if ( is_dir("c:/") ) {
125              
126             # windows
127 0         0 return "Windows";
128             }
129              
130 232 50       2129 if ( is_file("/etc/system-release") ) {
131 0         0 my $content = cat "/etc/system-release";
132 0 0       0 if ( $content =~ m/Amazon/sm ) {
133 0         0 return "Amazon";
134             }
135             }
136              
137 232 50       1276 if ( is_file("/etc/debian_version") ) {
138 232         5149 return "Debian";
139             }
140              
141 0 0 0     0 if ( is_file("/etc/SuSE-release") or is_file("/etc/SUSE-brand") ) {
142 0         0 return "SuSE";
143             }
144              
145 0 0       0 if ( is_file("/etc/mageia-release") ) {
146 0         0 return "Mageia";
147             }
148              
149 0 0       0 if ( is_file("/etc/fedora-release") ) {
150 0         0 return "Fedora";
151             }
152              
153 0 0       0 if ( is_file("/etc/gentoo-release") ) {
154 0         0 return "Gentoo";
155             }
156              
157 0 0       0 if ( is_file("/etc/altlinux-release") ) {
158 0         0 return "ALT";
159             }
160              
161 0 0       0 if ( is_file("/etc/redhat-release") ) {
162 0         0 my $fh = file_read("/etc/redhat-release");
163 0         0 my $content = $fh->read_all;
164 0         0 $fh->close;
165 0         0 chomp $content;
166              
167 0 0       0 if ( $content =~ m/CentOS/ ) {
    0          
168 0         0 return "CentOS";
169             }
170             elsif ( $content =~ m/Scientific/ ) {
171 0         0 return "Scientific";
172             }
173             else {
174 0         0 return "Redhat";
175             }
176             }
177              
178 0 0       0 if ( is_file("/etc/openwrt_release") ) {
179 0         0 return "OpenWrt";
180             }
181              
182 0 0       0 if ( is_file("/etc/arch-release") ) {
183 0         0 return "Arch";
184             }
185              
186 0 0       0 if ( is_file("/etc/manjaro-release") ) {
187 0         0 return "Manjaro";
188             }
189              
190 0         0 my $os_string = i_run("uname -s");
191 0         0 return $os_string; # return the plain os
192              
193             }
194              
195             sub get_operating_system_version {
196              
197 51     51 0 745 my $cache = Rex::get_cache();
198 51 50       1279 if ( $cache->valid("hardware.host") ) {
199 0         0 my $host_cache = $cache->get("hardware.host");
200 0 0       0 if ( exists $host_cache->{operatingsystemrelease} ) {
201 0         0 return $host_cache->{operatingsystemrelease};
202             }
203             }
204              
205 51         711 my $op = get_operating_system();
206              
207 51         737 my $is_lsb = can_run("lsb_release");
208              
209             # use lsb_release if available
210 51 50       1138 if ($is_lsb) {
211 0 0       0 if ( my $ret = i_run "lsb_release -r -s" ) {
212 0         0 my $os_check = i_run "lsb_release -d";
213 0 0       0 unless ( $os_check =~ m/SUSE\sLinux\sEnterprise/ ) {
214 0         0 return $ret;
215             }
216             }
217             }
218              
219 51 50 0     586 if ( $op eq "Debian" ) {
    0 0        
    0 0        
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
220              
221 51         1519 my $fh = file_read("/etc/debian_version");
222 51         621 my $content = $fh->read_all;
223 51         680 $fh->close;
224              
225 51         233 chomp $content;
226              
227 51         582 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"
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 0         if ( is_file("/etc/os-release") ) {
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 0         if ( $content =~ m/VERSION_ID/m ) {
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";
  0            
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 0         if ( $available_updates eq "" ) {
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 0         if ( $CHILD_ERROR == 0 ) {
339 0           return $version;
340             }
341             }
342              
343 0           return [ i_run("uname -r") ]->[0];
344              
345             }
346              
347             1;