paramiko错误ai_socktype不支持Servname

时间:2014-03-07 13:21:43

标签: paramiko

我无法通过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)

以下是输出:

socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno -8] Servname not supported for ai_socktype

1 个答案:

答案 0 :(得分:10)

问题在于对client.connect()的调用。它期望port是第二个参数并且是一个整数,而您将用户名(字符串)作为第二个参数。 尝试用下面的行替换它。

client.connect(hostname, username=username, password=password)

那应该有用。