Fetch prelogin banner from SSH server using Paramiko without authenticating

青春壹個敷衍的年華 提交于 2020-06-26 14:14:28

问题


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?

The code is based on: Is there a way using paramiko and python to get the banner of the ssh server you connected to?

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)

Thanks


回答1:


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).

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()

Here is a fix for Paramiko that allows retrieving the banner without the above workaround:
https://github.com/paramiko/paramiko/pull/438



来源:https://stackoverflow.com/questions/62370184/fetch-prelogin-banner-from-ssh-server-using-paramiko-without-authenticating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!