【发布时间】:2019-04-11 15:26:33
【问题描述】:
问候。
我知道 python 不是 shell。不过,我以这个项目为借口来提升对 python 的了解。但我被困住了。代码如下,嵌入问题
如果重要的话,我正在 python 3 中的 jupyter notebook 中工作。centos7 和 cisco 3650 交换机上的东西。
import sys,re
import os
import io
import subprocess
from netmiko import ConnectHandler
# trying to replicate this:
# ssh -q super@cisco1 "show ver" | grep -i "Cisco IOS Software" | sed -n -e 's/^.*Version //p' | sed -n -e 's/\,.*//p'
# [output is, in this case]
# 16.3.5b
platform = 'cisco_ios'
host = 'cisco1'
username = 'super'
password = 'sillypassword'
device= ConnectHandler(device_type=platform, ip=host, username=username, password=password)
out_version=device.send_command('show version')
# here's where I would do a
# grep -i "Cisco IOS Software" | sed -n -e 's/^.*Version //p' | sed -n -e 's/\,.*//p'
#
# However I understand that python doesn't 'pipe' like shell does so I need
# embedded loops (right?). But I don't understand how to do
# embedded loops with a stream of text that will be coming out
# of the device.send_command, and then save -that- into the
# variable out_version.
device.disconnect()
我尝试了很多方法,popens,子字符串,每一个都越来越难看。甚至用结果写出一个文件,然后读回 grep/sed 管道的输出。(这很难看)。必须有一个更简单的方法。有人可以让我指出正确的方向吗?
非常感谢。
【问题讨论】:
-
你的
out_version是什么样的?预期的输出是什么?
标签: python sed grep paramiko cisco