Free CCNP 350-401 ENCOR Complete Course: 6.2 JSON, Ubuntu and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- In the previous video in this course which I've linked here and below, I showed you how to run Python On-Box. So, run Python on a Nexus device and use CLI commands running within Python on the Nexus device. In this video, we're going to create a Python script and run it locally on my laptop. And SSH to the Nexus device and extracted data from the Nexus device. I wanna show you why it's powerful, using formatted data. So, using data in JSON format, rather than just in a string. It makes extracting information so much easier. Okay, so let's get started, and I'm gonna show you how to run Python locally, SSH to a Nexus device, and extract information from that Nexus device. ♪ Let's go. ♪ (upbeat techno music) - Okay so, I'm tired of using Python on the box now, what I'm gonna do is start an Ubuntu VM on my local Mac. I'm using Linux. I strongly suggest, just like Python, that you learn Linux. Linux is one of those core skills, you're gonna use it everywhere. So make sure that you learn Linux. So, here I'm going to open up a terminal. This is on my local computer, it's not in DevNet. This is local. And what I'm gonna do is install python3-pip and netmiko. Basically, what we're doing is we're installing python3 on the Ubuntu PC. And we're installing netmiko, which is a great way to connect to network devices. Rather than you writing all of the low-level code to SSH to a network device and then send commands to a network device, and then interpret the output, netmiko is your first step in automation. Because the developer of netmiko has done a lot of the hard work already, and you can just leverage that code in your Python scripts. One of the advantages of Python is there are a whole bunch of libraries available, including netmiko, including pyATS, including Nornir, that make our lives as network engineers a lot simpler. Don't reinvent the wheel, use the code and the hard work that others have done. And import those libraries into your code, and just leverage them. Don't reinvent the wheel. Now I've already installed these commands on my Ubuntu PC. But I'll paste them here just to show you the process. So, sudo apt install python3-pip And then I'm gonna install netmiko. Once again, I have previously installed the software. So, it should all be good. Just in case you want to know what release I'm using, I'm using Ubuntu 18.04.3 LTS. So this command, lsb release -a shows me the version of Ubuntu that I'm using. I'm just using one of the later releases of Ubuntu. So, I'll clear the screen. And now let's create a script. So, here's my script, I'm gonna copy it into that Ubuntu computer, and then I'll show you how to use it. I'm gonna create a script called python1.py. Not a good name, but it's okay for this demonstration. And I'm gonna paste that code in. This is importing the library netmiko. So, I'm gonna basically leverage the power of netmiko to connect to my Nexus device. Here's the username, here's the password, here's the port. Now, I know a lot of people are gonna complain, so I'll just say it right now. You don't have to store the credentials in your script. You could prompt for them if you wanted to, I'm just gonna store it here to keep it simple. It's a lab anyway, so it doesn't matter. I just want to show you how simple it is to extract information from a remote router. This router is running probably in the States somewhere, I don't know. It's hosted by DevNet. And I'm gonna pull back The show IP interface brief output, and display it on my computer. So, this is an example of a dictionary in Python. Notice the formatting. Does that look familiar? Hopefully that looks familiar. But it's a little bit different here to JSON, because in JSON formatting, we don't use single inverted commas. Basically, this is a dictionary storing the information that we need to connect to the router. This command allows me to connect to the router, and then this allows me to send a command to the router. In this case, 'show IP interface brief'. We store that in a variable, and then we print the results out. So, I'll save that script. I'm gonna use python3 to run that script. Okay, I'm getting an error, and this is one of the things you need to be careful with using Power Point. Notice the difference between that and this. Don't use Power Point, use a text editor. I typically use Sublime Text or Visual Studio Code. Notice that's gone green now. So, that looks better. I'll save that script. python3 python1.py. Script seems to be working now. So, this is SSHing to that device. And then running the command, and then hopefully we'll display the output, and there you go. We can see the output on that remote Nexus device on my Ubuntu computer. On the SSH session to the Nexus, show IP interface brief. There's the output of my direct SSH session. And here's the output using my Python script. Okay, so once again, I can simply type python3 python1.py. Python3 means I'm using Python 3. Python1 is the name of my script, and I can see the output. It's as simple as that to retrieve that data from that Nexus device. But let's try and do something more intelligent with the data. Okay, so this is just for us as humans; 'show IP interface brief' doesn't really help us. What I'm gonna do now however, is use the command 'JSON-pretty'. So, this line in yellow is actually the only change in the script. But I've put the full script in this Power Point Presentation so that you can download it. So let's use nano-nano once again, Nano is a simple text editor, Similar to Notepad on Windows if you like. So, I'm going to say nano python2. And what I'll do is paste my script in here. Here, pasted it correctly. So, I'll save that script. Only difference, once again is this. So, python3, run the second script. This should give us our JSON information. So, we should be able to get the JSON information back from the Nexus device. And, there you go. So, scrolling up, by using that single command, I've now been able to SSH to the device, retrieve the data, it's in JSON format. Notice double-inverted commas, we've got a colon, we've got a comma at the end. Make sure you recognize this formatting. I've said it, once again, many times. If you don't have a colon there, that would be an incorrect answer. If this is a semicolon, that would be an incorrect answer. It has to look like this. No comma at the end here. Okay, but once again, that's just given us some data. Perhaps not that useful. But, let's do something useful with the data now, and let's get the prefix from the data and the interface. So, basically I'm using a script now to pull back the first interface, and the first IP address. Don't worry, in the next script, I'm going to show you how to do that for multiple interfaces. So, now, L-S, we've got two python scripts, let's create a third python script. So, nano python3.py. I'll paste the script in. Stuff that we've added is this. So we pulling back specific interface information. Now I have made, done something bad here. I'm using double-inverted commas and single-inverted commas in Python. You should standardize on one. I'll fix that in the script that I give you. But that's fine. Python will accept that even though it's not great practice. But hopefully what will happen now, is this will connect to that Nexus device. Once again, SSHing across the internet, and it's gonna pull back first interface, first IP address, and then display that. And there we go. Okay, so just to prove it, here's my Nexus device once again. First interface, first IP address. There's that information. Now, you might so okay, but I want the state information. So, something like this. Remember we can simply edit that using our script. So, let's edit the script. So, rather than just seeing this data, what we said is we wanna get protocol state information. So, I'll copy that line, paste it here, and what we want is proto-state- is the name that they use. And I'll save the script. So, run it again. So, we should get the protocol status, we should get the interface name, we should get the IP address. This however will only be for one interface. Next script, I'll change that and make it better. Now, the formatting here's not great. I mean, that's just saying that you could print out information. This is the whole idea with iteration. So, rather than just saying that, you can say 'print interface.' And while I'm here, let's fix this, so I'd have to fix that for all the data to be consistent. I could do a find and replace, I suppose. That would actually make a lot more sense than doing it manually. But, the idea in Python is something called PEP8. PEP8 is talking about base practices. The moral of the story is be consistent. So, we can say print IP address. Print Protocol status. These are just examples, you could do what you wanted here. And this is the power of programming. You just change it the way you want it to changed. Okay, so it's got a problem, and this is my python2 history getting to me. In python3, we need to use a function for print. Print is a function. So, this need to be in brackets. Save that, run it again. Hopefully, this once again will connect to that Nexus device, and will tell us IP address, interface, and protocol status, and also show it in a little bit nicer format. So, here we go. There's the output of that script. Now, you might say but okay I want the information for all interfaces. So, I've got this script across two Power Point slides here. What I'm doing here, is creating a loop. So, we are using a four-loop here. What we're doing is getting the length of the rows. So, how many rows are there? So, if we jump back onto the device, so, this is SSHing directly to the device. Show IP interface brief JSON-pretty. ROW interface tells us how many interfaces there are. One, two, three, four, etc. 'Show IP interface brief' will just show us that, so here are all the interfaces. This length command allows me to work out how many interfaces there are, and then it just loops those commands for the number of interfaces that we've got. Okay, so first thing is. Netmiko configuration. So, L-S, we're now working on python script four. So, python4.py. I'll paste that in. That basically, once again just allows me to connect to the device. Didn't have enough space in Power Point. I've copied this into the VM now. So, the big difference now is, it's using the command, and then finding out how many interfaces there are, and then using a loop to basically run these commands multiple times for the number of interfaces that we have. So, clear the screen, python3 python4.py. This should hopefully now give us IP address and interface name for all the interfaces on that device. And to prove it, I'll create some extra interfaces and run the script again, and we should see that. So, there you go. All the interfaces on the device, and the IP addresses. So, if I show that side by side, they're all interfaces on the device and the IP addresses. But let's create another loop back. So, let's create a loop back to IP address 2.2.2.2 / 32 mask. Interface loop three. IP address, / 32 mask. Show IP interface brief. I've just created two additional loop-back interfaces. So, run that script again. This is, once again, connecting to that device in DevNet, and pulling back the information, and there you go. Additional loop-back interfaces are shown. Just to prove it once more, last interface loop-back four, IP address, quadruple four. Show IP interface brief. A new interface is created. Run the script again. We didn't previously have loop-back four in the output here, but hopefully we will now. And there we go. So, why are we using JSON? Much easier to work with this kind of data when you've got a python script. It's very difficult to find information if it's a string. If it's just data that's in an unformatted format. So, if it's just some kind of data that's like this capture, not easy for a machine to read that, we want to put it in a format that's easy for a machine to query and interpret. Your python scripts are gonna be so much easier if the data's in JSON. Trust me. Try and do that kind of stuff with just strings, and parsing the string, trying to find where the data is, and extracting a piece of data. It's very hard to do that with traditional mechanisms. So much easier if the data is correctly formatted using JSON or even XML or YML. Big idea for doing this, is to make it easy for machines to read. (upbeat techno music)
Info
Channel: David Bombal
Views: 25,862
Rating: undefined out of 5
Keywords: ccnp, encor, 350-401, python, json, ccna, ccnp encor, ccnp enterprise, ccnp training, new ccnp, ccnp study, network, nexus, cisco certifications, cisco training, cisco, cisco certified network professional, ccnp certification, encor 350-401, ccnp automation, ccie, ccie encor, cisco ccnp exam, ccie enterprise, cisco certification 2020, cisco ccnp, ccnp routing, cisco encor training, cisco encor exam, 2020 ccnp, ccnp review, cisco ccna, ccna study, ccna training, cisco encor test
Id: NvqD0wdQlYA
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.