line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Rex::Commands::MD5 - Calculate MD5 sum of files |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
With this module you calculate the md5 sum of a file. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
This is just a helper function and will not be reported. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Rex::Commands::MD5; |
18
|
|
|
|
|
|
|
my $md5 = md5($file); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
package Rex::Commands::MD5; |
25
|
|
|
|
|
|
|
|
26
|
49
|
|
|
49
|
|
93815
|
use v5.12.5; |
|
49
|
|
|
|
|
222
|
|
27
|
49
|
|
|
49
|
|
275
|
use warnings; |
|
49
|
|
|
|
|
102
|
|
|
49
|
|
|
|
|
2412
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
30
|
|
|
|
|
|
|
|
31
|
49
|
|
|
49
|
|
777
|
use Rex::Logger; |
|
49
|
|
|
|
|
110
|
|
|
49
|
|
|
|
|
1567
|
|
32
|
|
|
|
|
|
|
require Rex::Commands; |
33
|
|
|
|
|
|
|
require Rex::Commands::Run; |
34
|
49
|
|
|
49
|
|
2225
|
use Rex::Interface::Exec; |
|
49
|
|
|
|
|
111
|
|
|
49
|
|
|
|
|
337
|
|
35
|
49
|
|
|
49
|
|
1392
|
use Rex::Interface::File; |
|
49
|
|
|
|
|
114
|
|
|
49
|
|
|
|
|
409
|
|
36
|
49
|
|
|
49
|
|
1314
|
use Rex::Interface::Fs; |
|
49
|
|
|
|
|
119
|
|
|
49
|
|
|
|
|
260
|
|
37
|
49
|
|
|
49
|
|
1369
|
use Rex::Helper::Path; |
|
49
|
|
|
|
|
108
|
|
|
49
|
|
|
|
|
3234
|
|
38
|
49
|
|
|
49
|
|
360
|
use Rex::Helper::Run; |
|
49
|
|
|
|
|
117
|
|
|
49
|
|
|
|
|
3772
|
|
39
|
49
|
|
|
49
|
|
349
|
use English qw(-no_match_vars); |
|
49
|
|
|
|
|
110
|
|
|
49
|
|
|
|
|
535
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
require Rex::Exporter; |
42
|
49
|
|
|
49
|
|
20444
|
use base qw(Rex::Exporter); |
|
49
|
|
|
|
|
118
|
|
|
49
|
|
|
|
|
3682
|
|
43
|
49
|
|
|
49
|
|
334
|
use vars qw(@EXPORT); |
|
49
|
|
|
|
|
224
|
|
|
49
|
|
|
|
|
28168
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
@EXPORT = qw(md5); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 md5($file) |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
This function will return the MD5 checksum (hexadecimal) for the given file. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
task "checksum", "server01", sub { |
52
|
|
|
|
|
|
|
say md5("/etc/passwd"); |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub md5 { |
58
|
127
|
|
|
127
|
1
|
2132
|
my ($file) = @_; |
59
|
|
|
|
|
|
|
|
60
|
127
|
|
|
|
|
3227
|
my $fs = Rex::Interface::Fs->create; |
61
|
|
|
|
|
|
|
|
62
|
127
|
100
|
|
|
|
1165
|
if ( $fs->is_file($file) ) { |
63
|
113
|
|
|
|
|
1500
|
Rex::Logger::debug("Calculating checksum (MD5) of $file"); |
64
|
|
|
|
|
|
|
|
65
|
113
|
|
33
|
|
|
951
|
my $md5 = _digest_md5($file) // _binary_md5($file); |
66
|
|
|
|
|
|
|
|
67
|
113
|
50
|
|
|
|
711
|
if ( !$md5 ) { |
68
|
0
|
|
|
|
|
0
|
my $message = "Unable to get MD5 checksum of $file: $!"; |
69
|
0
|
|
|
|
|
0
|
Rex::Logger::info($message); |
70
|
0
|
|
|
|
|
0
|
die($message); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
113
|
|
|
|
|
1528
|
Rex::Logger::debug("MD5 checksum of $file: $md5"); |
74
|
|
|
|
|
|
|
|
75
|
113
|
|
|
|
|
3718
|
return $md5; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
14
|
|
|
|
|
140
|
my $message = "File not found: $file"; |
79
|
14
|
|
|
|
|
124
|
Rex::Logger::debug($message); |
80
|
14
|
|
|
|
|
289
|
die($message); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _digest_md5 { |
85
|
114
|
|
|
114
|
|
369
|
my $file = shift; |
86
|
114
|
|
|
|
|
264
|
my $md5; |
87
|
|
|
|
|
|
|
|
88
|
114
|
50
|
|
|
|
559
|
my $perl = Rex::is_local() ? $EXECUTABLE_NAME : 'perl'; |
89
|
|
|
|
|
|
|
|
90
|
114
|
50
|
33
|
|
|
2809
|
my $command = |
91
|
|
|
|
|
|
|
( $^O =~ m/^MSWin/i && Rex::is_local() ) |
92
|
|
|
|
|
|
|
? qq("$perl" -MDigest::MD5 -e "open my \$fh, '<', \$ARGV[0] or die 'Cannot open ' . \$ARGV[0]; binmode \$fh; print Digest::MD5->new->addfile(\$fh)->hexdigest;" "$file") |
93
|
|
|
|
|
|
|
: qq('$perl' -MDigest::MD5 -e 'open my \$fh, "<", \$ARGV[0] or die "Cannot open " . \$ARGV[0]; binmode \$fh; print Digest::MD5->new->addfile(\$fh)->hexdigest;' '$file'); |
94
|
|
|
|
|
|
|
|
95
|
114
|
|
|
|
|
1209
|
my $result = i_run( $command, fail_ok => 1 ); |
96
|
|
|
|
|
|
|
|
97
|
114
|
50
|
|
|
|
1238
|
if ( $? == 0 ) { |
98
|
114
|
|
|
|
|
350
|
$md5 = $result; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
114
|
|
|
|
|
2289
|
return $md5; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _binary_md5 { |
105
|
1
|
|
|
1
|
|
15
|
my $file = shift; |
106
|
1
|
|
|
|
|
7
|
my $md5; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
35
|
my $exec = Rex::Interface::Exec->create; |
109
|
|
|
|
|
|
|
|
110
|
1
|
50
|
|
|
|
25
|
if ( Rex::Commands::Run::can_run('md5') ) { |
|
|
50
|
|
|
|
|
|
111
|
0
|
|
|
|
|
0
|
($md5) = $exec->exec("md5 '$file'") =~ qr{\s+=\s+([a-f0-9]{32})\s*$}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
elsif ( Rex::Commands::Run::can_run('md5sum') ) { |
114
|
1
|
|
|
|
|
21
|
($md5) = $exec->exec("md5sum '$file'") =~ qr{^\\?([a-f0-9]{32})\s+}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
50
|
return $md5; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |