Exemple #1
0
    def Connect(self):
        """Makes the connection.

    We can have an instance of this class without being connected to the
    device, e.g. after a disconnect. Hence setting up the actual SSH connection
    should happen in this method, not in the constructor.
    """
        try:
            if self.ssh_client:
                # An SSH client was provided. Use it.
                self._ssh_client = self.ssh_client.Connect(
                    hostname=self._host,
                    username=self._username,
                    password=self._password,
                    ssh_keys=self._ssh_keys,
                    timeout=self._connect_timeout)
            else:
                # The Connect() function from the sshclient module is a factory that
                # returns a paramiko.SSHClient instance.
                self._ssh_client = sshclient.Connect(
                    hostname=self._host,
                    username=self._username,
                    password=self._password,
                    ssh_keys=self._ssh_keys,
                    timeout=self._connect_timeout)
        except (exceptions.ConnectError, exceptions.AuthenticationError) as e:
            raise ConnectionError(str(e))
        # We are connected. Now set up pexpect.
        logging.debug('SETTING UP PEXPECT')
        try:
            ssh_channel = self._ssh_client.invoke_shell()
            logging.debug('INVOKED A SHELL')
            ssh_channel.set_combine_stderr(True)
            logging.debug('COMBINED STDERR')
            self.child = self._spawn(ssh_channel, maxread=8192)
            logging.debug('SPAWNED')
            timeout = max(1, self._connect_timeout)
            pattern = self.child.expect([self._success], timeout=timeout)
            logging.debug('GOT PATTERN: %s', pattern)
            if pattern == 0:
                self._MaybeFindPrompt()
        except pexpect.TIMEOUT:
            raise TimeoutError(timeout)
        except pexpect.EOF as e:
            raise ConnectionError(str(e))
        except paramiko.SSHException as e:
            msg = 'SSHException connecting to %r: %s' % (self._host, e)
            raise ConnectionError(msg)

        # Used by _Disconnect in ftos.py and ios.py.
        self.exit_list = self.child.compile_pattern_list(pexpect.EOF)
        return None
Exemple #2
0
  def __init__(self, host, username, password=None):
    """Initializer.

    Args:
      host: As per parent.
      username: As per parent.
      password: As per parent.
    """
    super(ScpPutConnection, self).__init__(host, username, password)
    self._ssh_client = sshclient.Connect(hostname=self._host,
                                         username=self._username,
                                         password=self._password)
    self.transport = self._ssh_client.get_transport()
Exemple #3
0
    def _Connect(self,
                 username,
                 password=None,
                 ssh_keys=None,
                 enable_password=None,
                 ssl_cert_set=None):
        _ = ssl_cert_set
        logging.debug(
            'In Paramiko._Connect, host is %s, self._connected? %s'
            '_ssh_client is None? %s', self.host, self._connected,
            self._ssh_client is None)
        self._username = username
        self._password = password or self._password
        self._ssh_keys = ssh_keys or self._ssh_keys or ()
        self._enable_password = enable_password or self._enable_password

        self._ssh_client = sshclient.Connect(hostname=self.loopback_ipv4,
                                             username=self._username,
                                             password=self._password,
                                             port=self._port,
                                             ssh_keys=self._ssh_keys)
        return None