我想ssh到一个VM并执行一些命令。我尝试SSH的VM具有私钥认证和登录密码。这里使用了Paramiko python模块,但是得到了带有错误原因的saved_exception:
paramiko.BadAuthenticationType:错误的身份验证类型(allowed_types = [' publickey',' gssapi-keyex',' gssapi-with-mic'])
我在这里尝试使用用户名和密码作为密码,并将私钥文件作为key_file(也尝试了publick键)但无法建立连接。
尝试的代码如下:
from paramiko import client
class ssh:
client = None
def __init__ (self, server_addr,username, password, keyfile):
print ("Establishing VM connection")
#Create ssh client.
self.client = client.SSHClient ()
## The following line is required if you want the script to be able to access a server that's not yet in the known_hosts.
self.client.set_missing_host_key_policy (client.AutoAddPolicy())
#Make the connection.
self.client.connect (server_addr, username=username, password=password, key_filename=keyfile);
def sendCommand (self, command):
#Check if connection is made successfully.
if (self.client) :
stdin,stdout,stderr = self.client.exec_command (command)
while not stdout.channel.exit_status_ready ():
alldata = stdout.channel.recv (1024)
while stdout.channel.recv_ready ():
alldata += stdout.channel.recv(1024)
print (str(alldata))
else:
print "Connection is not opened"