使用基于主机的身份验证无需密码的Paramiko SSH

时间:2018-07-11 06:32:15

标签: python ssh paramiko

我可以使用命令从Linux盒A转换到Linux盒B

ssh user@ip.com 

此连接不需要密码。我正在尝试使用Python Paramiko将其自动化。这是代码

import os
import paramiko
ssh_client =paramiko.SSHClient()
ssh_client.connect(
    hostname="myhost.com",
    username="jagan",
    password=None,
    look_for_keys=False
)

它出现以下错误:

/lib/python3.6/site-packages/paramiko/client.py in connect(self, hostname, port, username, password, pkey, key_filename, timeout, allow_agent, look_for_keys, compress, sock, gss_auth, gss_kex, gss_deleg_creds, gss_host, banner_timeout, auth_timeout, gss_trust_dns, passphrase)
    422             username, password, pkey, key_filenames, allow_agent,
    423             look_for_keys, gss_auth, gss_kex, gss_deleg_creds, t.gss_host,
--> 424             passphrase,
    425         )
    426 

/Application/DataScience/Anaconda/anaconda3/envs/hub/lib/python3.6/site-packages/paramiko/client.py in _auth(self, username, password, pkey, key_filenames, allow_agent, look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host, passphrase)
    713         if saved_exception is not None:
    714             raise saved_exception
--> 715         raise SSHException('No authentication methods available')
    716 
    717     def _log(self, level, msg):

SSHException: No authentication methods available

这是连接服务器的正确方法而无需密码或没有任何内容


身份验证机制是基于主机的一种。在/etc/ssh/ssh_config中:

HostbasedAuthentication yes
EnableSSHKeySign yes setting

我认为这是不要求输入密码的原因。

2 个答案:

答案 0 :(得分:1)

Paramiko不支持SSH“基于主机”的身份验证。

考虑切换到常规公钥身份验证。

答案 1 :(得分:0)

有一个类似的问题,因为我想使用paramiko ssh的传输层执行安全副本(scp)。

花了我几个小时,直到我从scp python lib文档中找到了解决方案,然后我设法连接到不使用任何密钥也没有密码的主机。也许此传输层也可以用于SSH客户端。遵循我的示例代码:

import paramiko
from scp import SCPClient
import sys

def transfer_image(source, destination):
    def progress(file, size, transferred):
        sys.stdout.flush()
        sys.stdout.write("\rTransferred {0:.2f}Mb of {1:.2f} Mb total. {2:.2f}%"
              .format(transferred / 1048576, size / 1048576, transferred * 100 / size))

    hostname = '192.168.1.100'
    username = "root"
    port = 22

    try:
        print('Trying to connect...')

        t = paramiko.Transport((hostname, port))
        t.connect()
        t.auth_none(username)

        scp = SCPClient(t, progress=progress)
        print('Connected, sending file {}'.format(source))
        scp.put(source, destination)

        scp.close()
    except:
        print('Error transferring files.')