python - Python paramiko.ssh_exception.SSHException:没有现有会话
问题描述
此站点的示例代码(https://www.kite.com/python/answers/how-to-ssh-using-paramiko-in-python)
host = "test.rebex.net"
port = 22
username = "demo"
password = "password"
command = "ls"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)
应该产生这个输出。
['aspnet_client\n', 'pub\n', 'readme.txt\n']
凭证在此演示站点上运行良好
wolf@linux:~$ sshpass -p password ssh demo@test.rebex.net
Welcome to Rebex Virtual Shell!
For a list of supported commands, type 'help'.
demo@ETNA:/$
demo@ETNA:/$ ls
aspnet_client
pub
readme.txt
demo@ETNA:/$
但是,代码没有按预期工作。我在ssh.connect(host, port, username, password)
下线后立即收到错误。
>>> import paramiko
>>> host = "test.rebex.net"
>>> username = "demo"
>>> password = "password"
>>> port = 22
>>>
>>> command = "ls"
>>>
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(host, port, username, password)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/wolf/.local/lib/python3.8/site-packages/paramiko/client.py", line 435, in connect
self._auth(
File "/home/wolf/.local/lib/python3.8/site-packages/paramiko/client.py", line 764, in _auth
raise saved_exception
File "/home/wolf/.local/lib/python3.8/site-packages/paramiko/client.py", line 751, in _auth
self._transport.auth_password(username, password)
File "/home/wolf/.local/lib/python3.8/site-packages/paramiko/transport.py", line 1498, in auth_password
raise SSHException("No existing session")
paramiko.ssh_exception.SSHException: No existing session
>>>
让我知道如何使它起作用。我只想要一个简单的 SSH 连接代码。
更新
>>> ssh.connect(host, port, username, password, look_for_keys=False, allow_agent=False)
>>>
解决方案
该代码对我有用。
当我在 Pageant 中加载了一个密钥时,我也收到了错误auth_password
(尽管不同 - “身份验证失败” )。在 Paramiko 尝试使用 Pageant 密钥后,Rebex 测试服务器似乎关闭了会话。然后连续的密码认证失败。我想在你的情况下这将是一个类似的问题。不同的错误消息可能只是由于时间差异。
starting thread (client mode): 0x33629f0
Local version/idstring: SSH-2.0-paramiko_2.6.0
Remote version/idstring: SSH-2.0-RebexSSH_5.0.7448.0
Connected (version 2.0, client RebexSSH_5.0.7448.0)
kex algos:['curve25519-sha256', 'curve25519-sha256@libssh.org', 'ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group15-sha512', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group-exchange-sha1'] server key:['ssh-ed25519', 'ecdsa-sha2-nistp256', 'rsa-sha2-512', 'ssh-rsa-sha256@ssh.com', 'rsa-sha2-256', 'ssh-rsa'] client encrypt:['aes256-gcm@openssh.com', 'aes128-gcm@openssh.com', 'aes256-ctr', 'aes256-cbc', 'aes192-ctr', 'aes192-cbc', 'aes128-ctr', 'aes128-cbc', 'chacha20-poly1305@openssh.com', 'twofish256-ctr', 'twofish192-ctr', 'twofish128-ctr', 'twofish256-cbc', 'twofish192-cbc', 'twofish128-cbc', 'twofish-cbc', '3des-ctr', '3des-cbc'] server encrypt:['aes256-gcm@openssh.com', 'aes128-gcm@openssh.com', 'aes256-ctr', 'aes256-cbc', 'aes192-ctr', 'aes192-cbc', 'aes128-ctr', 'aes128-cbc', 'chacha20-poly1305@openssh.com', 'twofish256-ctr', 'twofish192-ctr', 'twofish128-ctr', 'twofish256-cbc', 'twofish192-cbc', 'twofish128-cbc', 'twofish-cbc', '3des-ctr', '3des-cbc'] client mac:['hmac-sha2-512-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512', 'hmac-sha2-256', 'hmac-sha1', 'hmac-sha1-96'] server mac:['hmac-sha2-512-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512', 'hmac-sha2-256', 'hmac-sha1', 'hmac-sha1-96'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
Kex agreed: curve25519-sha256@libssh.org
HostKey agreed: ssh-ed25519
Cipher agreed: aes128-ctr
MAC agreed: hmac-sha2-256
Compression agreed: none
kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
Switch to new keys ...
Adding ssh-ed25519 host key for test.rebex.net: b'e7e445c6b8e4bbd868892786fd0158f0'
Trying SSH agent key b'3f3ae3e3b0e0ef4e5b4bfe93613557d4'
userauth is OK
Authentication (publickey) failed.
Disconnect (code 2): Authentication is already in progress.
如果您想避免尝试 Pageant,请使用以下allow_agent
参数SSHClient.connect
:
ssh.connect(host, port, username, password, allow_agent=False)
强制性警告:请勿使用-这样做AutoAddPolicy
会失去对MITM 攻击的保护。有关正确的解决方案,请参阅Paramiko "Unknown Server"。
推荐阅读
- mysql - 如何在 MySQL 中加入 3 个表
- c++ - 由于重定位错误,编译失败
- vue.js - Vue.js - 从孙插槽组件发射没有将 $emit 事件冒泡到父级
- reactjs - 如何从反应原生的正常功能中获取上下文值?(没有 useContext 或类组件)
- c# - 如何访问 C# 中的继承方法?
- algorithm - 基于球员得分的比赛调度算法
- reactjs - 出现错误**错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。**
- python - TKinter:创建一个带有多选的下拉列表框
- c - C只需将新行添加到导致printf分段错误的NULL点
- javascript - How to filter array in javascript