Skip to main content
 首页 » 编程设计

python之使用python paramiko时如何过滤ssh横幅

2024年10月25日9myhome

我想知道如何让 paramiko 过滤 ssh 横幅。

Source Code from others

当我执行命令时,横幅的内容与结果一起出现。

类似下面的内容

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 源代码。内部有解析横幅的代码。但问题是我怎样才能确保他们做好我的工作。

谢谢

请您参考如下方法:

这是过滤横幅内容的解决方法

# 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 上是否有原生替代方案。