Cisco CUCM Automation | AXL API using Python for Network Engineers with zero coding experience

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to today's session of cucm automation today i'm going to talk about axl apis via python so today we will start with learning basic components of programming language it is not going to be the complete list obviously because i cannot cover the complete list of components of programming languages in one session but it is going to be enough to be able to automate the excel apis of cocm next i'm going to take an example problem problem is fetching the device pool of a list of given device names now i will also show you what are the steps required to automate this problem next i will give a very brief introduction about python and then we will head towards the demo session where i'll show you how to automate fetching device pool of a given list of devices from cucm let us talk now about some components of programming language so the first one is variables so i'm going to use this website called w3schools.com python to talk about different components of programming language so we will start with variables now variables are anything where we save some data just to use it later so as you can see here x saves an integer y saves a text and both can be used later to print it so variables are simplest to understand right now let's come to lists these are also known as arrays in some other programming languages in python these are called lists the list is a variable where you can save multiple data as the name suggests it is simply a list of values now one important thing to note here is indexing enlarged first element is indexed at zero the index number of second element is one third one is two and so on and so forth so when i print the list i get an output like this with brackets and all the elements of the list written together right how about if we want to print just one element of the list that can be done using brackets and within the brackets writing the index number so let's say we want to print banana so index would be one for banana because the indexing starts with zero so zero and one so this is how we can print any one element of the list how about if i put the index number 10 if it doesn't exist it should throw an error right so we are getting this error index out of range now let us talk about loops so in programming languages loop are a set of codes that are going to be repeated for a fixed number of times depending on the conditions applied so we will take an example of python for loops for loops in python are very easy if you have a list then for each element of the list you can run the below code all right so what does it say for x in fruits so each for loop has some iterations to go through so in this case it will go through three iterations first for apple second for banana third for cherry in the first iteration the value of x will be apple in the second iteration the value of x will be banana in the third iteration the value of x will be cherry and in each iteration we are simply printing the value of x hence this output let's modify it a little bit i will mention y is equal to abc and instead of just printing x i will print x plus y so what should get printed now apple abc banana abc and cherry abc so this is one of the simplest examples of a loop next let's talk about if else statements i will pick an example from here right so if else statement tells us to execute the code only if this statement is true so for example only if b is greater than a then you print this statement okay if this is not true don't print this statement and as you can see when b is greater than a of our program is printing it but as soon as i make it a greater than b what will happen to the output as expected there is no output at all okay let us make one more statement here so if b is less than a you print b is less than a now let's see the output now it is printing b is less than a we can also use else statement here which means if b is greater than a then you print this statement otherwise if this is false then you simply print this another statement so let's look at the output now it that's it for if else statements now let's move on to functions so functions in programming languages are a set of codes that can be reused in other parts of the program so whenever you see some repetitive codes we can create a function for it now this is another very simple function so what it says print hello from a function now a function can have argument also for example function receives two arguments x and y and you print sum of x and y and the function name is sum function now it needs two arguments let us see what happens if we pass to this function three comma five and then as i said function are our set of codes that can be reused 10 comma 50 and okay one more line of code within the function is to print x plus y okay now let's run this code see sum of x plus y in the first call of the function is 8 in the second call of the function is 60. so the first part of this definition we call it definition of the function or defining the function and hence the keyword def when we are calling the function this is called a function call so we can call the same function multiple times and give different arguments to it all right that is a very simple example of function now let us talk about what are libraries in python now let us change the code here i will say import math and i will calculate square root of x i will accept only one argument here and i will print math dot sqrt and then x now instead of defining so many now i'll put some different numbers here okay let's see the output now so you can see we are now calculating the square root now what we are importing here math is a library of function that we are importing so libraries extend the features of this programming language and because sqrt was not a function directly in python if we had not imported it only after we imported this math library we are able to use this sqrt function so that's what libraries do input and output in programming languages so this is how we take input in python we define a variable username and this is the method we are using input and then in the brackets enter username so it prints enter username and i can enter username here so i'll put username learn and learn and trade and it prints username is learn and train great so this is how we take input and python so we have completed basic components of programming language let's move ahead now let us take an example problem so our task is to fetch device pool of a list of devices so let us say we have a list of 100 device names okay and our task is to go to cucm call manager and fetch the device pool name of each of these devices so how we will achieve it with automation at first we need to take inputs our program need to know what are these list of devices we can put this device in a file and we can take input from a file we can create an output file as well so that we can produce the output in a file format for example a csv format is a good format to be used here next read one device name from the list so once we have got our input in a file now what we can do we can create a kind of for loop let me write some codes here in this case so let us say we have a list of device names so scp-123 scp-345 scp-789 these are the three devices for which we need to know the device pool names okay what do we do next read one device from the list one at a time right we need to ask cucm the device pool name of these devices one at a time how do we do one at a time we create a for loop for device n all right this should be in small letter device name list i will name it device name list or device name in device name list and what i will do i will call a function fetch device pool fetch device pool of this device name and we need to save it in a variable so i'll name dev pool name is equal to fetch device pool name function to fetch the device pool and give that function the device name so it will reply with a device pool we will save it in a device pool name variable what we do after this we print it we print like this device pool off right and i'll put plus dev name plus is plus devpool name and then a new line all right sounds good now what do we do next next we create a function to ask csm for the device pool of that device so here we have not defined what is this fetch device pool okay so we need to define that function dev fetch device pool and we can reuse the same name here device name all right any name d name let's see okay and inside this function we define a set of codes to ask cucm the name of the device pool okay so what i'll do here is i will return the name plus square tool okay now read the device name save it in a variable we have already done it okay open an excel file write the device name in one column and device pool in next column all right so in this example we are not going to do exactly the same because we are not opening a file but yes we are printing it the device name and device pool name and the output and repeat one to four use loops all right we have used loops here we have used the for loop here so this this kind of recording step i need to follow to be able to code this requirement so in today's session we are not showing how to code input and output files how to read from a file and how to to output to a file however you can go to this website w3schools.com and understand file handling there are simple codes to read from a file f equal to open and then the file name and read the file with f dot read okay so please explore more about the python coding from this website now let us talk briefly about python python codes are easy to read and easy to learn python codes have some versions so always use the latest version now what are environment variables in windows so unless we set the environment variables and path correctly in windows we will not be able to run python from the command prompt we will have to use the python.exe file location and then need to run the command if we don't set it right so i will show you how we can set it correctly while installing python for the first time now what is vip in python pip is a command or you can say it is a package manager in python this is used to install some external libraries to python remember libraries are used to enhance the existing features of the python by introducing new functions to it next data structures in python we have already talked about lists and dictionaries in previous slides you can explore more about data structures this is a very big topic actually you will find a complete books written on this topic alone so now is the time for a demo all right now let us see how we install python on windows simply type python or windows and here click on the first link from the google latest 3 release this is what i recommend you do download and use scroll down and windows installer 64-bit recommended this is the one i'm going to download today now let's install the python always use this option add python 3.9 to path these are the environment variables in windows i would highly recommend you to use it i will do install now but i'll close this cmd open your command prompt python you see when you get this output you know that the python is installed in your pc okay i can say x equal to two and then print x two python is working in my pc now guys i'm going to use this code editor this is windows visual studio code i again highly recommend you to use this code editor because it's easy to use but you can choose any code editor or even a notepad to write python codes let's create a new file untitled let me save it as cucm iphone python dot py that is what it tells program that it is a python file now the first thing that i'm going to do is import a library to handle http requests so this is the library called requests it is an http library and it will help us with writing http code okay you can if you are using this visual studio code you can you can install extensions for python and it is very helpful as soon as you hover over any module it will tell us about the requests and how you can form that request okay now let us follow an example from here okay now let us follow an example we're going to copy this example from here and write it down here r is equal to get requests.get all that is fine i'll just put cucm's ip address here after that i'm going to print the output the output is contained in r let us try to run this code i just want to show you step by step all right it is giving ssl error and it is not giving the output of the request okay let me switch off the verification now i will put verify equal to false now it will not verify the ssl certificate so we should not get that error at least create verify is equal to false alright save it and run it again you see we're getting a warning but not the error and this time the response is 200 okay that's great but we are not still able to read what is the response content so we'll put r dot content in the print statement let's run it again awesome this time we are getting the response a regular http page of cocm but we are not interested in that page we want to see the axl url now let us run it once more great now it says no authorization provided so guys if you have followed my previous videos or we are familiar with this kind of error and we need to provide authorization to fix this issue so i need to put authorization in this request now now let me put the authorization here auth is equal to excel api user and password put it as a string now let us let us run this code again after putting the authorization username and password run without debugging now let us see exl web services is working and accepting requests that's great use http post to send a request now we are getting somewhere now i will change this get to post because we are going to use post request now now if you remember from previous session this is going to throw an error here but we are not going to get the valid output until we send a valid message in the body so let me define some variables here so so request is of variable i'm going to define and i'm going to define data okay data equal to so request great now i'm going to send the data here in the soap request now we are going to take help of this soap ui tool once again okay skip this close this new soap project cucm project wsdl file now i've selected the wsdl file here so those who don't know how to use soap ui tool with the axl i would highly recommend them to watch my previous video cucm exile using soap ui tool now let us take an example get ccm version so from here we can copy the complete request leaving the optional field so i'll remove the optional fields from here copy this complete now you will notice something i need to copy the complete request but you look what happened python only treats the first line as a string but we need this these complete lines in the string in the soap request variable right how do we do that we need to use three times the double quotes for starting and one two three three times the double quote for ending now this is the way in python to define multi-line strings so we have defined it in the soap request now we are sending this data along with the post request to cucm let us run this now wow can you see the reply from cucm we are getting the version information now that's cool now there is no automation till this point right now let us stick to our problem statement we need to find device pool of devices how do we do that we go to get phone get yeah get phone request okay now to all those who don't know how this request work so i just remove the optional fields from here now we need to send in our request the name of the device the device name so let us see how it works i will open the cucm administrator don't save go to device phone so friends if we input the phone device name here in the soap ui tool i think this is something i forgot to remove okay so this is how we request using the name of the phone and these are the outputs that we get all right let's use it for our example we'll go to code editor and input this request message all right we will run this request from here alright friends so now we are getting a reply using our code as well so from our code we are able to send the soap message and get a reply from it but how do we read this document so many words and letters isn't it now let us take help from another library so import request is a you know library to send http requests and library to read xml document so i need to import a library to read xml document now this is the library i'm going to use the library is called xml module e3 element tree that i'm importing as et so what i'm going to do here is to read this output using et xml doc is equal to et dot from string i'm reading this xml document from a string and r dot content is our string and i will put print as xml doc let us see what happens now okay we are getting something now i will read the zeroth index of this document it converts the xml document in a readable format or we can say in a list kind of format now friends you notice what we are getting at get phone response so get phone respawn response we are at this element we need to go to device pool name let me copy it and then read the text from it so what i'm gonna do this time is xml talk dot find now friends we can use xpath so this kind of searching in xml document is known as xpath you need to just write it in this format dot double slash and the tag that you are searching for all right and then the text so friends using this uh library we are easily able to read the device pool name of this device okay now we need to do some automation let us see input dev names equal to now we have a list of device names okay this was just one of one of it this was the first device name let us take another one let us take csf user one okay let us take another one a tct user one nine and let us take one more from this test tp alright so this is our task there are four devices and with one click we need to find the device pool name of each one of these devices how do we do it so we are going to create a for loop for dev names dev name in input device names right now with for each iteration for the first iteration dev name will be equal to this tab user 006 for the second iteration dev name will be equal to csf usage001 so on and so forth in the fourth iteration the loop will end now for each iteration we need to send these commands so in python if you want to be inside this for loop we need to indent it to go inside the loop okay now this soap request is inside the for loop this statement row number 19 is outside the for loop just as of now but we need to send this inside the for loop so i will press tab once now it is inside the for loop similarly let us send each one of these inside the for loop so what happens now the for loop will run four times one two three four however every time we are going to we are sending as of now the tab user 006 we need to change it so let us remove it from here close the string open the string plus plus now here we will input the device name so our resulting soap request will include the device name exactly where it should be okay so we split this string sorry we split the request in two parts one before device name one after device name and we inserted the device name in between so each time for each iteration of the for loop soap request is going to contain the respective device name what do you think will happen now when we send this request now let us print in a more human readable format okay i will say device tool off plus dev name plus is colon space now let us run this code what is going to happen oops you know friends we are getting the output device pool of tab user 06 is testdb device pool of csf user 001 is default device pool of tct user 019 is default so this list could have contained hundreds of elements and with just one click we could fetch the desired results that's the beauty of automation now what is this what are these warnings if you remember in our last session we used to get similar warnings it is unverified http request the sending client is not able to verify the tomcat certificate provided by cucm let's solve it now so we'll define stomcat sort location a new variable equal to empty string just as of now because we are going to download that certificate to resolve this issue now administrator password will go to software upgrades oops security certificate management we will download the tomcat certificate from here all right so tomcat certificate is here i'm gonna download this download the pem file save it now one more change we need to do what is the cn name copy the cn name this is your fqdn of cucm now we are going to use the cn name here remember your dns should be able to resolve this name to ip address otherwise this will not work now c codon work slash tom cat dot pem so that's the location of my tomcat file now in the verify i will write this variable where i save the tomcat certificate file now let us run this again is it going to what if i could not find a suitable ca certificate bundle invalid path this should how the path should look like let us run this again certificate has expired that's too bad let me fix this so i hope you guys know how to fix this certificate expiry issue we go to certificate management generate new self-signed certificate i just generated the tomcat certificate from here and i'm just restarting the tomcat service now great so now let's download the tomcat certificate run this one again all right all right this guy is giving a lot of a lot of warnings now subject alt name warning i'm gonna google it python python remove subject all game form i'm gonna use this run this one again wow that that looks that looks clean now you know what how i should write it i should have written it in a csv format that's a more readable format let us print it like this in a csv comma let's run it again so great we are getting the output in the correct format now now friends uh i can't do anything about a certificate that call manager has generated and that doesn't contain subject alt name so sometimes we need to suppress the warnings now let's not suppress all warnings let's suppress this specific warning here okay great so we are getting the desired output now so friends let me know in the comment section below if you were able to follow through and understand each step of an automation process
Info
Channel: LearnNTrain
Views: 2,706
Rating: undefined out of 5
Keywords: Cisco, Unified Communications, Cisco UC, CUCM, AXL, Python, API, Automation, Python Automation, Network Automation, Python for Beginners, Network Automation for Beginners, Cisco Unified Communications, Cisco Unified Communication, Cisco Unified Communications Manager, Cisco network Automation
Id: ah_hoixNeqk
Channel Id: undefined
Length: 44min 10sec (2650 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.