2

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

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this answer! It worked perfectly. I had my "banner grabber" script working initially but it just... stopped... one day. Your solution worked correctly. Hopefully they merge that pull request!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.