beginner, netmiko from bash awk sed question












-3














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.










share|improve this question






















  • What is your out_version like? What is the expected output?
    – Wiktor Stribiżew
    Nov 8 '18 at 8:30
















-3














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.










share|improve this question






















  • What is your out_version like? What is the expected output?
    – Wiktor Stribiżew
    Nov 8 '18 at 8:30














-3












-3








-3







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.










share|improve this question













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.







python sed grep paramiko cisco






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 '18 at 6:12









user10622012

31




31












  • What is your out_version like? What is the expected output?
    – Wiktor Stribiżew
    Nov 8 '18 at 8:30


















  • What is your out_version like? What is the expected output?
    – Wiktor Stribiżew
    Nov 8 '18 at 8:30
















What is your out_version like? What is the expected output?
– Wiktor Stribiżew
Nov 8 '18 at 8:30




What is your out_version like? What is the expected output?
– Wiktor Stribiżew
Nov 8 '18 at 8:30












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer





















  • Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
    – user10622012
    Nov 16 '18 at 21:28










  • Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
    – user10622012
    Nov 16 '18 at 21:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202350%2fbeginner-netmiko-from-bash-awk-sed-question%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer





















  • Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
    – user10622012
    Nov 16 '18 at 21:28










  • Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
    – user10622012
    Nov 16 '18 at 21:29
















0














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






share|improve this answer





















  • Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
    – user10622012
    Nov 16 '18 at 21:28










  • Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
    – user10622012
    Nov 16 '18 at 21:29














0












0








0






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 4:46









Kirk Byers

1665




1665












  • Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
    – user10622012
    Nov 16 '18 at 21:28










  • Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
    – user10622012
    Nov 16 '18 at 21:29


















  • Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
    – user10622012
    Nov 16 '18 at 21:28










  • Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
    – user10622012
    Nov 16 '18 at 21:29
















Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
– user10622012
Nov 16 '18 at 21:28




Excellent! thank you. I actually resolved this particular problem by parsing the whole thing into a list, and then doing list processing:
– user10622012
Nov 16 '18 at 21:28












Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
– user10622012
Nov 16 '18 at 21:29




Well, sorry, I was going to post the code snippet, but apparently not. Thank you anyway.
– user10622012
Nov 16 '18 at 21:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202350%2fbeginner-netmiko-from-bash-awk-sed-question%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ