ai_socktype 不支持 paramiko 错误 Servname [英] paramiko Error Servname not supported for ai_socktype

查看:107
本文介绍了ai_socktype 不支持 paramiko 错误 Servname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过 paramiko 连接到其他服务器:

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)

这是输出:

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

推荐答案

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

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)

应该可以.

这篇关于ai_socktype 不支持 paramiko 错误 Servname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆