使用python paramiko时如何过滤ssh横幅 [英] How to filter ssh banner when using python paramiko

查看:86
本文介绍了使用python paramiko时如何过滤ssh横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

他人的源代码

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

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

类似于下面的内容

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

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.

谢谢

推荐答案

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

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

尽管此解决方法解决了我的问题,但我很高兴知道 paramiko 是否有本机替代方案.

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

这篇关于使用python paramiko时如何过滤ssh横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆