示例#1
0
 def connect(self, host=None, username=None, password=None,
             private_key=None, private_key_pass=None, port=None, timeout=30,
             compress=None):
     host = host or self._host
     username = username or self._username
     password = password or self._password
     compress = compress or self._compress
     port = port if port is not None else self._port
     pkey = self._pkey
     if private_key:
         pkey = self.load_private_key(private_key, private_key_pass)
     log.debug("connecting to host %s on port %d as user %s" % (host, port,
                                                                username))
     try:
         sock = self._get_socket(host, port)
         transport = paramiko.Transport(sock)
         transport.banner_timeout = timeout
     except socket.error:
         raise exception.SSHConnectionError(host, port)
     # Enable/disable compression
     transport.use_compression(compress)
     # Authenticate the transport.
     try:
         transport.connect(username=username, pkey=pkey, password=password)
     except paramiko.AuthenticationException:
         raise exception.SSHAuthException(username, host)
     except paramiko.SSHException, e:
         msg = e.args[0]
         raise exception.SSHError(msg)
示例#2
0
 def connect(self,
             host=None,
             username=None,
             password=None,
             private_key=None,
             private_key_pass=None,
             port=22,
             timeout=30):
     host = host or self._host
     username = username or self._username
     password = password or self._password
     pkey = self._pkey
     if private_key:
         pkey = self.load_private_key(private_key, private_key_pass)
     log.debug("connecting to host %s on port %d as user %s" %
               (host, port, username))
     try:
         sock = self._get_socket(host, port)
         transport = ssh.Transport(sock)
         transport.banner_timeout = timeout
     except socket.error:
         raise exception.SSHConnectionError(host, port)
     # Authenticate the transport.
     try:
         transport.connect(username=username, pkey=pkey, password=password)
     except ssh.AuthenticationException:
         raise exception.SSHAuthException(username, host)
     except ssh.SSHException, e:
         msg = e.args[0]
         raise exception.SSHError(msg)
示例#3
0
文件: ssh.py 项目: inteq/StarCluster
class SSHClient(object):
    """
    Establishes an SSH connection to a remote host using either password or
    private key authentication. Once established, this object allows executing
    commands, copying files to/from the remote host, various file querying
    similar to os.path.*, and much more.
    """
    def __init__(self,
                 host,
                 username=None,
                 password=None,
                 private_key=None,
                 private_key_pass=None,
                 port=22,
                 timeout=30):
        self._timeout = timeout
        self._sftp_live = False
        self._sftp = None
        self._pkey = None
        if not username:
            username = os.environ['LOGNAME']
        # Begin the SSH transport.
        self._transport_live = False
        try:
            sock = self._get_socket(host, port)
            self._transport = paramiko.Transport(sock)
            self._transport.banner_timeout = self._timeout
        except socket.error:
            raise exception.SSHConnectionError(host, port)
        self._transport_live = True
        # Authenticate the transport.
        pkey = None
        if private_key:
            # Use Private Key.
            log.debug('private key specified')
            if private_key.endswith('rsa') or private_key.count('rsa'):
                pkey = self._load_rsa_key(private_key, private_key_pass)
            elif private_key.endswith('dsa') or private_key.count('dsa'):
                pkey = self._load_dsa_key(private_key, private_key_pass)
            else:
                log.debug("specified key does not end in either rsa or dsa" + \
                          ", trying both")
                pkey = self._load_rsa_key(private_key, private_key_pass)
                if pkey is None:
                    pkey = self._load_dsa_key(private_key, private_key_pass)
            self._pkey = pkey
        elif not password:
            raise exception.SSHNoCredentialsError()
        try:
            self._transport.connect(username=username,
                                    pkey=pkey,
                                    password=password)
        except paramiko.AuthenticationException:
            raise exception.SSHAuthException(username, host)
        except paramiko.SSHException, e:
            msg = e.args[0]
            raise exception.SSHError(msg)
        except socket.error:
            raise exception.SSHConnectionError(host, port)
示例#4
0
文件: ssh.py 项目: inteq/StarCluster
 def __init__(self,
              host,
              username=None,
              password=None,
              private_key=None,
              private_key_pass=None,
              port=22,
              timeout=30):
     self._timeout = timeout
     self._sftp_live = False
     self._sftp = None
     self._pkey = None
     if not username:
         username = os.environ['LOGNAME']
     # Begin the SSH transport.
     self._transport_live = False
     try:
         sock = self._get_socket(host, port)
         self._transport = paramiko.Transport(sock)
         self._transport.banner_timeout = self._timeout
     except socket.error:
         raise exception.SSHConnectionError(host, port)
     self._transport_live = True
     # Authenticate the transport.
     pkey = None
     if private_key:
         # Use Private Key.
         log.debug('private key specified')
         if private_key.endswith('rsa') or private_key.count('rsa'):
             pkey = self._load_rsa_key(private_key, private_key_pass)
         elif private_key.endswith('dsa') or private_key.count('dsa'):
             pkey = self._load_dsa_key(private_key, private_key_pass)
         else:
             log.debug("specified key does not end in either rsa or dsa" + \
                       ", trying both")
             pkey = self._load_rsa_key(private_key, private_key_pass)
             if pkey is None:
                 pkey = self._load_dsa_key(private_key, private_key_pass)
         self._pkey = pkey
     elif not password:
         raise exception.SSHNoCredentialsError()
     try:
         self._transport.connect(username=username,
                                 pkey=pkey,
                                 password=password)
     except paramiko.AuthenticationException:
         raise exception.SSHAuthException(username, host)
     except paramiko.SSHException, e:
         msg = e.args[0]
         raise exception.SSHError(msg)
示例#5
0
文件: ssh.py 项目: inteq/StarCluster
            self._pkey = pkey
        elif not password:
            raise exception.SSHNoCredentialsError()
        try:
            self._transport.connect(username=username,
                                    pkey=pkey,
                                    password=password)
        except paramiko.AuthenticationException:
            raise exception.SSHAuthException(username, host)
        except paramiko.SSHException, e:
            msg = e.args[0]
            raise exception.SSHError(msg)
        except socket.error:
            raise exception.SSHConnectionError(host, port)
        except EOFError:
            raise exception.SSHConnectionError(host, port)
        except Exception, e:
            raise exception.SSHError(str(e))

    def _get_socket(self, hostname, port):
        for (family, socktype, proto, canonname, sockaddr) in \
        socket.getaddrinfo(hostname, port, socket.AF_UNSPEC,
                           socket.SOCK_STREAM):
            if socktype == socket.SOCK_STREAM:
                af = family
                break
            else:
                raise exception.SSHError('No suitable address family for %s' %
                                         hostname)
        sock = socket.socket(af, socket.SOCK_STREAM)
        sock.settimeout(self._timeout)
示例#6
0
class SSHClient(object):
    """
    Establishes an SSH connection to a remote host using either password or
    private key authentication. Once established, this object allows executing
    commands, copying files to/from the remote host, various file querying
    similar to os.path.*, and much more.
    """

    def __init__(self,
                 host,
                 username=None,
                 password=None,
                 private_key=None,
                 private_key_pass=None,
                 compress=False,
                 port=22,
                 timeout=30):
        self._host = host
        self._port = port
        self._pkey = None
        self._username = username or os.environ['LOGNAME']
        self._password = password
        self._timeout = timeout
        self._sftp = None
        self._scp = None
        self._transport = None
        self._progress_bar = None
        self._compress = compress
        if private_key:
            self._pkey = self.load_private_key(private_key, private_key_pass)
        elif not password:
            raise exception.SSHNoCredentialsError()
        self._glob = SSHGlob(self)
        self.__last_status = None
        atexit.register(self.close)

    def load_private_key(self, private_key, private_key_pass=None):
        # Use Private Key.
        log.debug('loading private key %s' % private_key)
        if private_key.endswith('rsa') or private_key.count('rsa'):
            pkey = self._load_rsa_key(private_key, private_key_pass)
        elif private_key.endswith('dsa') or private_key.count('dsa'):
            pkey = self._load_dsa_key(private_key, private_key_pass)
        else:
            log.debug(
                "specified key does not end in either rsa or dsa, trying both")
            pkey = self._load_rsa_key(private_key, private_key_pass)
            if pkey is None:
                pkey = self._load_dsa_key(private_key, private_key_pass)
        return pkey

    def connect(self, host=None, username=None, password=None,
                private_key=None, private_key_pass=None, port=None, timeout=30,
                compress=None):
        host = host or self._host
        username = username or self._username
        password = password or self._password
        compress = compress or self._compress
        port = port if port is not None else self._port
        pkey = self._pkey
        if private_key:
            pkey = self.load_private_key(private_key, private_key_pass)
        log.debug("connecting to host %s on port %d as user %s" % (host, port,
                                                                   username))
        try:
            sock = self._get_socket(host, port)
            transport = paramiko.Transport(sock)
            transport.banner_timeout = timeout
        except socket.error:
            raise exception.SSHConnectionError(host, port)
        # Enable/disable compression
        transport.use_compression(compress)
        # Authenticate the transport.
        try:
            transport.connect(username=username, pkey=pkey, password=password)
        except paramiko.AuthenticationException:
            raise exception.SSHAuthException(username, host)
        except paramiko.SSHException, e:
            msg = e.args[0]
            raise exception.SSHError(msg)
        except socket.error:
            raise exception.SSHConnectionError(host, port)