Network Automation Intro with Pexpect and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone so today we are going to be going over using pspet to automate some cisco switch configurations so automation is a big topic of course in networking and there's many ways to automate things there's ansible there's dna center there's everything i've found quite a bit of use in pizzabet it's very simple very easy to use and i think it's a good stepping stone into automation for most people because it's very simple at least to get started there's a lot of things you can do but the basic configuration or getting stuff off a switch is very easy so first off the main thing you want to do is look always look at the documentation so the php documentation is easy to find it's online and there's a little bit of difference between linux and windows but it's all in the documentation so for this video [Music] we are going to be logging into one switch we want to get the output of a show version command and then we want to save that on our computer so we're only going to be logging into one switch today of course with this you can spend on it and you can make it so that you can log into many switches at one time you can do a loop and then log into each switch one by one and then get the information and add it to the file anything you want to do you can do but this is just going to be the basic steps to log into a switch using pittsbet and getting the information that we want so right now using cml i have this switch set up with an ip address that i can reach from my virtual linux machine so with this we should be able to log in use the username and password of cisco get the show version and then add it to a file on our computer so for this video you can see here that i'm using python 3.6.8 with php version 4.8.0 so the first step you need to do is make sure that you have python 3 and pxpet you can also use python 2 but for this video i'm using python 3. all right so let's get started first off we have a file python file here and we want to start off with we want to import rpxpet next we want to import sys because we will need it to see what's going on which we'll get to in a second all right so first off as in our documentation we want to spawn an instance of an ssh connection to our switch so we can do this by saying pitch bet dot spawn and then the commands log in so let's just go and do that in our terminal first so if we say ssh cisco at our ip address you can see that we get logged in banner and then we put in our password and then we are in the switch so so from here we can do show version and that's that's that's what we want to capture in this video so we'll we'll be doing that same same steps but with pspet instead of us typing it in ourselves so we want to spawn a ssh instance and we'll say ssh same thing as what we did before all right now that we did that we want to expect what will this the switch will send us back so when we logged in it sent us back password so we can use that we'll say child which is what we set here the variable that we see here dot expect and we'll say password and this is also redress so you could say nor case or you can match whatever you you know using regular expressions now that we did that the next step is to send the password to log in so we can say child or ch dot synline and then we'll say cisco which is our password right now that we did that what happens next we log in and it comes up switch one greater than sign so we'll say trial dot inspect we'll say that now that we're here we want to get the show version output right so we can say trial dot sendline show version which is what we did here but there's one issue with that so if we do it over here show version you might notice that we have this because you know it's split so what we can do is we can say terminal length zero and then when we do it it will spit out the the whole thing and we want to do that so that way we make sure that we capture all of it so we'll do that before we do show version and then we'll expect the greater than sign because that's what we'll get back in the meantime all right so now we ssh we expect password as the prompt we send our our password we expect the greater than sign because that's what that's a that's what comes up here but this is pretty much guaranteed to pop up if our password's right and then we'll say terminal is zero which is allows us to see all of the output and then we'll expect that greater than sign again then we'll do show version so after that we once again want to expect this and then we want to edit because we got what we wanted right [Music] all right so now that we did that there's only one issue and that's what it that's why we imported cis up here we want to be able to see what's going on at least right now so that way we can if it messes up we know where it messed up so we can say up here that the log file it was cis dot std out dot buffer and that will put the output of what it's of what you expect to see onto our terminal when we run the program all right so now let's go ahead and try it out so we'll say python and then our file name and it went fast but you can see that we logged into the switch we did the same thing as we did before but this time it did it by itself [Music] so it's great you know we're we're getting somewhere now but we still don't have the output into saved onto our computer so how can we do that well there's a command which is before so this will capture anything before the last inspect so in this instance we don't want to put it there we want to put it after this one because that means that it will show us this anything be anything that came before this expect so we'll say child.before now of course we want to save that data into a variable so that way we can get it so we'll say x [Music] equals so now that we we saved it into this now we now what we want to write it or somewhere but for now we'll just print it so we can make sure that we're on the right track [Applause] and you can see yep after we had to the switch which is right here it printed out the output of chopper but it's not very readable right now but we can fix that so if we want to see it line by line we can say split lines so that will split on every new line but since this is going to split it based on new line we need to print out each line so for line and x and we want to print line twice again and there we go there's line by line the show version you might notice that there's a b before every start at the start of every line well that's because it's in bytes so we can fix that by saying decode and we want to code it to utf 8 so now once we do that do this again and boom we have the output saved into a variable and it is does it have the the b before every line all right so that's one step closer but we still don't have it saved onto our computer it's easy enough so we use a contact manager to open a file say with open we'll say output dot text and then we'll say we want to overwrite anything that's already there as file and then we want to say four lines and same as before but this time we'll write into file instead of just printing it then we'll say file because we named it open it as file dot right we'll say lines and then if you want to put uh something special after each line where you want to put a new line you can just not the most elegant way but you can just split that so that means that it will open a file called output.text it will overwrite a file if it's already there and then for each line and the child.before it will print out each line and then a new line so let's try this we'll take out the print so that way we don't see that run this again oh we got an error because we put lines instead of line so let's try it one more time all right so now let's see what's in that file you can't really notice anything change because it shows us everything that we just saw but it's a separate file because you can see output i typed in output.text which is what we saved it as and you can see we got our information into a file on the computer so this might give you a starting point where you can take what you learned here and you know expand it to do more things i would highly recommend that you look at the documentation and in the future i'll make more videos and spending on this and going into more advanced topics if you have any questions please let me know in the comments and thank you for [Music] watching [Music] you
Info
Channel: Lezu Networking
Views: 4,330
Rating: undefined out of 5
Keywords: python, automation, network, cisco, cnna
Id: 8QfD8V_-7ok
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Wed Aug 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.