350-401 ENCOR – Cisco CCIE Enterprise – 6.0 Automation part 1
January 27, 2023

1. Python Basics 01

Six dot one the basics of Python now, Python is one of the languages that you will find that each and every department, either say it department or non It department, it is using, it is heavily used inside machine language, artificially, artificial intelligence and so many different, different places. Why? Why Python is this much popular? The importance of Python and why it is popular is because this is for example, free source portable. It’s very easy to learn. Actually not that very easy, but it’s easy to learn and it’s object oriented and functional. So overall thing is that it’s easy to learn and easy to run as well. It’s very lightweight and you can run it very easily. Now, when we are talking about Python, that means that we are going to use the skills of programming inside the networking as a network engineer, that’s the use case we have.

So that means my network devices, the modern network devices with modern operating system, they are understanding the script, they are understanding the Python language. So we can write the code like we used to write in terms of CEC plus Java, any other object oriented program we can go right? And then we can send the instruction in terms of programming. Now, one really important thing here is that why we have programming, although the devices, they can understand CLI and then GUI and then application programming. Why? The answer is this, that if you see the evolution of the network device configuration, you’ll find that CLI is good and stable.

And again, there are so many different form inside CLI as well. For example, we can do some sort of scripting. So for example, in Cisco routers, we can go and write TCL script. In other vendors, they are supporting some other interpreter level languages. Likewise in Cisco routers we can go and write some em script that will come by the end of this particular automation section. So things are there, but it’s small or restricted. It’s not that broader that Python is. So you can learn the Python and you can apply to any of the network devices irrespective of say for example load balances or the compute devices, or firewalling or security devices or router switches, etc. Etc.

Although here you can see that there is one con runs lower than lower level languages. Lower level languages are basically in hex code or they are very near to the machine. So obviously the compilation will be faster and then execution will be faster. There are other program as well if you compare to Python like TCL visible basic TCL is natively supported in Cisco devices JavaScript, PHP, Ruby, Cuba. But Python is more powerful because it has so many attributes or feature. Obviously other languages also have but they have the restriction. Someone is hard to learn, someone is hard to scale, someone is hard to compile. So overall you will find that Python is easy or at least easier than the other program to use. Now, whenever we are writing the code in Python, the extension is PY.

So you can save the file as PY. And then we have so many different environment where you can write and execute. So that’s why we have this ID integrated Development environment. And I have one IDE. I will show you that how you can go at least at which place you can go and install the software. So here you can see that I have Python Three eight and I’ll come back to this particular display. Let me go through a few of the slides. For example, ID is integrated Development environment where you can go and write the code and it will get executed. For more information, you can go and check the Wiki. Now, how to launch a program Python program.

You have multiple ways you can go and use directly from the CD. You can go and dot slashandmyprogram PY, it will get executed. Or you can double click. You can go to the IDE. We are going to use ID in this particular section. And we have other example as well, related to say, for example inside Nexus OS, you can go and simply enable build the Python. Or maybe if you have any automation engine, that automation engine, you can go and run the Python program in the same network that we have the network devices. So from there also you can go and execute. You can go and run the program and you can get the result. Those examples we have in the upcoming session.

2. Python Basics 02

Let us continue. So how we are going to compile the program and how it is going to run. Here you can see that we have the source file called PY. So any program that we have created, you can save as PY. Then it will convert to the bytecode and that’s the reason that the execution is faster. And then it will go and compile is actually not compiled with thin it code and run to the PVM that’s the Python virtual machine. And then as compared to other program, the execution will be faster, the runtime will be faster. Why? Because of this byte code conversion. Okay, so now we have the installation step. I have a link here. You can see on your screen that you can go to Python. org. From there you can go and select your operating system. According to that you can go and install the Python.

 Now, I have installed in my Windows machine and I have installed three eight three with that particular installation. At the moment you install, you’ll get the IDE. So I have IDE, plus I have the Python compiler as well. This is something you can think of inline compiler. So here I can go and say assign some variables. For example, A is equal to two, B equal to eight, and then C equals. So C can go and carry the multiplication of A and say B. And then I can go and I can print C. So here you will see that it’s something like bytecode compilation. And it’s A, it’s an interpreter based, it’s very, very fast because everything is interpreting line by line and behind the scene it is converting into the bytecode and it is giving the output. Okay, now, what type of data variables, what are the assignments and all we can do in Python. So, for example, we can go and create the program PY file.

 So let me quickly go and show the object models that whatever objects we can create. So here we can see the list of the objects, we can go and assign the numbers, string, list, dictionaries to purse, files, set and other types like booleans and none. So these are the built in data objects that we can assign to certain variable. And then we can play around with that variable and we can get the desired results. So what are the things we need? We have the building block that’s the object. And then you need some sort of program or algorithm. So you have to fit these objects inside the program as per your desired output. Again, then you have the number say it is a normal and is a long floating point complex that you can have a reference here.

So next, what I want to show you here, that I want to show you one program, and let’s try to understand that how this program will execute and then how we’ll go and get the result related to this particular program. This is one of the program related to swapping of a number. So here you can see that X assigned five, y is ten.

If you want to write some comment you can use this hash and then you can give the comment anything that is commented that is not used. But when you go and see the program you’ll understand what’s the meaning of x and y and what steps you are doing in the programming and algorithm. So that will be as a comment. So for example here create a temporary variable. So I’m storing x inside temp and then X equal to y and then Y equal to temp. Actually this is the logic. Now here you can see these are the objects, these are the integers and this is the logic. And then you can go and you have the printf function like and see what will be your output. So what will be your input algorithm and then what will be your output. So output will be these print and again once you print X will become ten and Y will become five. That’s the swap function.

 So let’s do one thing, let’s try to execute this program. I’ll go and give X equal to five and then I can go and give comment value of x, then value of y and then I’ll go and give y equal to 200. I can give any number. Now what we can do that we want to create a temp to hold the value and now this temp equal to x and then X become y. So temp become x become y and then y become temp. That is Y become five and X become 200. Correct? Now here I can go and print say X like this also and then I can go and print y as well. So here you can see that print X and y x initially was five and Y was 200. This is the swap function. The actual algorithm is working here. Again, if you want to print in certain format you can go and do the print say for example as per our example.

So here you can see bottom as well. Say for example print the value of y after swapping and then you can go and use the parenthesis just the programming type of world we have and then we can go and use format y and then we can close it. So the value of y after swapping become five and I can go and put x here and then the value of X is 200. So here you can see X was five, now it becomes 200 and again it’s just a printf function. We are getting a nice looking output. All right. So there is lot many things inside Python that you can go and learn. Basically as a network engineering engineers we are focused on how we can do the automation for the networking devices.

So what we are going to do that in coming next, two videos you’ll find. That how we can go and run over NX OS. And you can put some sort of NX API or some sort of feature. You can go and enable over Nxos. Once you enable how you can verify as well. So Nxox is there, and then I have the automation engine connected with this in the same network. So from there, from that automation engine, I’ll go and run this script for my host name inventory. That is NX OS or NX operating system. All right, so let’s stop here.

3. Python basic program to add nxapi feature

In this example, we’ll see that how from the Python program, I can log into the Nexus, say for example, Nexus One device I can go to conft, and then I can enable the feature. Say feature nxapi. Okay, so let me go to the empty party that I have here. You can see the program user bin environment Python Two. And then you can see the comments as well in the blue color. So this comment is for SSH. Then pause the script a list of the host. Suppose in this case we have only one host. Then what is the login credential for Nxos? For example, username and password ansible ansible create a new SSH connection. Automatically add SSH host key for each host we wish to connect to.

So this will be the output once you log into the once I run this program, then the Sprint output will come. Create a shell session for multiple commands. So say with this connection dot connect host 22 is the port for SSH username password. Look for keys false allow agent false remote connection invoke cell. So I’m going to create the cell connection for multiple sessions. And then you can see the output here is remote cell receive. Let’s print the output. So it will print the output that is host. Then what type of commands that I want to run here? So, first of all, send the command conft. So I’m going to run the command conftnovice. The new line sleep for 1 second. Then the output will get the output

like this from the remote sell. Then the second command I am going to run is the enable feature NX API. That is a feature NX API. That will be the second command. Again, it will print the output for us. Once we run this Python script, exit the configuration mode. Then you can say end exit and then print the output. After that, close the SSS session to this particular host. We can reuse all these things. So no worries. What I can do, I can do here press escape colon Q for quit. Because what I want to do here, I want to run this program. You can see Python enable feature CLI the print output. You can see nxos. Then all these banner what it is doing.

Say these are the outputs in the program conft. You can see the output, then the feature Linux API output. That means now this particular device, if I go and log to this particular device, and if I see show feature, so for example I want what are the features have been enabled. So enable I can see NX API has been enabled. And if I go and check, say for example nxapi output, we can see the output for that nxapi is enabled. It can be listened on these particular ports. Then even I can go and open wire port Ay to this particular Nexus with the Google mode. This was a very small example but later on we are going to add multiple use cases on that.

4. Python basic program to add nxapi feature edit

In this section I am going to show you that how you can edit that program. So what is the end goal? We have that edit that Python program and add some lines. So while executing the program, I can check show Nxapi. Okay, so let me go and open the empty. All right. So we are inside the ansible. Let me go and edit this particular program. So I can go to conft. Then I can enable the feature that’s okay. And after that, what I want to do here that I want to see that as well. So how I can do that, I can click insert because I want to add few lines here. And I can go here. First of all, I want to add comment.

So for example, I want to show the Nxapi say feature status. Then I can give the same thing that you can see this particular place remote shell send. And what I want to send here is the show NX API end of the line, that will be the next line. And then close the parentheses or the bracket. That’s the common rule of the programming. Once you open some box or bracket, you have to close that as well. Then here I can give time sleep, say for example, one complete programming or description type of things that we are doing at the moment. Output I can give. Say for example, I can give a space here. Otherwise it will throw in some syntax error. Say remote cell receive. And most of the things you can see that’s the same that I’m using that is there in the upper lines. Then I can go and give print command print because I want to see this output. And here also if I want, I can give time sleep one. All right. So we have successfully edited one small portion of program here. Once we edit this particular program, what we can do, we need to save and come out from this. So for that I need to click, say escape and then quit and write that is QW enter, let me check that right. And quit come out. And then you can go and check the exclude this particular program. So it is printing the Nxos v one line by line. Oh, you can see that here it has error remote shall see. So that’s the beauty of program. It will go and check line by line all the items.

 And if you miss anything, it will simply go and tell you okay, you missed this particular portion. So what it is telling that this error this place we have error because the configuration is the spelling is incorrect. So I should give cell like this. And now I can type, skip, write and quit. And then let’s run that program one more time. So it is checking all those things up to here. It’s good. And now you can see the output that is running that command. That is shown NX API. So you can see that from ansible from one server I can send the command and I can see the output also. So I don’t need to go here to the Nexus box and SSH and check that output. Even from here also I can check the output. Okay. So that was the edit inside the small program that we have done initially.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!