问题
I am unable to connect to other server through paramiko:
import paramiko
import sys
import os
hostname = 'server1'
port = 22
username = 'root'
password = 'password'`enter code here`
def deploy_key(key, hostname, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username, password)
client.exec_command('mkdir -p ~/.ssh/')
client.exec_command('echo "%s" > ~/.ssh/authorized_keys' % key)
client.exec_command('chmod 644 ~/.ssh/authorized_keys')
client.exec_command('chmod 700 ~/.ssh/')
key = open(os.path.expanduser('~/.ssh/id_rsa.pub')).read()
deploy_key(key, hostname, username, password)
Here was the output:
socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno -8] Servname not supported for ai_socktype
回答1:
The problem is with the call to client.connect(). It expects port to be second parameter and to be an integer, whereas you are giving username (string) as second parameter.
Try replacing that with below line.
client.connect(hostname, username=username, password=password)
That should work.
来源:https://stackoverflow.com/questions/22251258/paramiko-error-servname-not-supported-for-ai-socktype