使用 Paramiko 从 SSH 服务器获取登录前横幅而不进行身份验证 [英] Fetch prelogin banner from SSH server using Paramiko without authenticating
问题描述
我正在尝试使用以下代码从服务器获取横幅.但结果总是说无",甚至认为横幅存在.我尝试过 Python 2 和 3、Paramiko 2.4 和 2.7.0,结果与无"相同.任何人都可以纠正/帮助我吗?
I am trying to fetch banner from sever using below code. But the result always says "None", even thought banner exists. I have tried with Python 2 and 3, Paramiko 2.4 and 2.7.0, same result as "None". Can anyone correct/help me?
代码基于:有没有办法使用paramiko和python来获取你连接的ssh服务器的banner?
使用 Banner
指令在 sshd_config
中配置横幅.
The banner is configured in sshd_config
using Banner
directive.
# !/usr/bin/python
import paramiko
def grab_banner(ip_address, port):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(ip_address, port=port, username='username',
password='bad-password-on-purpose')
except:
return client._transport.get_banner()
if __name__ == '__main__':
print grab_banner('192.168.1.26', 22)
谢谢
推荐答案
总的来说,我相信您的代码应该可以工作.但是由于密码认证失败后,Paramiko 尝试了各种其他的认证方法都徒劳无功,进一步的尝试将丢弃横幅(这在我看来像是 Paramiko 中的一个错误).
In general I believe that your code should work. But as after failed password authentication, Paramiko tries in vain various other authentication methods, the further attempts will discard the banner (it looks like a bug in Paramiko to me).
通过在 SSHClient.connect
:
Prevent that by setting look_for_keys
and allow_agent
in SSHClient.connect
:
try:
client.connect(ip_address, port=port, username='username',
password='bad-password-on-purpose',
look_for_keys=False, allow_agent=False)
except:
return client._transport.get_banner()
以下是 Paramiko 的修复程序,它允许在没有上述解决方法的情况下检索横幅:
https://github.com/paramiko/paramiko/pull/438
这篇关于使用 Paramiko 从 SSH 服务器获取登录前横幅而不进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!