| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Interface::Connection::SSH; | ||
| 6 | |||
| 7 | use v5.14.4; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | our $VERSION = '1.16.1'; # VERSION | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | use Rex::Require; | ||
| 14 | Net::SSH2->require; | ||
| 15 | } | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use Rex::Helper::IP; | ||
| 19 | use Rex::Interface::Connection::Base; | ||
| 20 | use base qw(Rex::Interface::Connection::Base); | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $that = shift; | ||
| 24 | my $proto = ref($that) || $that; | ||
| 25 | my $self = $that->SUPER::new(@_); | ||
| 26 | |||
| 27 | bless( $self, $proto ); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub connect { | ||
| 33 | my ( $self, %option ) = @_; | ||
| 34 | |||
| 35 | my ( | ||
| 36 | $user, $pass, $private_key, $public_key, $server, | ||
| 37 | $port, $timeout, $auth_type, $is_sudo | ||
| 38 | ); | ||
| 39 | |||
| 40 | $user = $option{user}; | ||
| 41 | $pass = $option{password}; | ||
| 42 | $server = $option{server}; | ||
| 43 | $port = $option{port}; | ||
| 44 | $timeout = $option{timeout}; | ||
| 45 | $public_key = $option{public_key}; | ||
| 46 | $private_key = $option{private_key}; | ||
| 47 | $auth_type = $option{auth_type}; | ||
| 48 | $is_sudo = $option{sudo}; | ||
| 49 | |||
| 50 | $self->{server} = $server; | ||
| 51 | $self->{is_sudo} = $is_sudo; | ||
| 52 | $self->{__auth_info__} = \%option; | ||
| 53 | |||
| 54 | Rex::Logger::debug("Using Net::SSH2 for connection"); | ||
| 55 | Rex::Logger::debug( "Using user: " . $user ); | ||
| 56 | Rex::Logger::debug( Rex::Logger::masq( "Using password: %s", $pass ) ) | ||
| 57 | if defined $pass; | ||
| 58 | |||
| 59 | $self->{ssh} = Net::SSH2->new; | ||
| 60 | |||
| 61 | my $fail_connect = 0; | ||
| 62 | |||
| 63 | CON_SSH: | ||
| 64 | $port ||= Rex::Config->get_port( server => $server ) || 22; | ||
| 65 | $timeout ||= Rex::Config->get_timeout( server => $server ) || 3; | ||
| 66 | $self->{ssh}->timeout( $timeout * 1000 ); | ||
| 67 | |||
| 68 | $server = | ||
| 69 | Rex::Config->get_ssh_config_hostname( server => $server ) || $server; | ||
| 70 | |||
| 71 | ( $server, $port ) = Rex::Helper::IP::get_server_and_port( $server, $port ); | ||
| 72 | |||
| 73 | Rex::Logger::debug( "Connecting to $server:$port (" . $user . ")" ); | ||
| 74 | |||
| 75 | unless ( $self->{ssh}->connect( $server, $port ) ) { | ||
| 76 | ++$fail_connect; | ||
| 77 | sleep 1; | ||
| 78 | goto CON_SSH | ||
| 79 | if ( | ||
| 80 | $fail_connect < Rex::Config->get_max_connect_fails( server => $server ) ) | ||
| 81 | ; # try connecting 3 times | ||
| 82 | |||
| 83 | Rex::Logger::info( "Can't connect to $server", "warn" ); | ||
| 84 | |||
| 85 | $self->{connected} = 0; | ||
| 86 | |||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | Rex::Logger::debug( "Current Error-Code: " . $self->{ssh}->error() ); | ||
| 91 | Rex::Logger::debug("Connected to $server, trying to authenticate."); | ||
| 92 | |||
| 93 | $self->{connected} = 1; | ||
| 94 | |||
| 95 | if ( $auth_type && $auth_type eq "pass" ) { | ||
| 96 | Rex::Logger::debug("Using password authentication."); | ||
| 97 | $self->{auth_ret} = $self->{ssh}->auth_password( $user, $pass ); | ||
| 98 | if ( !$self->{auth_ret} ) { | ||
| 99 | |||
| 100 | # try guessing | ||
| 101 | $self->{auth_ret} = $self->{ssh}->auth( | ||
| 102 | 'username' => $user, | ||
| 103 | 'password' => $pass | ||
| 104 | ); | ||
| 105 | |||
| 106 | } | ||
| 107 | } | ||
| 108 | elsif ( $auth_type && $auth_type eq "key" ) { | ||
| 109 | Rex::Logger::debug("Using key authentication."); | ||
| 110 | |||
| 111 | croak "No public_key file defined." if !$public_key; | ||
| 112 | croak "No private_key file defined." if !$private_key; | ||
| 113 | |||
| 114 | $self->{auth_ret} = | ||
| 115 | $self->{ssh}->auth_publickey( $user, $public_key, $private_key, $pass ); | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | Rex::Logger::debug("Trying to guess the authentication method."); | ||
| 119 | $self->{auth_ret} = $self->{ssh}->auth( | ||
| 120 | 'username' => $user, | ||
| 121 | 'password' => $pass, | ||
| 122 | 'publickey' => $public_key || "", | ||
| 123 | 'privatekey' => $private_key || "" | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | $self->{sftp} = $self->{ssh}->sftp; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub reconnect { | ||
| 131 | my ($self) = @_; | ||
| 132 | Rex::Logger::debug("Reconnecting SSH"); | ||
| 133 | |||
| 134 | $self->connect( %{ $self->{__auth_info__} } ); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub disconnect { | ||
| 138 | my ($self) = @_; | ||
| 139 | $self->get_connection_object->disconnect; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub error { | ||
| 143 | my ($self) = @_; | ||
| 144 | return $self->get_connection_object->error; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get_connection_object { | ||
| 148 | my ($self) = @_; | ||
| 149 | return $self->{ssh}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_fs_connection_object { | ||
| 153 | my ($self) = @_; | ||
| 154 | if ( !defined $self->{sftp} ) { | ||
| 155 | Rex::Logger::info( | ||
| 156 | "It seems that you haven't installed or configured sftp on your server.", | ||
| 157 | "warn" | ||
| 158 | ); | ||
| 159 | Rex::Logger::info( | ||
| 160 | "Rex needs sftp for file operations, so please install one.", "warn" ); | ||
| 161 | die("No SFTP server found on remote host."); | ||
| 162 | } | ||
| 163 | return $self->{sftp}; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub is_connected { | ||
| 167 | my ($self) = @_; | ||
| 168 | return $self->{connected}; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub is_authenticated { | ||
| 172 | my ($self) = @_; | ||
| 173 | return $self->{auth_ret}; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub get_connection_type { | ||
| 177 | my ($self) = @_; | ||
| 178 | |||
| 179 | my $type = "SSH"; | ||
| 180 | |||
| 181 | if ( $self->{is_sudo} && $self->{is_sudo} == 1 ) { | ||
| 182 | return "Sudo"; | ||
| 183 | } | ||
| 184 | |||
| 185 | if ( Rex::is_ssh() && !Rex::is_sudo() ) { | ||
| 186 | $type = "SSH"; | ||
| 187 | } | ||
| 188 | elsif ( Rex::is_sudo() ) { | ||
| 189 | $type = "Sudo"; | ||
| 190 | } | ||
| 191 | |||
| 192 | return $type; | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |