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