How to filter ssh banner when using python paramiko

感情迁移 提交于 2020-01-04 11:13:28

问题


I am wondering how to ask paramiko to filter the ssh banner.

Source Code from others

When I execute a command, contents of the banner come with result together.

Something like below

pprint(connection.execute('date'))

#['Welcome to my shell\n', 'Fri Jul 11 15:07:11 HKT 2014\n']

Method I have tried

self._transport.get_banner() # always return none

I have checked out a bit of the paramiko source code. There are codes for parsing banner internally. But the question is how can I make sure of them to do my job well.

Thanks


回答1:


Here is a workaround for filtering banner contents

# Assume you are using the source code I posted
conn = Connection(HOST, USERNAME, PW)
banner = conn.execute('\n') # Fetch banner content
dateResult = conn.execute('date') # Target command result

# since banner is always a subset of dataResult, you can do the following
ret = dataResult.replace(banner, '')
print ret # ret is the answer you want

Although this workaround solve my problem, I will be happy to know if there is a native alternative on paramiko.



来源:https://stackoverflow.com/questions/24692174/how-to-filter-ssh-banner-when-using-python-paramiko

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