def manual_auth(username, hostname): default_auth = ‘p‘ auth = input(‘Auth by (p)assword, (r)sa key, or (d)ss key? [%s] ‘ % default_auth) if len(auth) == 0: auth = default_auth if auth == ‘r‘: default_path = os.path.join(os.environ[‘HOME‘], ‘.ssh‘, ‘id_rsa‘) path = input(‘RSA key [%s]: ‘ % default_path) if len(path) == 0: path = default_path try: key = paramiko.RSAKey.from_private_key_file(path) except paramiko.PasswordRequiredException: password = getpass.getpass(‘RSA key password: ‘) key = paramiko.RSAKey.from_private_key_file(path, password) t.auth_publickey(username, key) elif auth == ‘d‘: default_path = os.path.join(os.environ[‘HOME‘], ‘.ssh‘, ‘id_dsa‘) path = input(‘DSS key [%s]: ‘ % default_path) if len(path) == 0: path = default_path try: key = paramiko.DSSKey.from_private_key_file(path) except paramiko.PasswordRequiredException: password = getpass.getpass(‘DSS key password: ‘) key = paramiko.DSSKey.from_private_key_file(path, password) t.auth_publickey(username, key) else: pw = getpass.getpass(‘Password for %[email protected]%s: ‘ % (username, hostname)) t.auth_password(username, pw)
时间: 2024-10-08 17:41:17