Ejemplo n.º 1
0
 def setUp(self):
     self.user = '******'
     self.password1 = 'test1'
     self.password2 = 'test2'
     self.key = PrivateKey()
     self.account = Account(self.user, self.password1, self.password2,
                            self.key)
Ejemplo n.º 2
0
    def testConstructor(self):
        key     = PrivateKey()
        account = Account(self.user, self.password1, key = key)
        self.assertEqual(account.get_key(), key)
        self.assertEqual(account.get_password(),
                         account.get_authorization_password())

        account = Account(self.user, self.password1, self.password2)
        self.failIfEqual(account.get_password(),
                         account.get_authorization_password())
Ejemplo n.º 3
0
def account_factory(username=getuser(), password=None, private_key="", keytype="rsa"):
    assert username != ""
    assert keytype in set(["rsa", "dsa", "ecdsa", "ed25519"])

    if password is None:
        password = getpass("Login password for %s: " % username)

    private_key_obj = None
    if isinstance(private_key, str) and private_key != "":
        keypath = os.path.expanduser(private_key)
        assert os.path.isfile(keypath)
        private_key_obj = PrivateKey(keytype=keytype).from_file(keypath)

    account = Account(name=username, password=password, key=private_key_obj)
    return account
Ejemplo n.º 4
0
 def testGetType(self):
     self.testConstructor()
     self.assertEqual(self.key.get_type(), 'rsa')
     self.key = PrivateKey('dss')
     self.assertEqual(self.key.get_type(), 'dss')
Ejemplo n.º 5
0
 def testConstructor(self):
     self.key = PrivateKey()
     self.assertRaises(TypeError, PrivateKey, 'foo')
     PrivateKey('rsa')
     PrivateKey('dss')
Ejemplo n.º 6
0
    def do_ssh_login(
        self,
        connect_timeout=10,
        debug=0,
    ):
        assert isinstance(connect_timeout, int)
        assert isinstance(debug, int)
        assert len(self.account_list) > 0

        # FIXME - clean up PrivateKey here...
        private_key = PrivateKey(keytype="rsa").from_file(self.private_key_path)

        self.downgrade_ssh_crypto = False
        if self.protocol == "ssh":

            for self.ssh_attempt_number in [1, 2, 3]:

                assert self.ssh_attempt_number <= self.MAX_SSH_ATTEMPT

                # You have to change allowed ciphers / key exchange options
                # **before** the connection
                #     -> https://stackoverflow.com/a/31303321/667301
                if self.downgrade_ssh_crypto is True:
                    paramiko.Transport._preferred_ciphers = (
                        "aes128-cbc",
                        "3des-cbc",
                    )
                    paramiko.Transport._preferred_kex = (
                        "diffie-hellman-group-exchange-sha1",
                        "diffie-hellman-group14-sha1",
                        "diffie-hellman-group1-sha1",
                    )

                conn = SSH2(driver=self.driver)

                # Save default values...
                DEFAULT_CONNECT_TIMEOUT = conn.get_connect_timeout()
                DEFAULT_PROMPT_LIST = conn.get_prompt()
                DEFAULT_PROMPT_TIMEOUT = conn.get_timeout()

                # FIXME - Exscript should be handling this but the pypi pkg doesn't
                #

                conn.set_connect_timeout(connect_timeout)
                try:
                    conn.connect(hostname=self.host, port=self.port)
                    break

                except sock.timeout as ee:
                    self.downgrade_ssh_crypto = True
                    if self.ssh_attempt_number == self.MAX_SSH_ATTEMPT:
                        error = "Timeout connecting to TCP port {1} on host:{0}".format(
                            self.host, self.port
                        )
                        logger.critical(error)
                        raise OSError(error)
                    else:
                        assert self.ssh_attempt_number < self.MAX_SSH_ATTEMPT
                        time.sleep(0.5)

                except SSHException as ee:
                    self.downgrade_ssh_crypto = True
                    if self.ssh_attempt_number == self.MAX_SSH_ATTEMPT:
                        error = (
                            "Connection to host:{0} on TCP port {1} was reset".format(
                                self.host, self.port
                            )
                        )
                        logger.critical(error)
                        raise OSError(error)
                    else:
                        assert self.ssh_attempt_number < self.MAX_SSH_ATTEMPT
                        time.sleep(0.5)

            login_success = False
            for account in self.account_list:
                conn.login(account)
                try:
                    assert isinstance(conn, SSH2)  # This succeeds if logged in...
                    login_success = True
                    break
                except AssertionError as aa:
                    # login with account failed...
                    continue

            assert login_success is True
            if login_success is True:
                self.password = account.password
            else:
                raise ValueError("Login to host='%s' failed" % self.host)

            conn.set_connect_timeout(DEFAULT_CONNECT_TIMEOUT)

            return conn

        else:
            raise ValueError("FATAL: proto='%s' isn't a valid protocol" % proto)