def _get_pkey(self, key): if isinstance(key, six.string_types): key = six.moves.StringIO(key) errors = [] for key_class in (paramiko.rsakey.RSAKey, paramiko.dsskey.DSSKey): try: return key_class.from_private_key(key) except paramiko.SSHException as e: errors.append(e) raise exceptions.SSHError(error_msg='Invalid pkey: %s' % errors)
def _get_client(self): if self.is_connected: return self._client try: self._client = paramiko.SSHClient() self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self._client.connect(self.host, username=self.user, port=self.port, pkey=self.pkey, key_filename=self.key_filename, password=self.password, allow_agent=False, look_for_keys=False, timeout=1) return self._client except Exception as e: message = ("Exception %(exception_type)s was raised " "during connect. Exception value is: %(exception)r" % { "exception": e, "exception_type": type(e) }) self._client = False raise exceptions.SSHError(error_msg=message)
def _run(self, client, cmd, stdin=None, stdout=None, stderr=None, raise_on_error=True, timeout=3600, keep_stdin_open=False, pty=False): transport = client.get_transport() session = transport.open_session() if pty: session.get_pty() session.exec_command(cmd) start_time = time.time() # encode on transmit, decode on receive data_to_send = encodeutils.safe_encode("", incoming='utf-8') stderr_data = None # If we have data to be sent to stdin then `select' should also # check for stdin availability. if stdin and not stdin.closed: writes = [session] else: writes = [] while True: # Block until data can be read/write. e = select.select([session], writes, [session], 1)[2] if session.recv_ready(): data = encodeutils.safe_decode(session.recv(4096), 'utf-8') self.log.debug("stdout: %r", data) if stdout is not None: stdout.write(data) continue if session.recv_stderr_ready(): stderr_data = encodeutils.safe_decode( session.recv_stderr(4096), 'utf-8') self.log.debug("stderr: %r", stderr_data) if stderr is not None: stderr.write(stderr_data) continue if session.send_ready(): if stdin is not None and not stdin.closed: if not data_to_send: stdin_txt = stdin.read(4096) if stdin_txt is None: stdin_txt = '' data_to_send = encodeutils.safe_encode( stdin_txt, incoming='utf-8') if not data_to_send: # we may need to keep stdin open if not keep_stdin_open: stdin.close() session.shutdown_write() writes = [] if data_to_send: sent_bytes = session.send(data_to_send) # LOG.debug("sent: %s" % data_to_send[:sent_bytes]) data_to_send = data_to_send[sent_bytes:] if session.exit_status_ready(): break if timeout and (time.time() - timeout) > start_time: message = ( 'Timeout executing command %(cmd)s on host %(host)s' % { "cmd": cmd, "host": self.host }) raise exceptions.SSHTimeout(error_msg=message) if e: raise exceptions.SSHError(error_msg='Socket error') exit_status = session.recv_exit_status() if exit_status != 0 and raise_on_error: fmt = "Command '%(cmd)s' failed with exit_status %(status)d." details = fmt % {"cmd": cmd, "status": exit_status} if stderr_data: details += " Last stderr data: '%s'." % stderr_data raise exceptions.SSHError(error_msg=details) return exit_status