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