Paramiko 捕获命令输出 [英] Paramiko capturing command output

查看:103
本文介绍了Paramiko 捕获命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题困扰了我好几天.我在 Python 2.7.10 中使用 Paramiko 模块,我想向 Brocade 路由器发出多个命令,但只返回给定命令之一的输出,如下所示:

I have an issue that has been giving me a headache for a few days. I am using the Paramiko module with Python 2.7.10 and I'd like to issue multiple commands to a Brocade router, but only return output from one of the given commands like so:

#!/usr/bin/env python
import paramiko, time

router = 'r1.test.example.com'
password = 'password'
username = 'testuser'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(router, username=username, password=password)
print('Successfully connected to %s' % router)

remote_conn = ssh.invoke_shell()
output = remote_conn.recv(1000)

# Disable paging on Brocade.
remote_conn.send('terminal length 0\n')
# Check interface status.
remote_conn.send('show interfaces ethernet 0/1\n') # I only want output from this command.
time.sleep(2)
output = remote_conn.recv(5000)
print(output)

如果我要打印完整输出,它将包含发送给路由器的所有内容,但我只想查看 show interfaces ethernet 0/1\n 命令的输出.

If I were to print the full output it would contain everything issued to the router, but I only want to see output from the show interfaces ethernet 0/1\n command.

有人可以帮忙解决这个问题吗?

Can anyone help with this issue?

我想问的最后一件事.我想过滤 output 变量并检查是否出现up"或down"等字符串,但我似乎无法让它工作,因为输出中的所有内容似乎都在换行?

One final thing I would like to ask. I want to filter through the output variable and check for occurrences of strings like "up" or "down", but I can't seem to get it to work because everything in the output appears to be on new lines?

例如:

如果我在 for 循环中遍历 output 变量,我会得到变量中的所有字符,如下所示:

If I iterate over the output variable in a for loop I get all of the characters in the variable like so:

for line in output:
    print(line)

我得到这样的输出:

t

e

r

n

一个

l

l

e

n

g

t

h

0

有什么办法可以解决这个问题吗?

Any way around this?

再说一遍

在此先感谢您的帮助.

最好的问候,

亚伦 C.

推荐答案

对于你的第二个问题:虽然我不是 paramiko 的专家,但我看到了函数 recv,根据文档,返回一个字符串.如果对字符串应用 for 循环,您将得到字符(而不是人们可能期望的行).换行是由您使用打印功能引起的,如本页第 6.3 段所述.

For your second question: Though I am not specialist of paramiko, I see that function recv, according to the doc, returns a string. If you apply a for loop on a string, you will get characters (and not lines as one might perhaps expect). The newline is caused by your use of the print function as explained on this page, at paragraph 6.3.

我还没有研究过 paramiko 建议做什么.但是为什么不将完整的字符串视为单个实体呢?例如,您可以检查up"是否存在:

I haven't studied what paramiko suggests to do. But why don't you treat the full string as a single entity? For example, you could check the presence of "up" as:

if "up" in output:

或者,如果这更适合您的需求,您可以 将字符串分成几行 然后做任何你想做的测试:

Or, if that suits your needs better, you could split the string into lines and then do whatever test you want to do:

for line in output.split('\n'): 

这篇关于Paramiko 捕获命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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