-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperl-remote-exec.pl
executable file
·72 lines (53 loc) · 1.41 KB
/
perl-remote-exec.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl -w
#
# perl-remote-exec.pl
#
# Developed by Lubomir Host <[email protected]>
# Licensed under terms of GNU General Public License.
# All rights reserved.
#
# Changelog:
# 2016-11-17 - created
#
use strict;
use warnings;
use MIME::Base64 qw( encode_base64 decode_base64 );
$| = 1;
# piese of cake:
# loader replaces very very hard quote escaping for perl(shell(ssh(shell(perl -e))))
my $base64_loader = encode_base64('sub load { eval(decode_base64(shift)); if ($@) { print "{ \"error\": \"$@\" }"; } else { run(@_); } }', '');
# read source code into variable
sub encode_perl_code($)
{ # {{{
my ($scriptname) = @_;
my $code = '';
open(CODE, '<', $scriptname) or die "Can't read perl code '$scriptname': $!";
while (my $line = <CODE>) {
$code .= $line;
}
close(CODE);
my $encoded = encode_base64($code, '');
return $encoded;
} # }}}
sub cmd_output($)
{ # {{{
my ($command) = @_;
my $out = '';
#print "CMD_OUTPUT: $command\n";
open(CMD, '-|', $command) or die $@;
my $line;
while (defined($line = <CMD>)) {
$out .= $line;
}
close CMD;
return $out;
} # }}}
my ($remote, $scriptname, @args) = @ARGV;
my $base64_code = encode_perl_code($scriptname);
my $output_str = cmd_output("ssh $remote "
. "perl -MMIME::Base64 -e '\"eval decode_base64 qw=$base64_loader; load(\@ARGV);\"' "
. "$base64_code "
. join(' ', @args)
);
print "OUTPUT: '$output_str'\n";
# vim: ts=4 fdm=marker fdl=0 fdc=3