问题描述
我正在学习paramiko的基础知识,为此,我建立了一个基础实验室,将Ubuntu VM连接到EVE-ng模拟的路由器。
第一步是通过ssh-Keygen在客户端中生成密钥对
接下来,我使用以下命令将公钥加载到远程服务器(Cisco路由器):
ip ssh pubkey-chain
username administrator
key-hash ssh-rsa 97D0E9B5630D05D78EA9531053124BFF
此后,我立即能够从Ubuntu VM登录到Cisco路由器:
$ ssh administrator@192.168.1.1
7206_1.rt#
然后,从同一客户端,我启动了Python Shell会话,并尝试使用Paramiko建立SSH会话:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.1',username='administrator',password='password',key_filename= '/home/administrator/.ssh/id_rsa.pub')
但是这次我遇到了以下异常:
Exception: Illegal info request from server
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/paramiko/transport.py",line 2109,in run
handler(self.auth_handler,m)
File "/usr/local/lib/python3.8/dist-packages/paramiko/auth_handler.py",line 661,in _parse_userauth_info_request
raise SSHException("Illegal info request from server")
paramiko.ssh_exception.SSHException: Illegal info request from server
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
File "/usr/local/lib/python3.8/dist-packages/paramiko/client.py",line 435,in connect
self._auth(
File "/usr/local/lib/python3.8/dist-packages/paramiko/client.py",line 764,in _auth
raise saved_exception
File "/usr/local/lib/python3.8/dist-packages/paramiko/client.py",line 751,in _auth
self._transport.auth_password(username,password)
File "/usr/local/lib/python3.8/dist-packages/paramiko/transport.py",line 1498,in auth_password
raise SSHException("No existing session")
paramiko.ssh_exception.SSHException: No existing session
远程路由器SSH调试显示身份验证失败:
*Aug 16 01:18:07.295: SSH2 0: MAC compared for #5 :ok
*Aug 16 01:18:07.299: SSH2 0: input: padlength 16 bytes
*Aug 16 01:18:07.299: SSH2 0: Using method = publickey
*Aug 16 01:18:07.307: SSH2 0: send:packet of length 432 (length also includes padlen of 4)
*Aug 16 01:18:07.307: SSH2 0: computed MAC for sequence no.#5 type 60
*Aug 16 01:18:07.311: SSH2 0: Authenticating 'administrator' with method: publickey
*Aug 16 01:18:07.327: SSH2 0: SSH ERROR closing the connection
*Aug 16 01:18:07.331: SSH2 0: send:packet of length 80 (length also includes padlen of 15)
*Aug 16 01:18:07.331: SSH2 0: computed MAC for sequence no.#6 type 1
*Aug 16 01:18:07.335: SSH2 0: Pubkey Authentication Failed for user administrator
*Aug 16 01:18:07.335: SSH0: password authentication Failed for administrator
这时我无法确定问题出在服务器还是路由器中,因为在不使用Paramiko的情况下直接从服务器连接到路由器时,一切都正常。
谢谢。
解决方法
好吧,看起来默认情况下,Paramiko在〜/ .ssh /中搜索可发现的私钥文件,如果尝试连接到另一台服务器就可以了,但是由于它试图访问路由器,因此需要通过设置禁用此功能 look_for_keys 设置为False。这解决了这个问题(只要这不是生产环境),这就是我的情况。
,通过/home/administrator/.ssh/id_rsa.pub上的公钥完成身份验证
不太完全:这是通过键入时使用的 local 用户的 private 键完成的:
ssh administrator@192.168.1.1
'administrator
'是用于在远程服务器192.168.1.1
将使用~administrator/.ssh/authorized_keys
(同样在远程计算机上)在远程进行身份验证,以检查本地~/.ssh/id_rsa.pub
的公钥是否已在远程{{1 }}。
您的本地帐户也可能是'~administrator/.ssh/authorized_keys
',但是执行Python Shell时,该本地帐户可能并不相同。
看到时
administrator
SSH谈论的是远程服务器上的远程“ Authenticating 'administrator' with method: publickey
”帐户,无论您使用的是本地用户帐户。