Python 初学者,来自bash awk的netmiko sed问题
问候 我知道python不是shell。不过,我还是以这个项目为借口来提升python的性能。但是我被卡住了。代码如下,带有嵌入式问题 如果有关系的话,我正在使用Python3.0版本的jupyter笔记本电脑在centos7和cisco 3650交换机上工作Python 初学者,来自bash awk的netmiko sed问题,python,sed,grep,paramiko,cisco,Python,Sed,Grep,Paramiko,Cisco,问候 我知道python不是shell。不过,我还是以这个项目为借口来提升python的性能。但是我被卡住了。代码如下,带有嵌入式问题 如果有关系的话,我正在使用Python3.0版本的jupyter笔记本电脑在centos7和cisco 3650交换机上工作 import sys,re import os import io import subprocess from netmiko import ConnectHandler # trying to replicate this: #
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,substring,每一种都越来越难看。甚至将结果剥离并写入一个文件,然后将grep/sed管道的输出读回。(那太难看了)。一定有更简单的方法。有人能给我指出正确的方向吗
非常感谢。您可能应该查看网络工具库,其中有一小部分使用Netmiko的命令行工具。其中一个工具是netmiko grep。请看这里:
你的
版本是什么样的?预期产量是多少?太好了!非常感谢。实际上,我解决了这个特殊问题,将整个问题解析成一个列表,然后进行列表处理:哦,对不起,我本来打算发布代码片段,但显然没有。无论如何,谢谢你。