来自 bash awk sed 问题的初学者,netmiko

beginner, netmiko from bash awk sed question

您好。

我知道 python 不是 shell。不过,我正在使用这个项目作为提升 python 的借口。但我卡住了。代码如下,带有嵌入式问题

如果重要的话,我正在 python 3.something 的 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,子字符串,每一种都越来越难看。甚至 shell 输出并用结果写一个文件,然后读回 grep/sed 管道的输出。(这很丑陋)。必须有一个更简单的方法。有人能帮我指出正确的方向吗?

非常感谢。

您可能应该查看网络工具库,它有一小组使用 Netmiko 的命令行工具。其中一个工具是 netmiko-grep。看这里:

https://pynet.twb-tech.com/blog/automation/netmiko-grep.html