调用paramiko client.connect函数

时间:2019-07-15 18:10:45

标签: authentication exception ssh google-cloud-platform paramiko

我对paramiko完全陌生。我正在尝试使用此程序包SSH到gcp虚拟机并在其上运行一些命令。但是我收到了AuthenticationException错误。而且我很确定conf_file中的用户名和密码正确。

这是我运行的代码:

import paramiko
import os, sys, time
import configparser
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from conf import ssh_conf as conf_file
import socket

class Ssh_Util:
    "Class to connect to remote server"

    def __init__(self):
        self.ssh_output = None
        self.ssh_error = None
        self.client = None
        self.host = conf_file.HOST
        self.username = conf_file.USERNAME
        self.password = conf_file.PASSWORD
        self.timeout = float(conf_file.TIMEOUT)
        self.commands = conf_file.COMMANDS
        self.pkey = conf_file.PKEY
        self.passpharse = conf_file.PASSPHRASE
        self.port = conf_file.PORT
        self.uploadremotefilepath = conf_file.UPLOADREMOTEFILEPATH
        self.uploadlocalfilepath = conf_file.UPLOADLOCALFILEPATH
        self.downloadremotefilepath = conf_file.DOWNLOADREMOTEFILEPATH
        self.downloadlocalfilepath = conf_file.DOWNLOADLOCALFILEPATH

ssh_obj = Ssh_Util()
print("Establishing ssh connection")
ssh_obj.client = paramiko.SSHClient()
ssh_obj.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_obj.pkey = paramiko.RSAKey.from_private_key_file(ssh_obj.pkey, password = ssh_obj.passpharse)
ssh_obj.client.connect(hostname=ssh_obj.host, port=ssh_obj.port, username=ssh_obj.username, pkey=ssh_obj.pkey,
                                    timeout=ssh_obj.timeout, allow_agent=False, look_for_keys=False)

错误消息显示如下:

Establishing ssh connection
Traceback (most recent call last):

  File "<ipython-input-40-499c7580c0b0>", line 1, in <module>
    runfile('/Users/yuyanwang/Desktop/gcp_utilization/temp.py', wdir='/Users/yuyanwang/Desktop/gcp_utilization')

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/yuyanwang/Desktop/gcp_utilization/temp.py", line 50, in <module>
    timeout=ssh_obj.timeout, allow_agent=False, look_for_keys=False)

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/paramiko-2.6.0-py3.6.egg/paramiko/client.py", line 446, in connect
    passphrase,

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/paramiko-2.6.0-py3.6.egg/paramiko/client.py", line 764, in _auth
    raise saved_exception

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/paramiko-2.6.0-py3.6.egg/paramiko/client.py", line 664, in _auth
    self._transport.auth_publickey(username, pkey)

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/paramiko-2.6.0-py3.6.egg/paramiko/transport.py", line 1580, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)

  File "/Users/yuyanwang/anaconda3/lib/python3.6/site-packages/paramiko-2.6.0-py3.6.egg/paramiko/auth_handler.py", line 250, in wait_for_response
    raise e

AuthenticationException: Authentication failed.

我该如何解决此错误并通过SSH托管?

0 个答案:

没有答案