| 1 | package Net::CLI::Interact::Transport::SSH; | ||
| 2 | { | ||
| 3 | $Net::CLI::Interact::Transport::SSH::VERSION = '2.143070'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Sub::Quote; | ||
| 8 | use MooX::Types::MooseLike::Base qw(InstanceOf); | ||
| 9 | |||
| 10 | extends 'Net::CLI::Interact::Transport::Base'; | ||
| 11 | |||
| 12 | { | ||
| 13 | package # hide from pause | ||
| 14 | Net::CLI::Interact::Transport::SSH::Options; | ||
| 15 | |||
| 16 | use Moo; | ||
| 17 | use Sub::Quote; | ||
| 18 | use MooX::Types::MooseLike::Base qw(Str Bool ArrayRef Any); | ||
| 19 | |||
| 20 | extends 'Net::CLI::Interact::Transport::Options'; | ||
| 21 | |||
| 22 | has 'host' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => Str, | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has 'username' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => Str, | ||
| 31 | predicate => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has 'shkc' => ( | ||
| 35 | is => 'rw', | ||
| 36 | isa => Bool, | ||
| 37 | default => quote_sub('0'), | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'ignore_host_checks' => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => Bool, | ||
| 43 | default => quote_sub('1'), | ||
| 44 | ); | ||
| 45 | |||
| 46 | has 'opts' => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => ArrayRef[Any], | ||
| 49 | default => sub { [] }, | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 53 | |||
| 54 | has 'connect_options' => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => InstanceOf['Net::CLI::Interact::Transport::SSH::Options'], | ||
| 57 | coerce => quote_sub(q{ (ref '' eq ref $_[0]) ? $_[0] : | ||
| 58 | Net::CLI::Interact::Transport::SSH::Options->new(@_) }), | ||
| 59 | required => 1, | ||
| 60 | ); | ||
| 61 | |||
| 62 | sub _build_app { | ||
| 63 | my $self = shift; | ||
| 64 | die "please pass location of plink.exe in 'app' parameter to new()\n" | ||
| 65 | if $self->is_win32; | ||
| 66 | return 'ssh'; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub runtime_options { | ||
| 70 | my $self = shift; | ||
| 71 | if ($self->is_win32) { | ||
| 72 | return ( | ||
| 73 | '-ssh', | ||
| 74 | @{$self->connect_options->opts}, | ||
| 75 | ($self->connect_options->has_username | ||
| 76 | ? ($self->connect_options->username . '@') : '') | ||
| 77 | . $self->connect_options->host, | ||
| 78 | ); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | return ( | ||
| 82 | (($self->connect_options->ignore_host_checks and not $self->connect_options->shkc) | ||
| 83 | ? ( | ||
| 84 | '-o', 'StrictHostKeyChecking=no', | ||
| 85 | '-o', 'UserKnownHostsFile=/dev/null', | ||
| 86 | '-o', 'CheckHostIP=no', | ||
| 87 | ) : ()), | ||
| 88 | @{$self->connect_options->opts}, | ||
| 89 | ($self->connect_options->has_username | ||
| 90 | ? ('-l', $self->connect_options->username) : ()), | ||
| 91 | $self->connect_options->host, | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | # ABSTRACT: SSH based CLI connection | ||
| 99 | |||
| 100 | |||
| 101 | __END__ | ||
| 102 | =pod | ||
| 103 | |||
| 104 | =head1 NAME | ||
| 105 | |||
| 106 | Net::CLI::Interact::Transport::SSH - SSH based CLI connection | ||
| 107 | |||
| 108 | =head1 VERSION | ||
| 109 | |||
| 110 | version 2.143070 | ||
| 111 | |||
| 112 | =head1 DESCRIPTION | ||
| 113 | |||
| 114 | This module provides a wrapped instance of an SSH client for use by | ||
| 115 | L<Net::CLI::Interact>. | ||
| 116 | |||
| 117 | =head1 INTERFACE | ||
| 118 | |||
| 119 | =head2 app | ||
| 120 | |||
| 121 | On Windows platforms you B<must> download the C<plink.exe> program, and pass its | ||
| 122 | location to the library in this parameter. On other platforms, this defaults to | ||
| 123 | C<ssh> (openssh). | ||
| 124 | |||
| 125 | =head2 runtime_options | ||
| 126 | |||
| 127 | Based on the C<connect_options> hash provided to Net::CLI::Interact on | ||
| 128 | construction, selects and formats parameters to provide to C<app> on the | ||
| 129 | command line. Supported attributes: | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item host (required) | ||
| 134 | |||
| 135 | Host name or IP address of the host to which the SSH application is to | ||
| 136 | connect. Alternatively you can pass a value of the form C<user@host>, but it's | ||
| 137 | probably better to use the separate C<username> parameter instead. | ||
| 138 | |||
| 139 | =item username | ||
| 140 | |||
| 141 | Optionally pass in the username for the SSH connection, otherwise the SSH | ||
| 142 | client defaults to the current user's username. When using this option, you | ||
| 143 | should obviously I<only> pass the host name to C<host>. | ||
| 144 | |||
| 145 | =item ignore_host_checks | ||
| 146 | |||
| 147 | Under normal interactive use C<openssh> tracks the identity of connected hosts | ||
| 148 | and verifies these identities upon each connection. In automation this behaviour | ||
| 149 | can be irritating because it is interactive. | ||
| 150 | |||
| 151 | This option, enabled by default, causes C<openssh> to skip or ignore this host | ||
| 152 | identity verification. This means the default setting is less secure, but also | ||
| 153 | less likely to trip you up. It is equivalent to the following: | ||
| 154 | |||
| 155 | StrictHostKeyChecking=no | ||
| 156 | UserKnownHostsFile=/dev/null | ||
| 157 | CheckHostIP=no | ||
| 158 | |||
| 159 | Pass a false value to this option to disable the above and return C<openssh> to | ||
| 160 | its default configured settings. | ||
| 161 | |||
| 162 | =item opts | ||
| 163 | |||
| 164 | If you want to pass any other options to openssh on its command line, then use | ||
| 165 | this option, which should be an array reference. Each item in the list will be | ||
| 166 | passed to C<openssh>, separated by a single space character. For example: | ||
| 167 | |||
| 168 | $s->new({ | ||
| 169 | # ...other parameters to new()... | ||
| 170 | connect_options => { | ||
| 171 | opts => [ | ||
| 172 | '-p', '222', # connect to non-standard port on remote host | ||
| 173 | '-o', 'CheckHostIP=no', # don't check host IP in known_hosts file | ||
| 174 | ], | ||
| 175 | }, | ||
| 176 | }); | ||
| 177 | |||
| 178 | =item reap | ||
| 179 | |||
| 180 | Only used on Unix platforms, this installs a signal handler which attempts to | ||
| 181 | reap the C<ssh> child process. Pass a true value to enable this feature only | ||
| 182 | if you notice zombie processes are being left behind after use. | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | =head1 COMPOSITION | ||
| 187 | |||
| 188 | See the following for further interface details: | ||
| 189 | |||
| 190 | =over 4 | ||
| 191 | |||
| 192 | =item * | ||
| 193 | |||
| 194 | L<Net::CLI::Interact::Transport::Base> | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | Oliver Gorwits <oliver@cpan.org> | ||
| 201 | |||
| 202 | =head1 COPYRIGHT AND LICENSE | ||
| 203 | |||
| 204 | This software is copyright (c) 2014 by Oliver Gorwits. | ||
| 205 | |||
| 206 | This is free software; you can redistribute it and/or modify it under | ||
| 207 | the same terms as the Perl 5 programming language system itself. | ||
| 208 | |||
| 209 | =cut | ||
| 210 |