line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SSH::RPC::Shell; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = 1.201; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1461
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
6
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
SSH::RPC::Shell - The shell, or server side, of an RPC call over SSH. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
To make your own shell with it's own methods: |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
package MyShell; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use base 'SSH::RPC::Shell'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub run_time { |
21
|
|
|
|
|
|
|
my ($class, $args) = @_; |
22
|
|
|
|
|
|
|
return { |
23
|
|
|
|
|
|
|
status => 200, |
24
|
|
|
|
|
|
|
response => time(), |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
1; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
To create a usuable shell: |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#!/usr/bin/perl |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use strict; |
36
|
|
|
|
|
|
|
use MyShell; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
MyShell->run(); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
SSH::RPC::Shell allows you to quickly implement your own shells that are remotely callable with L. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 METHODS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
The following methods are available from this class. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#------------------------------------------------------------------- |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head2 processRequest ( request ) |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub processRequest { |
61
|
1
|
|
|
1
|
1
|
3
|
my ($class, $request) = @_; |
62
|
1
|
|
|
|
|
4
|
my $command = 'run_'.$request->{command}; |
63
|
1
|
|
|
|
|
2
|
my $args = $request->{args}; |
64
|
1
|
50
|
|
|
|
15
|
if (my $sub = $class->can($command)) { |
65
|
1
|
|
|
|
|
4
|
return $sub->($args); |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
0
|
return { "error" => "Method not allowed.", "status" => "405" }; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#------------------------------------------------------------------- |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 run () |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Class method. This method is executed to invoke the shell. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub run { |
79
|
0
|
|
|
0
|
1
|
0
|
my $class = shift; |
80
|
0
|
|
|
|
|
0
|
my $json = JSON->new->utf8; |
81
|
0
|
|
|
|
|
0
|
my $request; |
82
|
0
|
|
|
|
|
0
|
while (my $line = ) { |
83
|
0
|
|
|
|
|
0
|
$request = eval {$json->incr_parse($line)}; |
|
0
|
|
|
|
|
0
|
|
84
|
0
|
0
|
|
|
|
0
|
if ($@) { |
85
|
0
|
|
|
|
|
0
|
warn $@; |
86
|
0
|
|
|
|
|
0
|
print '{ "error" : "Malformed request.", "status" : "400" }'; |
87
|
0
|
|
|
|
|
0
|
return; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
0
|
|
|
|
0
|
last if defined $request; |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
0
|
my $result = $class->processRequest($request); |
92
|
0
|
|
|
|
|
0
|
$result->{version} = $VERSION; |
93
|
0
|
|
|
|
|
0
|
my $encodedResult = eval{JSON->new->pretty->utf8->encode($result)}; |
|
0
|
|
|
|
|
0
|
|
94
|
0
|
0
|
|
|
|
0
|
if ($@) { |
95
|
0
|
|
|
|
|
0
|
print { "error" => "Malformed response.", "status" => "511" }; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
0
|
|
|
|
|
0
|
print $encodedResult."\n"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#------------------------------------------------------------------- |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 run_noop () |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Class method. This subroutine just returns a successful status so you know that communication is working. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub run_noop { |
112
|
3
|
|
|
3
|
1
|
691
|
return {status=>200}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 LEGAL |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
------------------------------------------------------------------- |
118
|
|
|
|
|
|
|
SSH::RPC::Client is Copyright 2008-2009 Plain Black Corporation |
119
|
|
|
|
|
|
|
and is licensed under the same terms as Perl itself. |
120
|
|
|
|
|
|
|
------------------------------------------------------------------- |
121
|
|
|
|
|
|
|
http://www.plainblack.com info@plainblack.com |
122
|
|
|
|
|
|
|
------------------------------------------------------------------- |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|