如何在使用Python Paramiko时过滤SSH横幅
我想知道怎么让paramiko过滤掉ssh的欢迎信息。
当我执行一个命令时,欢迎信息的内容会和结果一起显示出来。
像下面这样
pprint(connection.execute('date'))
#['Welcome to my shell\n', 'Fri Jul 11 15:07:11 HKT 2014\n']
我尝试过的方法
self._transport.get_banner() # always return none
我查看了一下paramiko的源代码。里面有处理欢迎信息的代码。但我的问题是,怎么才能确保这些代码能顺利完成我的任务。
谢谢
1 个回答
2
这里有一个解决过滤横幅内容的方法
# 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
虽然这个方法解决了我的问题,但我还是希望知道在paramiko中有没有更直接的替代方案。