Paramiko捕获命令输出
我遇到了一个困扰我几天的问题。我在Python
2.7.10中使用了Paramiko模块,我想向Brocade路由器发出多个命令,但仅返回给定命令之一的输出,如下所示:
#!/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)
如果要打印完整的输出,它将包含发布到路由器的所有内容,但是我只想查看showinterfaces ethernet 0/1\n
命令的输出。
谁能解决这个问题?
我想问的最后一件事。我想过滤output
变量并检查是否出现诸如“ up”或“
down”之类的字符串,但是由于输出中的所有内容似乎都在新行上,所以我似乎无法使其正常工作?
例如:
如果我output
在for循环中遍历变量,则将获得变量中的所有字符,如下所示:
for line in output:
print(line)
我得到这样的输出:
Ť
Ë
[R
米
一世
ñ
一种
升
升
Ë
ñ
G
Ť
H
0
可以解决吗?
再次,
在此先感谢您的帮助。
最好的祝福,
亚伦C.