| 1 | package XAS::Lib::SSH::Client::Shell; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | our $VERSION = '0.01'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use Params::Validate qw(SCALAR CODEREF); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | use XAS::Class | ||||||||||||||
| 7 | 1 | debug => 0, | |||||||||||||
| 8 | version => $VERSION, | ||||||||||||||
| 9 | base => 'XAS::Lib::SSH::Client', | ||||||||||||||
| 10 | utils => 'trim', | ||||||||||||||
| 11 | vars => { | ||||||||||||||
| 12 | PARAMS => { | ||||||||||||||
| 13 | -eol => { optional => 1, default => "\012" } | ||||||||||||||
| 14 | } | ||||||||||||||
| 15 | } | ||||||||||||||
| 16 | 1 | ▶ | ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | #use Data::Dumper; | ||||||||||||||
| 19 | |||||||||||||||
| 20 | # ---------------------------------------------------------------------- | ||||||||||||||
| 21 | # Public Methods | ||||||||||||||
| 22 | # ---------------------------------------------------------------------- | ||||||||||||||
| 23 | |||||||||||||||
| 24 | sub setup { | ||||||||||||||
| 25 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
setup: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | my $output; | |||||||||||||
| 28 | |||||||||||||||
| 29 | # Merge stderr and stdout. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 0 | $self->chan->ext_data('merge'); | |||||||||||||
| 32 | |||||||||||||||
| 33 | # The following needs to be done to talk with a | ||||||||||||||
| 34 | # KpyM SSH Server, other servers don't seem to care. | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | $self->chan->pty('vt100'); # set up a default pty | |||||||||||||
| 37 | 0 | $self->chan->shell(); # ask for a shell | |||||||||||||
| 38 | 0 | $self->put($self->eol); # flush output buffer | |||||||||||||
| 39 | |||||||||||||||
| 40 | # Flush the input buffer. Discards any banners, welcomes, | ||||||||||||||
| 41 | # announcements, motds and other assorted stuff. | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | while ($output = $self->get()) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | # Parse the output looking for specific strings. There | ||||||||||||||
| 46 | # must be a better way... | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | ▶ | if ($output =~ /\[3;1f$/ ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | # Found a KpyM SSH Server, with the naq screen... | ||||||||||||||
| 51 | # | ||||||||||||||
| 52 | # Also KpyM (cmd.exe??) needs a \r\n eol for command | ||||||||||||||
| 53 | # execution. Bitvise dosen't seem to require this. | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | $self->{eol} = "\015\012"; | |||||||||||||
| 56 | |||||||||||||||
| 57 | # Need to wait for the "continue" line. Pay the | ||||||||||||||
| 58 | # danegield, but don't register the key, or this | ||||||||||||||
| 59 | # code will stop working! | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | while ($output = $self->get()) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ▶ | if ($output =~ /continue\./) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | $self->put($self->eol); | |||||||||||||
| 66 | |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | } elsif ($output =~ /\[c$/) { | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # Found an OpenVMS SSH server. SET TERM/INQUIRE must | ||||||||||||||
| 74 | # be set for this code to work. DCL expects a \r\n | ||||||||||||||
| 75 | # eol for command execution. | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | $self->{eol} = "\015\012"; | |||||||||||||
| 78 | |||||||||||||||
| 79 | # Wait for this line, it indicates that the terminal | ||||||||||||||
| 80 | # capabilities negotiation has finished. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | ▶ | do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | $output = $self->get(); | |||||||||||||
| 85 | |||||||||||||||
| 86 | } until ($output =~ /\[0c$/); | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | $self->put($self->eol); | |||||||||||||
| 89 | |||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | sub run { | ||||||||||||||
| 97 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 98 | 0 | my ($command) = $self->validate_params(\@_, [1] ); | |||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | $self->puts($command); # send the command | |||||||||||||
| 101 | 0 | $self->get(); # strip the echo back | |||||||||||||
| 102 | |||||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | sub call { | ||||||||||||||
| 106 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
call: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 107 | 0 | my ($command, $parser) = $self->validate_params(\@_, [ | |||||||||||||
| 108 | { type => SCALAR }, | ||||||||||||||
| 109 | { type => CODEREF }, | ||||||||||||||
| 110 | ]); | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | my $output; | |||||||||||||
| 113 | |||||||||||||||
| 114 | # execute a command, retrieve the output and dispatch to a parser. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | $self->puts($command); # send the command | |||||||||||||
| 117 | 0 | $output = $self->gets(); # get the command result | |||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | return $parser->(trim($output)); # remove line endings | |||||||||||||
| 120 | |||||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | # ---------------------------------------------------------------------- | ||||||||||||||
| 124 | # Private Methods | ||||||||||||||
| 125 | # ---------------------------------------------------------------------- | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 1 | 1; | |||||||||||||
| 128 | |||||||||||||||
| 129 | __END__ | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 NAME | ||||||||||||||
| 132 | |||||||||||||||
| 133 | XAS::Lib::SSH::Client::Shell - A class to interact with the SSH Shell facility | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head1 SYNOPSIS | ||||||||||||||
| 136 | |||||||||||||||
| 137 | use XAS::Lib::SSH::Client::Shell; | ||||||||||||||
| 138 | |||||||||||||||
| 139 | my $client = XAS::Lib::SSH::Client::Shell->new( | ||||||||||||||
| 140 | -host => 'test-xen-01', | ||||||||||||||
| 141 | -username => 'root', | ||||||||||||||
| 142 | -password => 'secret', | ||||||||||||||
| 143 | -eol => "\012", | ||||||||||||||
| 144 | ); | ||||||||||||||
| 145 | |||||||||||||||
| 146 | $client->connect(); | ||||||||||||||
| 147 | |||||||||||||||
| 148 | my @vms = $client->call('xe vm-list params', sub { | ||||||||||||||
| 149 | my $output = shift; | ||||||||||||||
| 150 | ... | ||||||||||||||
| 151 | }); | ||||||||||||||
| 152 | |||||||||||||||
| 153 | $client->disconnect(); | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head1 DESCRIPTION | ||||||||||||||
| 156 | |||||||||||||||
| 157 | This module uses the SSH Shell subsystem to execute commands. Which means it | ||||||||||||||
| 158 | executes a procedure on a remote host and parses the resulting output. This | ||||||||||||||
| 159 | module inherits from L<XAS::Lib::SSH::Client|XAS::Lib::SSH::Client>. | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head1 METHODS | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =head2 setup | ||||||||||||||
| 164 | |||||||||||||||
| 165 | This method will set up the environment to execute commands using the shell | ||||||||||||||
| 166 | subsystem on a remote system. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head2 run($command) | ||||||||||||||
| 169 | |||||||||||||||
| 170 | Run a command. The purpose is to run a procedure on the remote host | ||||||||||||||
| 171 | that will interact with your process over STDIN/STDOUT. This is a work around | ||||||||||||||
| 172 | for SSH Servers that don't support subsystems. | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =over 4 | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =item B<$command> | ||||||||||||||
| 177 | |||||||||||||||
| 178 | The command to run on the remote system. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =back | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head2 call($buffer, $parser) | ||||||||||||||
| 183 | |||||||||||||||
| 184 | This method sends a buffer to the remote host and parses the output. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | The assumption with this method is that some sort of parsable data stream will | ||||||||||||||
| 187 | be returned. After the data has been parsed the results are returned to the | ||||||||||||||
| 188 | caller. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =over 4 | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =item B<$buffer> | ||||||||||||||
| 193 | |||||||||||||||
| 194 | The buffer to send. | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =item B<$parser> | ||||||||||||||
| 197 | |||||||||||||||
| 198 | A coderef to the parser that will parse the returned data. The parser | ||||||||||||||
| 199 | will accept one parameter which is a reference to that data. | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =back | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head1 SEE ALSO | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =over 4 | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =item L<XAS|XAS> | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =back | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||
| 212 | |||||||||||||||
| 213 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 216 | |||||||||||||||
| 217 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||||||
| 218 | |||||||||||||||
| 219 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 220 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||||||
| 221 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||||||
| 222 | |||||||||||||||
| 223 | =cut | ||||||||||||||