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
|
78
|
|
|
78
|
|
94511
|
use v5.12.5; |
|
78
|
|
|
|
|
312
|
|
27
|
78
|
|
|
78
|
|
448
|
use warnings; |
|
78
|
|
|
|
|
219
|
|
|
78
|
|
|
|
|
3726
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '1.14.2.3'; # TRIAL VERSION |
30
|
|
|
|
|
|
|
|
31
|
78
|
|
|
78
|
|
950
|
use Rex::Logger; |
|
78
|
|
|
|
|
1301
|
|
|
78
|
|
|
|
|
618
|
|
32
|
|
|
|
|
|
|
require Rex::Commands; |
33
|
|
|
|
|
|
|
require Rex::Commands::Run; |
34
|
78
|
|
|
78
|
|
3575
|
use Rex::Interface::Exec; |
|
78
|
|
|
|
|
199
|
|
|
78
|
|
|
|
|
1074
|
|
35
|
78
|
|
|
78
|
|
2127
|
use Rex::Interface::File; |
|
78
|
|
|
|
|
190
|
|
|
78
|
|
|
|
|
510
|
|
36
|
78
|
|
|
78
|
|
2104
|
use Rex::Interface::Fs; |
|
78
|
|
|
|
|
192
|
|
|
78
|
|
|
|
|
494
|
|
37
|
78
|
|
|
78
|
|
2509
|
use Rex::Helper::Path; |
|
78
|
|
|
|
|
229
|
|
|
78
|
|
|
|
|
5156
|
|
38
|
78
|
|
|
78
|
|
589
|
use Rex::Helper::Run; |
|
78
|
|
|
|
|
203
|
|
|
78
|
|
|
|
|
5775
|
|
39
|
78
|
|
|
78
|
|
549
|
use English qw(-no_match_vars); |
|
78
|
|
|
|
|
194
|
|
|
78
|
|
|
|
|
781
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
require Rex::Exporter; |
42
|
78
|
|
|
78
|
|
30786
|
use base qw(Rex::Exporter); |
|
78
|
|
|
|
|
195
|
|
|
78
|
|
|
|
|
6408
|
|
43
|
78
|
|
|
78
|
|
588
|
use vars qw(@EXPORT); |
|
78
|
|
|
|
|
293
|
|
|
78
|
|
|
|
|
46536
|
|
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
|
1931
|
my ($file) = @_; |
59
|
|
|
|
|
|
|
|
60
|
127
|
|
|
|
|
3106
|
my $fs = Rex::Interface::Fs->create; |
61
|
|
|
|
|
|
|
|
62
|
127
|
100
|
|
|
|
849
|
if ( $fs->is_file($file) ) { |
63
|
112
|
|
|
|
|
1874
|
Rex::Logger::debug("Calculating checksum (MD5) of $file"); |
64
|
|
|
|
|
|
|
|
65
|
112
|
|
33
|
|
|
680
|
my $md5 = _digest_md5($file) // _binary_md5($file); |
66
|
|
|
|
|
|
|
|
67
|
112
|
50
|
|
|
|
707
|
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
|
112
|
|
|
|
|
1483
|
Rex::Logger::debug("MD5 checksum of $file: $md5"); |
74
|
|
|
|
|
|
|
|
75
|
112
|
|
|
|
|
3471
|
return $md5; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
15
|
|
|
|
|
143
|
my $message = "File not found: $file"; |
79
|
15
|
|
|
|
|
102
|
Rex::Logger::debug($message); |
80
|
15
|
|
|
|
|
273
|
die($message); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _digest_md5 { |
85
|
113
|
|
|
113
|
|
301
|
my $file = shift; |
86
|
113
|
|
|
|
|
245
|
my $md5; |
87
|
|
|
|
|
|
|
|
88
|
113
|
50
|
|
|
|
509
|
my $perl = Rex::is_local() ? $EXECUTABLE_NAME : 'perl'; |
89
|
|
|
|
|
|
|
|
90
|
113
|
50
|
33
|
|
|
2721
|
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
|
113
|
|
|
|
|
1370
|
my $result = i_run( $command, fail_ok => 1 ); |
96
|
|
|
|
|
|
|
|
97
|
113
|
50
|
|
|
|
762
|
if ( $? == 0 ) { |
98
|
113
|
|
|
|
|
343
|
$md5 = $result; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
113
|
|
|
|
|
1713
|
return $md5; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _binary_md5 { |
105
|
1
|
|
|
1
|
|
7
|
my $file = shift; |
106
|
1
|
|
|
|
|
12
|
my $md5; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
35
|
my $exec = Rex::Interface::Exec->create; |
109
|
|
|
|
|
|
|
|
110
|
1
|
50
|
|
|
|
20
|
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
|
|
|
|
|
18
|
($md5) = $exec->exec("md5sum '$file'") =~ qr{^\\?([a-f0-9]{32})\s+}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
39
|
return $md5; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |