Network Automation : pyATS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks welcome back this is joel and in this video we'll look into we'll actually do a little bit of network automation and kind of take a break from our regular intense networking right uh we'll talk about pi it's in this particular video right so pits is a very interesting uh think of it as a test automation framework right um and i've been using it uh for for for some time now uh to do some of my day-to-day you know i've been using it in one of some of my day-to-day scripts right or day-to-day automation tasks so i thought maybe i'll do a quick video on how to install it how to do some basic uh you know how to connect to the devices uh run write probably a couple of scripts right and we'll see where it goes from there all right so that being said let's uh all the information which you need is right here on the devnet website right developer.cisco.com go down to slash by ats you'll get to the home page of ideas you've got a lot of examples right lot of examples on code exchange right and the documentation right which you need is right here on the center right you have the pits documentation you have the genie docs and uh something very new which they have right now is um expresso which is kind of like a dashboard for you know running out your test cases and all of that right so we'll look into pieters and genie mainly probably not express that much so coming to uh some of the documentation around this uh so this this is the piety's main dock right the main documentation page and on the left hand side you will find the links to various other you know sub documentations right you will find the core infrastructure which is important the genie library guide and so on right now what is five pie ts to begin with it's um this i think this kind of image kind of clarifies that uh so on the top is your integration which is you know basically if you're writing a python script that would come here right but not just python you can you know i don't know maybe integrate your pids with ansible as well right or maybe any other kind of you know cicd pipelines and any other kind of maybe you are writing a web you have a web framework that also can integrate right on the top but the main meat of it is actually in these two layers right so the genie is nothing but so the whole pyramid here is basically together called spite yes but it has like i said you know a bunch of components genie and the corresponding sdk right you can use genie and sdk byte it's by itself as well right so genie has a lot of parsers so maybe if i have the page open here yeah there you go right so this is basically showing us all the various parsers which you know people have written uh till now and this is a very active community so people are writing parsers day in and day out right so you also can contribute by writing a parser and uh you know it if it gets approved by the pie ts team they'll basically add it here so you can select whichever operating system you have here right you have uh you know a lot of cisco operating systems i see junos as well and uh couple of these even i don't know right and viptela and all of that so you can you can basically uh write parsers right it's pretty simple to write parsers and that's that's why you know this this particular layer on the top here is contributing you know to the whole framework that's the main place where you write the parsers where you include the new protocols models and all of that and this is the library framework for the uh you know for the libraries or the further like i said for the parcels and all of that which you write right it's kind it kind of gives you that you know agnostic uh you know feeling right which means by invoking this library i can just say hey go and collect bgp from my network and it will it will basically go to every single device figure out you know which operating system it is running and then it can you know it can it can get you the outcome right so you don't have to write a separate script for i don't know maybe nexus you don't have to write a separation for ios xr xc or junos right you you can have just one script use the framework use the library framework and you can you know kind of let the system decide and figure out and uh it's intelligent that way right and uh the bottom most layer this one is the core test infrastructure right so this is mainly for writing your test cases test definitions and it also gives you a very nice report at the end of the day right so if you're writing individual scripts you can directly use genie right and then if you're running some kind of test cases you you would generally use pi it is right test cases are nothing but i would say it's nothing but um a bunch of tasks right so you might want to do some testing with your device right you might want to enable a new feature in your network maybe say i don't know maybe ntp you want to enable a new feature and then you want to also make sure that nothing breaks right so you can define a test case to do that you can say let's go enable ntp test these things and then you know obviously go and disable ntp and again test if the situation is all good so you can define your own custom test cases like that and you can for doing that you can use the pie ts right maybe we will not see test cases in this video maybe if i have time i'll do another detailed one on test cases later but for now we'll mainly concentrate on pie ts and genie but from a very uh you know how you can use this in your day-to-day scripts right um to do some of the simplest tasks right so that's what you're gonna check all right so this is the main documentation like i said but uh so you can you can get to all these links right from here right uh i'm sorry from here on the left hand side you've got all your documentation pages i've just opened them one after the other just to show you right so that being said let's okay what what topology do we have here it's a very simple i have a nexus 9k and couple of you know csrs over here right so okay i think there's a small spelling mistake here it's supposed to be csr okay so we've got nexus 9 k1 csr3 and i don't know what they have they have probably some bgp running i guess right now let me check so it's i think the username password this one is admin admin yeah so if i'm not wrong they have some kind of uh yeah so they have they have bgp running or let's just look at show ip route yeah so they have eigrp and bgp running right pretty simple so in the i have the underlay kind of uh uh i have the igp which i have used is eigrp so that is basically uh the loopback zero which is 1.12.23.3 right for the whole three devices you know is advertised in hrp and uh then we have bgp which is running on top of it and the other loopback which is loopback one is advertised in this vgp that's it right there is nothing else so they just have a jrp and bgp and we are gonna use these three devices for our testing right so let's start with first uh what let's start with clearly uh in installing uh piotrs right so right now i mean these are just two windows i like to have two windows so i'm just gonna keep it here let me just clear the screen a bit here yeah there we go hope this is visible it's big enough cool so what we're going to do is let's create a new folder right i'm going to call this guy as i know this bits please underscore pie ideas right awesome so we have created that folder let's get inside that and i like to use virtual environments whenever i'm playing with python right so uh and what python i'm using right now i think it's 3.7 yeah it's 3.7 right just want to make sure if you are following through you can do the same as me as well all right so we've got we've got 3.7 right so that's good uh what else what else should we do next okay so let's go about um installing this virtual environment right it's basically nothing but uh python uh the module which is v and v right and since i'm already inside the folders i can just do a dot right so now it's going to install think of virtual environment as a small instance of python why do you do this because this way i can have my multiple projects different projects running in different python virtual environments right it won't it won't cause trouble to my other existing python project so that's why i like to create new virtual environments cool so once you're done with that you can just uh source it right your sourcing is nothing but activating that you know environment so right now i activate it and you can see the prompt has changed over here so which means we are good next let's install pi ads it's pretty simple just one command right pick install and if you are using z cell like me probably you might have to put a code like this right pi ets and you can mention it to be full installation right so that's useful so if you are probably just try if you are not using if you are using bash i think this codes might not even be needed right try it without the quotes but if you are using z shell like me so i'm using you see on the top here i use z shell so in that case uh you might need the codes i'm gonna hit the enter here and uh so there you go it's trying to collect it's going to download and install a bunch of things i'm gonna come back in a minute once this is done all right all right so looks like uh the installation is completed uh the other interesting thing which i like to also install along with its it's nothing to do with its as such but i like to install it because ipython right you guys probably know about it it's an interactive way of you know sending python commands right so that's something which i like to do as well so let me install that i mean again this has nothing to do with its if you don't want if you're already using an ide and all of that you probably don't even need this so yeah let's give it a minute all right so ipython is done as well cool so i think we are good to go ahead guys you can see uh uh everything which we need is going to be that here so uh in fact let me create a new folder inside my you know workspace and maybe let's call this as uh and let's call it as codes right maybe that is good so yeah that's good so let me clear the screen a bit okay so we are inside the folder codes and we are gonna try some basic stuff right so first thing when you have to connect to any device in our case you know what devices we're connecting those these three devices right so when you have to create connect to any devices you need what they call as a we need a testbed file right so uh there are different ways of creating test beds right you can manually create it it's nothing but a yaml file or you can use spiders you know the create testbed utility as well right to create it for you right so we'll actually do that but there are different ways to do it and you can see in one of the documentation i don't know if i've opened it here uh there are quite a lot of ways of doing it so maybe i think it's on this one yeah connection [Music] yeah i think this one or probably this one yeah i think this one so you have uh if you go to the test bed you know options here you'll find a lot of ways to create the test bed right you don't have to really do it manually you can do it in different ways you can check it out from here if you have a list of devices in your csv file right with the corresponding i don't know maybe ssh username passwords then you can create it from there as well right so but then in my case i'm going to show you one way of doing it right so let's say we're going to call pi ideas right and the utility is called i think create test but if i'm not wrong yeah it's create testbed and interactive right so uh this way you can and then you have to give the name for the output file which is gonna be output and and maybe you can give the name as testbed.yaml right okay so now what it is going to do is uh let's go and click enter give it a second all right so my screen was a bit stuck but i think we are back uh pythius create testbed interactive and the output file which is uh testped file uh okay let me do one thing let me just cancel this let me go inside the codes folder right which i was earlier on all right so here let's run the same thing again okay so now it's going to ask me do do all the devices have the same username in my case there is no so i'm going to say no do all the devices have the same default password again no do all the devices have same enable password i actually don't have enable password at all so i'm going to say again no now the device host name device house name uh in our case let's do for nexus right we're gonna pick this guy and let's paste it here so that's the device hostname the ip address is 1064 66.39 right in my case it's basically i'm connecting to my eve devices right my devices which are on my eve topology uh and i'm telenating it to it right whenever whenever you go onto your ef topology when you double click any of the device you see on the left hand bottom corner you can see that there is a port number right so in my case it is three two seven seven zero so let's use that so the port number is going to be uh three two seven seven zero so there you go so that's the ip and the port number right in your case uh if you're using the default um intel net or ssh port then you don't have to mention the port number but in my case you know since i'm using connecting to the devices via basically the simulated devices i need to do that right and username is admin password is i know let's use admin right it's the same password and the protocol is obviously telnet the operating system makes nexus index os more devices to add um i mean i can add here but you see it's i have to keep on typing right so what i'll do is instead i'm going to say do more devices to add i'm going to say no press enter and there you go the testbed file got created and let's open this testbed file right all right so there you go i've opened the testbed file in my um visual studio right uh and you can see this is how it gets created so something which you need to be very careful is with the indentation right so you see this is the format of the yaml file and it has to retain like this right uh now what i'm gonna do is i'm gonna uh just uh use the same format right and i'm gonna add my two of the other devices right i could have done it uh directly over there right by adding more devices using that uh utility but i i'm gonna do it this way to show you that you can manually edit this file as well so what is it going to be in our case it's uh csr1000v right this one so let's grab that and let's change this host name for the second one csr1000b the ipads remains same let's check the port number port number here is three two seven six nine so let's change this one to three two seven six nine right telnet all of this remains same the user name and password on these devices this is cos cisco right so let's change that and obviously this one is going to be ios xc right because these are csr1000v devices cool now let's again do the same thing let's copy the same section let's quickly go and dump it again and change this guy to what three right and the port number let's figure out the fourth number the port number is three two seven seven one so let's change this one to seven 7 1 the rest everything looks good right so let's save that it's already saved now if you want to quickly check if you have done everything right you there is a way to do that as well i think it's called a validate or something let's try that so it's called pi ats i think it's validate and then you have to give the uh test bet obviously and the testbed file if i'm not wrong yeah there you go right so let's wait for this if there are some errors in your yaml file it basically shows up here right you see there are no errors uh there's some warnings but these are fine um and all your textbook devices are good right so that's good let's clear the screen a little bit also while we are here let me also show you what version of pi ats i'm running because they release new versions i think every month or so right or every three weeks i'm not exactly sure but uh you can do pi its version check and um you will basically get to know so see i'm running 20.12 right so if you have some errors and if you are trying to talk to the pids community maybe this will help you right to tell them that this is a version which you're running cool again let's go back so so we have created the testbed file what else what next now there are two ways of interacting or using pie ts right one is inside your python script but you can also do it without that right and what i mean by that is see whatever we did till now right we were using the pythos uh uh you know tool toolbox directly right we were not using python at all we were directly using the pie ts toolbox so very similar way for connecting to the devices and doing your tasks right you you can use the pi its shell right you can you don't have to again use python at all right so uh you you can uh sorry not buy it your shell you can use pie tears directly without python right to connect to the devices and do everything right so that's a way of doing as well and we'll probably check that out maybe later give me a second right so we will check that out uh maybe the very next step um as a next step uh but for now let's let's get into what we call as the pi ts shell right so pi aps shell let me tell you what this actually does so pi to shell and what is the testbed file which we just now created i think it's called again test bed only right if i'm not wrong yeah there you go so when you ignite the pie to a shell what it does is it basically nothing but uh you know runs these two commands right which is nothing but it is loading the genie testbed right and it is also loading the file which you just now provided right you provided this file over here so it is going to load this for you right this is nothing different so what i can do instead of doing this is i can do this let me split horizontally and okay after i can go back to my ids folder let me source this so that's why i always keep two terminals open so that i can do some quick testing right so now the same thing if i want to do what i can do is i can simply go and trigger my ipython here right so i can trigger my python and basically run these two commands directly here right and this one as well okay so that's that's it okay okay because i am not inside the uh that's uh that's the reason okay let's go back let's go inside the codes folder right because that is where the file is let me go inside again and this time let's load it and there we go right so you see um it's it's nothing but the very same thing what what what this one command does for you which is pie to your shell and providing this testbed file right it basically simplifies you running three commands right i had to now run ipython then i had to you know import this testbed and then i had to load the testbed instead of these three commands what they have done is to make it simpler they have bundled all the three commands into this one single uh command here and that basically loads everything and gives everything ready for you right so right now what we are trying to do is we are trying to explore pi ats via python so right now we are in inside python and we have loaded couple of things we have loaded the testbed file let's see so what is this testbed file to begin with if you see testbed it's nothing but an object see here it's an object right it's nothing uh it's like a regular object okay and we can do something like test with dot devices right to see what are the various devices right you can see here you have the nexus device you've got the csr1000v2 v3 basically all the three devices are visible i don't know if clear works here yeah clear works cool so uh uh that's that's basically it right you you can you can see testbed is an object that's without devices you can see the devices now what will be the very next step normally if you want to connect to one device right i'll have to do something like we saw that there are many devices so what we'll do is testbed.devices and let's actually pick one device right which one do we pick let's pick the first one probably this nexus one let's pick this guy okay i think that did not go the way i planned let me try it again let's put a code yeah there we go right so this is how you can connect to the devices right so we saw what testbed is we saw what testbed.devices is now we are picking one device out of it right and we are connecting it sis is not defined okay let me fix that give me a second all right so i don't think it was an error i think probably because i had two environments two ipythons running maybe it was kind of creating some kind of trouble so i just closed the one at the bottom and now you can see uh basically connected to my device which is nexus 9001 right which is the which is basically now stored in device right and how you can connect you can do just simply device dot connect as simple as that right there you go you see what it is doing it is using all the username password which we provided in our you know test page file right and it is basically connecting to the device so right now it is really connected and what is the meaning of it is connected which means i can do anything on the device right which means let's say i can do something like output equals i know maybe the device is basically stored in dev right so let's do dev execute right there are a lot of utilities now so that's where let me go back to the documentation and show you here right so on the pi it is right on the top left hand side you can see this is this is what you can do with pie ts you can you know connect to the device you can um you know pass the output of the devices you can learn some device feature you can configure it um you can you know do some diff utility and so on so this is basically what we are exploring right now right so um yeah so we are basically going to execute what do you want to execute maybe let's run a very simple command which is like show version or something right let's try that ah device object has no okay the spelling was wrong my bad terrible with my typing so yeah there you go right so device dot execute it is running the command there you go we ran show version we literally ran the show version come on and if you want to see the output i can just do output here and there you go this is the output right but really this is difficult for you to use in your python script right i mean how would you kind of you will have to write your own parser to do this but this is where you know pi just makes it easier for you right instead of doing the same thing which we did before right we earlier ran execute but this time instead of running execute what i'm going to do is i'm going to change this to parse right so i want to change it to pass and what i'm gonna do here is excuse me and uh and just change this to i know maybe something like patched right now let's run this okay now let's see the output of parched ah this is useful right you can see now it is json now this is very easier for us to kind of what you say to explore isn't it to to kind of use in our script and one simple example of that would be something like let's say someone is telling you uh provide me the value of the memory right or you want to use the memory value of this memory or this processor board id it's very easy to fetch that how would you fetch it you just go to parched right and what is the outermost uh you know json key which is platform right and let's go this is basically nothing but a nested kind of a dictionary right so you just need to go inside one of the other so we are inside platform and from platform i know maybe let's go inside hardware right so you see this one basically ends so this is platform starting from here and then you've got like software which is ending here so uh inside platform there is another key which is hardware so let's go inside hardware and so i can simply do click on hardware and you see i get so much but i still have to go one more level inside because i need the value of memory right so i can basically grab this paste it here and probably go inside i know maybe if you want the value of memory you can you know just get that and now there you go we have the value right so literally using this one single entry or one single you know i know maybe hardly four words i am able to grab the memory value of my device right instead of me having to write a parser to parse all this output of the show and we already we have in pie ts we are exploring the already existing parser here and you can see the output is already obtained in the json and it's very easier for me to tinker with this right we could probably try one more so what we'll do is let's try a different command so what we were doing before was show version instead let's uh try show interface right so let's try that oh and uh i forgot okay nexus has lot of interfaces so it's going to take a minute or so to run but you can see how quick it is doing it's connecting to the devices you could probably also see this on the device because i have console logging enabled so there you go right so as we are doing it uh it is getting locked on my console as well uh and we can see everything which you know pie ts is actually doing very well it's done i think let's check yeah looks like it is completed now if i want uh to probably see the output of past right there you go so you have the json key values whatever right and let's say someone tells you provide me the output of i don't know maybe the mto of this particular interface which is ethernet one slash 28 right one slash 128 so how do you do it you would go inside parts right you go inside the interface and once you're inside the interface let's find where is the mtu right so this is the interface and the mtu should be somewhere here one of these key values ah there you go right so the empty is here so directly i can access it saying empty there you go so we have the value of mtu which is 1500 right so also this is the reason why i like pi ipython right it gives you like a very nice interactive way of dealing with python so this is why i mean again it's not compulsive that you ought to use python you can use something else whichever you are comfortable with all right so we tried that what else let's also try something simple let's uh so we tried uh i don't know maybe executed parsing right we tried those two utilities let's see if we can configure a device configure and configure something so what we are going to do is uh probably let's um let's let's uh try to configure so how do you configure it's nothing but dev dot configure and first let's not configure anything let's just send some empty value right basically we are not configuring anything nothing explicit we are just seeing what it does right so there you go so when you when you go to the dot configure utility you can see how the prompt change to conf mode right so which means it it has the capability to configure as well so what we are going to do is which was the interface ethernet one slash 28 right so let's go and configure something so what we're going to do is we're going to say uh maybe we can send any command you want so in my case i'm going to send interface and let's grab this interface whatever this is ethernet one slash 28 whichever you were playing before right so let's do that yeah they were gone inside the interface and uh next line on the next line you have to put a slash n right and let's put a description and we are going to put a description and what is the description going to be something like bits please let's put the description as bits please here all right so that's good let's close this and let's see if this configures ah there you go it went right you can double check what happened here right you can see pi ats has gone and configured it for me i can again double check if you want show run interface ethernet one slash 28 there you go right you can see the description has come in so that's how easy it is to you know execute parse and configure your devices using pythons okay now whatever we did was till now was with python right what if you want to do it without python so let's probably try that so let me clear the screen a little bit let's grab this guy okay so till now like i said we did it with python right we did actually with interactive item it's normal python right but there is also utility for you to kind of do it without python so if you are not familiar with python and you want to still use pythos you can do it and that's how that's what you're going to check now so you can use pythos and these commands you can use directly like parse right whatever we did earlier right we passed show version earlier so let's do parse show version right and let's provide the test pad file and all these things are there in the documentation right i am just referring my notes here but you you will basically find all of this in the documentation all right so we're going to say pi its path show version provide the test pad file and uh you know you also have to provide the output right output and uh the output here will be stored into a folder so let's provide a folder let's say snapshot one or something right so actually you know what let's uh let's run not just one command so you can run one command like this but you can run multiple commands also right so we have we can parse multiple commands so that's what we are going to do let me show you that so instead of just running show version i'm going to do show version and show interface as well sweet so show interface yeah there you go so we are running uh okay the problem here is i think the show interface will not work on nexus let me check that show interface yeah okay show interface works here but i think show interface will not work on this guy because it's a ios xc right let me just double check that give me a second i think it will show interfaces here yeah it show interfaces so what i'm going to do is i'm going to run three commands then so let's do that so let's run show interfaces as well right okay there we go so what we are doing here is we are basically running these three commands right on all the three devices and not just running we are parsing we are running it using the parts utility so that output will be in the json format now what it is going to do is since show interface is not supported on ios xc it is obviously going to give me an error or something you know for ios xe but instead it will go ahead and run show interfaces on those devices similarly show interfaces i think is not supported on nexus so for that you know we are going to get an error but rather it is going to run this for that right but i just want to capture my version information and interfaces from all my three devices which are there in my test bed so that's why i put in all the three commands here so that it can run one after the other and the output like i said gets stored in snapshot one so let's run this so give it a second sometimes it's a bit slow to connect to the devices has it already connected we can check from here yeah i can see it's doing something on the nexus already right you can see some the status bar has come in yeah so it's probably uh doing this on one of the device and as soon as it does the task it will give you some output about it some summary right and then it will proceed to the next device so it's gonna take some time because you know there are like i said in nexus there are so many interfaces right so it's going to you'll have to run it through all the interfaces so it's going to take some time so there you go right it finished it finished nexus you see what it did it basically ran all the three commands for the first device so it will do device by device right whatever it does right in any of your test cases and everywhere in future as well it's going to do device by device so it picked the first device it went and run show version show interface and show interfaces but show interfaces you see it said it could not find the parser because that command is not supported on access rather it ran these two but the same thing for csr it is obviously this one will error out whereas show version and show interfaces will succeed and you can see all the three finished now and let's check probably in my folder here you see there is a folder called snapshot created and inside the snapshot we will find all the files right let's do lsl probably and you'll see there are a bunch of files so there is connection file which is created which is nothing but the same log which we have here right all the connection related logs will be that the main outputs are always stacked with this parsed you can see there is a parsed this is basically for the csr uh the second device there's one more parts here so these are the outputs we have to see like for example i can just pick this guy and uh or maybe we can just look it from our code here right vs code so here there you go let me just grab this to the right okay so let's look at the parts right this is what i i was talking about so you see the parsed output is nothing but the json value the console is nothing but the whole con console you know log right uh the the raw output basically the raw output of the you know uh of that particular command so this is the parsed one and this is the whole complete uh you know log which is seen on your console right so that is that is what it does right you can coming back to our discussion like i was saying we really don't have to run it using python right we can run it without python as well now just to show you another interesting utility while we are here what i'm going to do is there's a very interesting way of doing diff right you can see here there's a comparison of network states right you can do that and that's something which i really like about pyths so what i'll do now is let me go to one of the device which is say this one right the nexus 9001 and let's see so show ip interface brief what do we have okay so uh let's see show run interface one of the interfaces which is one slash 128 right which we were playing earlier looks like there is nothing now what i'm gonna do is uh or or let me do one thing so uh because i want to clearly show you how it is differing so let me do one thing so let me just uh or or i could create a new loop back now i'll create a new loop back i'm going to say low back 10 right let's give us some ip address to this guy i'm going to say you know triple 1 triple 1 triple 1 and triple ones right 255 all right so there you go so i'm going to put that okay i mean i just want to do some change on my device and i want to show you that it is able to capture the difference right so there you go so i have created a new loop back now and let's go back to the same command which we ran earlier right let me clear the screen a little bit so that you are able to see right let's run the same command as before let's actually go back cd dot dot yeah so now now let's run that pi ds command but the same thing i'm going to capture it into a different folder right not snapshot1 and let's capture it into snapshot 2. now let's wait for this to run so let's pause the video and come back once this is completed all right so looks like it is completed and we should see that folder snapshot 2 as well over here so now uh pi just gives you this cool capability to do a diff what i mean by that is i can do pi its diff and snapshot one and snapshot two right so it is going to now compare basically it will do kind of like a linux kind of a comparison and it's going to do complete comparison of um you know both the devices and we'll try to find if there are some diffs right so here you go so we've got uh we've got we've got some dips i believe let's see so looks like these devices are all identical this one also is identical uh looks like this one was there only in snapshot one uh but the one which i actually changed was this one right so let's check this one so it is telling me the output can be seen here so let's try that what is it having ah there you go right and you can see the difference right the basically that new interface which i just now added you can see there is a plus mark along with it right and because i added the new interface obviously there are plus marks around admin stata bandwidth and all of that which is very much perfect right which is exactly what i wished for okay so uh there seems to be one more diff here i don't know what this is having did i change something let me see that uh cat let me just okay that did not get pasted let me paste it again ideally there shouldn't be anything inside this because we did not do any change uh but looks like okay looks like there was some kind of counter changes so that's why they that also got picked up right there was no configuration change but there was some a little bit of counters probably changed ah that's why anyway so you can see but uh majorly um this is a very useful place it's nothing but your traditional linux diff right uh but you know they have integrated inside pie ts that so that you can use this in your code as well so you can capture the pre checks and keep it in a folder and then doing the post checks you know you can do a different you can quickly figure out what are the changes in your network all right so next the other interesting thing so let's go back to the documentation i keep forgetting what are the other things uh learn learn okay i think i forgot the learn one so let's look at learn so learn is a very interesting again utility again all of this whatever i am showing you can be done either using python or you can do it using the non-pythonic way where i am showing it directly using the pi atf cli right so um let's do it by the cli again so we can do pieteos and learn right i can use directly learn here and i can tell by ideas to now go and learn stuff from my network right so remember i have what bgp running so i'm going to say priorities go learn bgp and not just bgp probably learn about all the interfaces as well so i'm gonna say learn bgp interface and let's provide that testbed file right you'll always have to provide the testbed file because you know pi just need to know what the device details right so that's that and then let's store this in some output right and let's create a new snapshot folder let's call it as operations snapshot one right so i mean the this is again one thing which i want to call out and let's probably go to the documentation to actually show you that so if you see here you'll find something called models here on the left side right and these are the models which are already written right and i'm pretty sure they'll be writing more in the near future as well so in the models you can see these are the models which are written now we are looking at what bgp right let's look where is bgp yeah there you go let's click on bgp let's click on model so this will tell us what they have modeled right for different platforms right they have for xc xr nexus right they have created this and let's go inside the ops structure here right so the bgp op structure so you see here this is what they are doing for ios xc when i am telling my it is to go and learn bgp it is basically running all these commands right so some experts right in the back end they have verified and they have made sure that all the important commands for bgp are captured right similarly for ios xr and for nexus right so this is why i like to say it is platform agnostic right so pids will do the job for you you just need to tell your pious to go and run bgp and it is going to internally figure out that for ios xc these are the things which are important for xr this is what they have to run for nexus so it's going to capture all this right and it is going to call it as a bgp operational state operational structure right and let's run that now so we are running two things right now we are running bgp similarly we are also running the interface so here there should be something for interfaces so let's go back yeah there you go right so this one so we are running bgp and we are running interface so let's run this so we are storing that in op snapshot one so let that run so you can see it's learning vgp descend this one which on all the three devices we are running because we have bgp running on all the three devices so it will be good to learn on all three devices so let's give it a minute i'll be back once it is done and you can see that has completed so it has learned about bgp and uh interfaces and so on right we can quickly check as well we could probably check it on our studio here right uh there you go right so all of the information right so you've got the ops you can see how in a jsonic way it has learned about bgp so this is this is all done because you know they have already defined the model for bgp right so it knows exactly what commands it has to run and it all stores it in a specific way so that you know you can um and it's trying to make it as much as you know platform agnostic as possible so that you can probably learn bgp from i don't know maybe tomorrow they can add junos and other platforms as well so that way you will you will learn about bgp the same way from all those different platforms and it will be easier for people to use it in their script to do a diff and to kind of see where the changes are and all of that right so that was bgp and let's look at one of the interfaces as well there we go right so all the information about the interface is also stored sweet so that's uh one of the diff utility which i wanted to show as well okay so that being said uh what else do we do next is probably we could um before i mean i just want to show you probably a couple of scripts now but before that let me also show probably how you can do the same learn which we did let's also jump up here and let me try to do the same thing using what using my pythonic way as well right so let's do that so which device do we have connected now we have the nexus connected which is fine so i'm going to do output right output equals let's do device dot learn right and let's learn what interface probably right we are not running earlier remember we did device dot executed we did device dot parse but this time i'm doing device device.learn right and you see the difference right whenever you use the learn it is basically using those models so those models uh internally have a bunch of show commands right it's not single one show command it's a bunch of show commands for that particular platform which they have written so all of those will get executed right so now we are doing learn so there you go it's running it's gonna run a bunch of show commands now okay so there you go it's trying to do i believe let's see what is happening on the device yeah there it goes see it it not just ran the normal show command but it ran a bunch of things see it ran ipv6 vrf show vrf all interface it ran show routing vrf all and so this is all done in the background right so yeah looks like my screen got a bit stuck here right now what should i do probably close this let me fix it give me a second all right looks like it finally ran so now if i want uh to see the output what i can do is output uh because it is learn i'll have to do output dot info there you go right so all the values from the interface are stored in this model right in this way and if i want to extract a specific value i can do the same way like we did earlier so i can pick this interface if you want right i can do output.info let's go inside that interface and i know do you want me to grab something important what do we do so let's grab this type right let's see what this type is so let's do that sorry yeah [Music] okay can i make a mistake let's try it again so let's grab the interface right so let's put that and let's do the type okay there you go so we have the type right very similar to what we did so output it's always easier to process output in json right and this is really how you can do it because it's all nothing but uh it comes as a nested dictionary so it's very easier for us to kind of go and get the required value out of it all right so uh probably let's do a couple of actual python examples i think you kind of got the feeling now but let me still probably try out couple of examples i think i already have them stored here so it'll be easier for me to run it right so what we are going to do is let's show let me show you one of my script right let's call this as script one this is again on my i've stored it i've tried this before it's in my notes so i'm gonna just directly run it for you here and explain what it does but uh you know it's very straightforward right you can probably write similar scripts as well uh so let me grab that give me a second all right so this is one of the script right uh pretty simple so what is it trying to do remember these two lines which we talked about before it is um connecting to the genie testbed right it's kind of importing it and loading the testbed file which we have defined in our case it is testmet.yaml then it is connecting to the device device.connect we did this before right and it is parsing the output right i mean um when we were trying that ipython we stopped at this point but now i have put some small logic here so what is the logic here the logic is it is trying to say which interface has crc errors right so what it is trying to do is uh for every interface right uh it is basically seeing if the let's look at that ipython output right you will basically be able to see it here yeah so if you see here in for every interface there is what we call as counters right so there is a key called as counters and what we are trying to do is inside that counters there should be something called crc errors right so what we are seeing is what we are doing the script is we are seeing if there are counters for any of the interface right output of the interface and if it has crc errors in the output as well right inside the counters it is also checking if we have crc errors and if these these if crc errors are present for that interface then we are capturing the crc error right how do you capture we check this before right go inside the interface go to the counters inside counters find the crcs input cr series and then we are doing if in cr series greater than zero we are basically printing it and uh using the dot format right very common straightforward way of doing it right so we are printing the crc error and if the interface doesn't have any crcs we are printing no crc errors right so very simple i mean we did till here before i just added a small piece of logic here so now we literally have a script ready which is going to provide us or which is going to tell us if there are cs errors on any of the interfaces on my device right let's run this so for running this probably let's do it here below i really don't like running it from my vs studio so i'm gonna do it from here right so i believe the script is here uh yeah that is that so let's run python script one dot py right so it is running there you go it is connecting to the device and there it's running all the interfaces right now it's going through all the interfaces and you'll have to wait for a second because there are so many interfaces here okay still going through yeah looks like it's done yeah there you go right so we have the final output as well right you can see the final output where it is telling uh you know these interfaces don't have any error and that's exactly what we wanted maybe okay right now i think we don't have even a single interface with crc errors but it's a good thing but you see we were able to run this now you might have a question how do you prevent all this you know logs from coming up here right if that is your question we can do that as well so let me show you in the documentation where it does that i believe it is somewhere here yeah so i think it's in your connection so uh i think it is called uh log std output or something yeah there we go right so there is a flag called log s to the output right where is it where is the flag uh this flag is when you uh connect to the device right you can provide this a flag so device dot connect right you can provide uh this guy's an argument over here and you can basically put it as a fault so let me probably do that and show it to you so what we'll do is let's go to the connect here right let's grab that flag like i said everything is in the documentation you just need to search for it right so there you go let's grab that guy and let's put it here and let's say false right so now what is going to do is it is uh it is still going to give me the same output but with it's not going to print all this junk which on the screen right except the final output so let's try that so script right and you can i have started my script and you see there is nothing getting shown on my screen whereas it is actually doing the job in the backend right look it is actually connecting to the device and running the commands but it is not actually throwing anything on my screen over here right so yeah while this is running what we'll do is let's try the second script so let me grab the second script give me a second all right so looks like this guy ran and you can see we did not get any noise this time rather we ran the script and you saw the final output being shown directly here right we did not have any noise because we kind of uh turned off the uh logging onto the s2d output right standard output cool the next script is i have thing i have it ready here uh script two right where what is it trying to do very similar right you probably know this by now so much it's because we have done it multiple times what we're trying to do is add another slightly different type of logic right this time we are connecting to the csr 1000b right i think it is supposed to be v2 if i'm not wrong right so we are connecting to the csr1000v device we are running show interfaces right on it and what we will do is we want to shut the interface and do a no shut right that's what we are trying to do so you see for every interface in the output we are checking if the operational status is what if the operation status is present in the output and the output is equal to out up right so if the interface is up then what we are doing is we are appending it to a list called as two shut and two no shut right so we are appending it to both the list and then what we are doing is we are doing device dot configure right and we are sending that list so this list will be something like interface one shut interface two shut interface three shot so that whole list will be present in two shut and that we are providing it to the device.configure and then we are printing saying that you know all the interfaces have been shut right and finally it is going to be we are going to do a notion so maybe you can do like this lets just grab this one so lets uh put that print shut our interfaces and the same thing sorry control c let's put it here and i'm gonna say no shot so we are doing two things the same interfaces which we shut right we are shutting it first and the same interfaces we are going to do no shut as well and also i think this case okay so let's grab it this side okay so that's good nothing very complicated as such probably you might need a slash in here which is fine okay so let's try so what we'll do is let's go back here let's probably clear the screen a little bit and we're gonna say python 2 this time okay i did not turn off the uh you know study output or the logging for this one so you can see the logging now there you go it is capturing all the interfaces related information and once it has all the information see there you go you can see it is shutting it now right and then it is doing the no shut there you go right the job is done see first it did the shut interfaces and you saw so all my four interfaces got shut right even the loop back everything got shut and then what it did was it went uh and run the no shut part of it which is the same interfaces it did the no shut as well right so technically i think my bgp and all would have bounced a bit but uh yeah i think within the time i think it just came back as well so my bgp should be good now let's check that show ip out all right there you go so i have the bgp routes here so which means my pcp and everything is fine right so this is this is a very simple use case right what we did was we picked all the uh you know interfaces which were up we shut it down and we did a notion right and you saw how quickly we could do this right so similarly you can write any of your you know use cases and you can use the pi ideas in your scripts in the very similar fashion right generally your script should be something like this you this is your housekeeping stuff right you're loading the test pad you're connecting to the devices parsing it what not and then you have the logic part right where you write your logic or you might integrate this with your existing script as well right now again whatever we did now were basically standalone scripts right but uh uh the next thing which i don't know if i have time probably i'll make a video on that which is about exploring the test cases test cases was basically nothing but uh you know defining tasks right one after the other and then making paretos you know execute all those tasks one after the other and then you also get a fine report you know then showing you what you have done and um you know all the useful stuff around it right so hope that was useful guys so this was a very simple um quick introduction to you know pious hope it was useful and uh do check it do check some of their other features as well right so and do try out some useful or some existing code samples from their github repositories and it's going to be pretty interesting right thanks a lot for watching have a good one
Info
Channel: BitsPlease
Views: 3,144
Rating: 5 out of 5
Keywords: network automation using python, python, network automation, ccna, pyats
Id: _a5wfdnOC4E
Channel Id: undefined
Length: 57min 5sec (3425 seconds)
Published: Fri Jan 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.