Free CCNP 350-401 ENCOR Complete Course: 6.2 JSON & Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- In the previous video in this series, which I've linked here and below, I showed you how to interpret JSON data. I showed you a JSON object as well as a JSON array. I also showed you how to use JSON on a Nexus device. So, we could use a command such as show interface Vlan100 and pipe that to JSON or pipe that to JSON pretty. In this video what we're going to do is use Python on-box, so, Python running on the Nexus device, and I'm gonna show you how you can use CLI commands within Python running on the Nexus device. So, in this video we start in slowly, I wanna introduce some Python concepts to you, I wanna show you how you can run Python directly on a Nexus device and run show commands and other commands directly within Python running on the Nexus device. In the next video I'm gonna create a Python script that runs locally within Ubuntu on my Mac and then I'm gonna SSH to the Nexus device and run commands on the Nexus device and extract the data from the Nexus device using Python. So, this is a step by step process. We've looked a little bit at JSON, now we're going to run a Python script on-box and then in the next video, I'll show you how to use Python locally on my laptop. Now, please do me a favor. If you're enjoying the CC and P course consider subscribing to my YouTube channel, consider liking this video and clicking on the bell to get notifications. That really does help me with the YouTube robots. Okay, let's get started. (techno music) Okay, so I'm going to copy this bit of Python code. Go back to the Nexus box. At the moment I'm not running Python. I can run standard Cisco IOS commands such as show IP interface brief. Or show version. So, these are standard commands. Show run, et cetera. But what I'm gonna do now is run Python. This is on-box Python. I'm running Python directly on this Nexus box. And I'm gonna teach you a bit of Python now. The first statement we're gonna use is from cli import *. So, from cli import * or import everything. To be able to use CLI commands from within Python, running on this Nexus box we need to import CLI. We need to type that command to allow us to basically run any CLI command. This is called a variable, cmd1. You can call this anything you wanted to. The command that we're going to run is show IP interface brief JSON pretty. So, we're basically using the command that I've previously demonstrated. So, I'll quit out of Python just to show you that again. Show IP interface brief. JSON pretty. That's me typing the command manually and here's our output once again. JSON array. But I'm going to run Python on-box. The version of Python here is 2.7.11. Now for the real world you really wanna learn Python 3, if you're new to Python. But, here's a real world example of why sometimes it's well worth knowing about Python 2.7 because you'll still encounter it. Devices like this Nexus are running Python 2.7, not Python 3. So, once again I need to type from cli import everything and then I'm going to paste this statement that says this command is gonna be stored in this variable. So, if I type cmd1, that's the command that's stored in this variable. So, here I'm going to store the output of that command in another variable. Basically the output variable stores the output of this command, which I'm running using the CLI function. So, if I look at output1 now, there's the output. There's the information stored in that variable. But, it's not very easy to read. So, I'm gonna type print(output1) and there you go. There's the output of that show command in JSON formatting running within Python. Now, if you're new to Python you might say, but, David, I could just type the command manually. Sure you could. But I'm showing you this step by step. In a moment I'm going to show you how to run a Python script from my local computer that SSHs into this device and runs one or two commands and then extracts information from that device and shows it on my local computer. So, rather than me having to manually SSH or log in to the device, type a command, see the output and then interpret it, I can get a Python script to automatically do that. In this example I'm doing it for one device, but remember, I could do this with a hundred devices or 500 devices. Automation and network programmability make a lot of sense when you have many devices. Not necessarily always when you only have one device. But you need to work in building blocks. So, basically, this little script of four lines and I could've made it short if I wanted to, but I don't wanna make it complex, I wanna make it easy to read. Short script of four lines has given me the output of show IP interface brief using JSON pretty format. What we can do now is extract specific information from the table interface dictionary and the row interface dictionary. This is what's called a dictionary. We can query this information. So, as an example, I could query the IP address of a specific interface if I wanted to. So, let's do that by updating our script. So, this information is the same, but what I'm gonna do now is load that information using this command so that Python knows that this information is in JSON format. At the moment it's currently a string. So, if I use the type command and look at output one it's a string and we can't query that data format as easily as JSON, so it's gonna make much more sense to change this into a dictionary using JSON formatting so that we can query the information much more easily. Remember the captcha. Captcha is a way to stop machines. It's easier for humans but not easy for machines. Here we wanna put it in a format that machines understand. And again, if you know Python you could do this more simply than I am. It's complaining about my script and that's because I forgot this command. Previously I had this as output1 and I actually changed the script. So, that's bad. So, let's do that again. So, to make sure that you're not confused I'll simply start this from the beginning again. So, I'll quit out of Python and let's start again. So, Python 2.7. These commands you've already seen. So, let's copy those commands and I'll paste them into Python which is running on that Nexus device. This statement runs this command using the CLI function and stores it in json_data rather than in output one. Which is the name I used previously. So, that json_data now contains the information but the problem with that is it's a string. So, notice type string. So, what I wanna do is I wanna load that data using json.loads. So, I'm loading this information using this function and I'm storing it in this variable. Now, json_final has that information. I actually wanted to use type here so that we can see what type this is. What you can see is it's now a dictionary. Dictionaries in Python use key value pairs. So, I can query, very easily, within the dictionary for specific piece of information rather than trying to parse or go through a string and look for specific characters and then find the answer. That's very difficult to do for a machine. Very easy for us as humans. But, once again, it's the idea of show version is just text, easy for a human to read that, not for a machine. If I put that into JSON format, it's much easier to find information. So, as an example, what I can do now is print the prefix of the first interface. So, going up through this information, not easy to read here, but notice the first interface is Vlan100, first IP address is this. So, if I paste that command in, we've just retrieved the IP address or prefix of the first interface. Again, that's not very easy but don't lose sight of the fact that I'm going to show you a script in a moment that automates all of this. Just trying to show you step by step. So, again, just to make sure that you understand the script we are importing CLI so we can use CLI commands within Python. We're importing JSON so that we can use this command which allows us to take the string data and basically convert it to a dictionary so we have JSON formatting. This is the command that we ran on the switch, show IP interface brief JSON pretty. And then I've now queried that data which is stored in what's called a dictionary. A dictionary in Python is an unordered key value pair. So, I can simply look for a value in the dictionary and easily retrieve a piece of information. So, again, I've retrieved the IP address of the first interface starting at zero. Now, if want the IP address of the second interface I could change that to one. Third interface. Fourth interface. Now, I'm doing that manually. That's not the idea with automation. The idea with automation is to automate this. But I'm bolding or iterating is the term we use in programming, I'm iterating the process, improving what I'm doing and hopefully teaching you along the way. But notice how cool this is. We're running Python on a Cisco Nexus device. So, I simply started Python on the Nexus device. Once again, I'll exit out of Python. I simply typed Python to start Python on the Nexus device and now I'm running Python scripts directly on the Nexus device. I'm gonna show you some other videos in this course showing you how to do sort of Linux based commands and Python on XE. This is Nexus, IOS XE also supports Python so you can run Python directly on an IOS XE device. Classic or monolithic IOS doesn't support Python but you can still use Linux commands on classic IOS. Notice where the world is going, notice how it's important for you to learn Python, network automation, Linux, these kind of new skills is where the industry's moving. (techno music)
Info
Channel: David Bombal
Views: 28,691
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: 8_R0gmpM8eI
Channel Id: undefined
Length: 11min 57sec (717 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.