【问题标题】:beginner, netmiko from bash awk sed question初学者,来自 bash awk sed 问题的 netmiko
【发布时间】: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


【解决方案1】:

您可能应该查看 network-tools 库,其中包含一小部分使用 Netmiko 的命令行工具。其中一个工具是 netmiko-grep。见这里:

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

【讨论】:

  • 太棒了!谢谢。我实际上通过将整个事情解析成一个列表,然后进行列表处理来解决这个特殊问题:
  • 好吧,对不起,我打算发布代码 sn-p,但显然不是。还是谢谢你。
猜你喜欢
  • 1970-01-01
  • 2020-01-04
  • 2020-11-23
  • 2015-03-16
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多