Greets.
I understand that python isn't shell. I'm using this project as an excuse to get a boost into python though. But I'm stuck. Code is below, with embedded questions
If it matters I'm working in a jupyter notebook in python 3.something on centos7 and cisco 3650 switches.
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()
I've tried a number of approaches, popens, substrings, each increasingly uglier. Even shelling out and writing a file with the result, then reading the output of the grep/sed pipe back in. (that was ugly). There's got to be a simpler way. Can someone get me pointed in the right direction?
Thanks much.
You probably should look into the network-tools library which has a small set of command-line tools that use Netmiko. One of those tools is netmiko-grep. See here:
https://pynet.twb-tech.com/blog/automation/netmiko-grep.html