当我使用ssh命令手动连接到主机时,一切正常:
$ ssh -v admin@mycli.abc.com -p 10000 OpenSSH_6.1p1, OpenSSL 1.0.1c 10 May 2012 debug1: Reading configuration data /home/todd/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: Connecting to mycli.abc.com [mycli.abc.com] port 10000. debug1: Connection established. debug1: identity file /home/todd/.ssh/id_rsa type 1 debug1: identity file /home/todd/.ssh/id_rsa-cert type -1 debug1: identity file /home/todd/.ssh/id_dsa type -1 debug1: identity file /home/todd/.ssh/id_dsa-cert type -1 debug1: identity file /home/todd/.ssh/id_ecdsa type -1 debug1: identity file /home/todd/.ssh/id_ecdsa-cert type -1 debug1: Remote protocol version 2.0, remote software version SSHD-CORE-0.6.0 debug1: no match: SSHD-CORE-0.6.0 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.1 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-cbc hmac-md5 none debug1: kex: client->server aes128-cbc hmac-md5 none debug1: sending SSH2_MSG_KEXDH_INIT debug1: expecting SSH2_MSG_KEXDH_REPLY debug1: Server host key: DSA 6f:2c:48:80:86:ff:69:99:28:c2:21:5b:02:d4:7f:63 debug1: checking without port identifier The authenticity of host '[mycli.abc.com]:10000 ([mycli.abc.com]:10000)' can't be established. DSA key fingerprint is 6f:2c:48:80:86:ff:69:99:28:c2:21:5b:02:d4:7f:63. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[mycli.abc.com]:10000' (DSA) to the list of known hosts. debug1: ssh_dss_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: Roaming not allowed by server debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: password,publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /home/todd/.ssh/id_rsa debug1: Authentications that can continue: password,publickey debug1: Trying private key: /home/todd/.ssh/id_dsa debug1: Trying private key: /home/todd/.ssh/id_ecdsa debug1: Next authentication method: password admin@mycli.abc.com's password: debug1: Authentication succeeded (password). Authenticated to mycli.abc.com ([mycli.abc.com]:10000). debug1: channel 0: new [client-session] debug1: Entering interactive session. debug1: No xauth program. Warning: No xauth data; using fake authentication data for X11 forwarding. debug1: Requesting X11 forwarding with authentication spoofing. X11 forwarding request failed on channel 0 Cli>
但是使用paramiko总是失败:
DEBUG:paramiko.transport:starting thread (client mode): 0x22783e10L INFO:paramiko.transport:Connected (version 2.0, client SSHD-CORE-0.6.0) DEBUG:paramiko.transport:kex algos:['diffie-hellman-group1-sha1'] server key:['ssh-dss'] client encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc'] server encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-md5-96', 'hmac-sha1-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-md5-96', 'hmac-sha1-96'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False DEBUG:paramiko.transport:Ciphers agreed: local=aes128-cbc, remote=aes128-cbc DEBUG:paramiko.transport:using kex diffie-hellman-group1-sha1; server key type ssh-dss; cipher: local aes128-cbc, remote aes128-cbc; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none DEBUG:paramiko.transport:Switch to new keys ... DEBUG:paramiko.transport:Adding ssh-dss host key for [mycli.abc.com]:10000: 6f2c488086ff699928c2215b02d47f63 DEBUG:paramiko.transport:Trying discovered key d7ab335994d0e5c90f8886b515faff55 in /home/todd/.ssh/id_rsa DEBUG:paramiko.transport:userauth is OK INFO:paramiko.transport:Authentication (publickey) failed. INFO:paramiko.transport:Disconnect (code 2): Protocol error: expected packet SSH_MSG_USERAUTH_REQUEST, got SSH_MSG_SERVICE_REQUEST Traceback (most recent call last): File "./mycli_test.py", line 13, in <module> oc = MyCli(hostname=hostname, username=username, password=password) File "/home/todd/devel/mylib/mycli.py", line 16, in __init__ key_filename, timeout, allow_agent, look_for_keys, compress) File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 338, in connect self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 519, in _auth raise saved_exception paramiko.AuthenticationException: Authentication failed.
paramiko脚本:
#!/usr/bin/env python2 import paramiko paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) ssh.connect(hostname='mycli.abc.com', port=10000, username='admin', password='abc123') i, o, e = ssh.exec_command('uname -a') print o.readlines() ssh.close()
paramiko版本:1.9.0
为什么会引发“协议错误:预期的数据包SSH_MSG_USERAUTH_REQUEST,得到SSH_MSG_SERVICE_REQUEST”错误?如何避免呢?
TL; DR: 将此添加:allow_agent=False和/或添加look_for_keys=False到您的connect()呼叫中。
allow_agent=False
look_for_keys=False
connect()
通过对此进行一些研究,我得出的结论是,Paramiko SSH客户端的公共密钥身份验证并不总是适用于所有SSH服务器。我试图连接到Cisco ASA设备。错误日志的相关部分是:
DEBUG:paramiko.transport:Switch to new keys ... DEBUG:paramiko.transport:Trying SSH agent key 5d081d52f889f2c224606d6b2065606e DEBUG:paramiko.transport:userauth is OK DEBUG:paramiko.transport:Authentication type (publickey) not permitted. DEBUG:paramiko.transport:Allowed methods: ['password'] INFO:paramiko.transport:Disconnect (code 2): Protocol error: expected packet type 50, got 5
注意“尝试SSH代理密钥…”部分吗?我不想要那个,我想强制密码验证。在OpenSSH交互式客户端从公钥正常退回到密码的地方,paramiko则没有,强制它使用用户/通过身份验证为我解决了这个问题,它可能对您有用。
我不知道这是paramiko错误还是SSH服务器上的错误。与OpenSSH服务器通讯时,Paramiko确实会从公钥还原为密码或与键盘交互。