| 1 | package Net::CLI::Interact::Transport::Net_OpenSSH; | ||
| 2 | { | ||
| 3 | $Net::CLI::Interact::Transport::Net_OpenSSH::VERSION = '0.01'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Sub::Quote; | ||
| 8 | use MooX::Types::MooseLike::Base qw(InstanceOf ArrayRef Str); | ||
| 9 | |||
| 10 | extends 'Net::CLI::Interact::Transport::Base'; | ||
| 11 | |||
| 12 | { | ||
| 13 | package # hide from pause | ||
| 14 | Net::CLI::Interact::Transport::Net_OpenSSH::Options; | ||
| 15 | |||
| 16 | use Moo; | ||
| 17 | use Sub::Quote; | ||
| 18 | use MooX::Types::MooseLike::Base qw(InstanceOf Any Str HashRef ArrayRef); | ||
| 19 | |||
| 20 | extends 'Net::CLI::Interact::Transport::Options'; | ||
| 21 | |||
| 22 | has 'master' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => InstanceOf['Net::OpenSSH'], | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has 'opts' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => HashRef[Any], | ||
| 31 | default => sub { {} }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has 'shell_cmd' => ( | ||
| 35 | is => 'rw', | ||
| 36 | coerce => quote_sub(q{ (ref '' eq ref $_[0]) ? [$_[0]] : $_[0] }), | ||
| 37 | isa => ArrayRef[Str], | ||
| 38 | default => sub { [] }, | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 42 | |||
| 43 | has 'connect_options' => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => InstanceOf['Net::CLI::Interact::Transport::Net_OpenSSH::Options'], | ||
| 46 | coerce => quote_sub(q{ (ref '' eq ref $_[0]) ? $_[0] : | ||
| 47 | Net::CLI::Interact::Transport::Net_OpenSSH::Options->new(@_) }), | ||
| 48 | required => 1, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has app_and_runtime_options => ( | ||
| 52 | is => 'lazy', | ||
| 53 | isa => ArrayRef[Str], | ||
| 54 | ); | ||
| 55 | |||
| 56 | sub _build_app_and_runtime_options { | ||
| 57 | my $self = shift; | ||
| 58 | my $master = $self->connect_options->master; | ||
| 59 | [ $master->make_remote_command($self->connect_options->opts, | ||
| 60 | @{$self->connect_options->shell_cmd}) ] | ||
| 61 | } | ||
| 62 | |||
| 63 | sub app { | ||
| 64 | shift->app_and_runtime_options->[0] | ||
| 65 | } | ||
| 66 | |||
| 67 | sub runtime_options { | ||
| 68 | my @cmd = @{ shift->app_and_runtime_options }; | ||
| 69 | shift @cmd; | ||
| 70 | @cmd; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | # ABSTRACT: Net_OpenSSH based CLI connection | ||
| 76 | |||
| 77 | |||
| 78 | __END__ | ||
| 79 | =pod | ||
| 80 | |||
| 81 | =encoding UTF-8 | ||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | Net::CLI::Interact::Transport::Net_OpenSSH - Net::OpenSSH based CLI connection | ||
| 86 | |||
| 87 | =head1 VERSION | ||
| 88 | |||
| 89 | Version 0.01 | ||
| 90 | |||
| 91 | =head1 DESCRIPTION | ||
| 92 | |||
| 93 | This module provides a wrapped instance of a L<Net::OpenSSH> SSH | ||
| 94 | client object for use by L<Net::CLI::Interact>. | ||
| 95 | |||
| 96 | This allows one to combine the capability of Net::CLI::Interact to | ||
| 97 | talk to remote servers for which Net::OpenSSH one-command-per-session | ||
| 98 | approach is not well suited (i.e. network equipment running custom | ||
| 99 | administration shells) and still use the capability of Net::OpenSSH to | ||
| 100 | run several sessions over one single SSH connection, including | ||
| 101 | accessing SCP and SFTP services. | ||
| 102 | |||
| 103 | Note that this transport is not supported on Windows as Net::OpenSSH | ||
| 104 | is not supported there either. | ||
| 105 | |||
| 106 | =head1 INTERFACE | ||
| 107 | |||
| 108 | =head2 app_and_runtime_options | ||
| 109 | |||
| 110 | Based on the C<connect_options> hash provided to Net::CLI::Interact on | ||
| 111 | construction, selects and formats the command and arguments required | ||
| 112 | to run the SSH session over the Net::OpenSSH connection. | ||
| 113 | |||
| 114 | Under the hood, this method just wraps Net::OpenSSH | ||
| 115 | C<make_remote_command> method. | ||
| 116 | |||
| 117 | Supported attributes: | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item master | ||
| 122 | |||
| 123 | Reference to the Net::OpenSSH object wrapping the SSH master connection. | ||
| 124 | |||
| 125 | =item opts | ||
| 126 | |||
| 127 | Optional hash of extra options to be forwarded to Net::OpenSSH | ||
| 128 | C<make_remote_command> method. | ||
| 129 | |||
| 130 | =item shell_cmd | ||
| 131 | |||
| 132 | Remote command to start the shell. Can be a single string or an array reference. | ||
| 133 | |||
| 134 | The default is to pass nothing which on conforming SSH implementations | ||
| 135 | starts the shell configured for the user. | ||
| 136 | |||
| 137 | Examples: | ||
| 138 | |||
| 139 | # interact with default user shell: | ||
| 140 | $s->new({ | ||
| 141 | # ...other parameters to new()... | ||
| 142 | connect_options => { master => $ssh }, | ||
| 143 | }); | ||
| 144 | |||
| 145 | # interact with csh: | ||
| 146 | $s->new({ | ||
| 147 | # ...other parameters to new()... | ||
| 148 | connect_options => { | ||
| 149 | master => $ssh, | ||
| 150 | shell_cmd => ['csh', '-i'], | ||
| 151 | }, | ||
| 152 | }); | ||
| 153 | |||
| 154 | =item reap | ||
| 155 | |||
| 156 | Only used on Unix platforms, this installs a signal handler which attempts to | ||
| 157 | reap the C<ssh> child process. Pass a true value to enable this feature only | ||
| 158 | if you notice zombie processes are being left behind after use. | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head1 COMPOSITION | ||
| 163 | |||
| 164 | See the following for further interface details: | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | L<Net::CLI::Interact::Transport::Base> | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | =head1 AUTHORS | ||
| 175 | |||
| 176 | Oliver Gorwits <oliver@cpan.org> | ||
| 177 | Salvador FandiE<ntilde>o <sfandino@yahoo.com> | ||
| 178 | |||
| 179 | =head1 COPYRIGHT AND LICENSE | ||
| 180 | |||
| 181 | This software is copyright (c) 2014 by Oliver Gorwits. | ||
| 182 | This software is copyright (c) 2014 by Salvador Fandiño. | ||
| 183 | |||
| 184 | This is free software; you can redistribute it and/or modify it under | ||
| 185 | the same terms as the Perl 5 programming language system itself. | ||
| 186 | |||
| 187 | =cut | ||
| 188 |