问题
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