Welcome to EveDumps This is a free, Complete course for the CCNP 350-401 ENCOR Exam Training. 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 article.

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 article 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 article. 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 article I'll, Show you how to use Python locally. On my laptop Now, Please do me a favor If you're enjoying the CCNP course consider subscribing to my EveDumps channel, Consider liking this article and clicking on the bell to get notifications. That really does help me with the EveDumps robots. Let's get started (techno, Music), 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 2711. 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 27, Because you'll still encounter it. Devices like this Nexus are running Python 27, 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.

Cisco DevNet 200-901 Dumps

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 27.

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 jsonloads. 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 articles 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!

We would be discussing the ways for clearing. I would suggest you focus on the below-mentioned resources and also check out the Cisco DevNet 200-901 Dumps offered at the EveDumps, they are the best when it comes to Certifications Vendor.

Comments

Leave a comment