问候。
我了解python不是shell。我以这个项目为借口,虽然可以增强python的功能。但是我被困住了。代码如下,带有嵌入式问题
如果重要的话,我正在使用python 3的jupyter笔记本工作,在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()
我尝试了许多方法,popen,子字符串,每种方法都越来越难看。甚至脱壳并写入结果文件,然后读回grep / sed管道的输出。(这很丑陋)。必须有一个更简单的方法。有人能让我指出正确的方向吗?
非常感谢。
答案 0 :(得分:0)
您可能应该看一下网络工具库,该库具有一小组使用Netmiko的命令行工具。这些工具之一是netmiko-grep。看到这里:
https://pynet.twb-tech.com/blog/automation/netmiko-grep.html