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

时间:2014-07-11 07:21:45

标签: python ssh paramiko

我想知道如何让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源代码。内部有解析横幅的代码。但问题是如何确保他们做好我的工作。

由于

1 个答案:

答案 0 :(得分: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是否有本地替代品。