Automation with Python - Login to Multiple Cisco Switches.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello it's bros and welcome back to my it workshop this is alvin drill and in this video i'm going to do something i said i was going to do um like some weeks ago we're going to write a script for using python of course and the net micro library to get information to pull uh commands from a switch and we're going to save for multiple switches in this case i'm going to use two but i have tested in like eight switch switches at the same time is it all it also works for routers i would say more for most cisco devices but i only tested it in sys um routers and switches okay so for that end uh i have this this is the script we have used some weeks ago this is to get to retrieve information from a switch so we're going to execute commands we're going to send commands to the switch and the result we're going to save it in a txt right so we're going to modify this a little so before that this is my it workshop this is my channel and i'm not going to be explaining everything because i have explained how to install the redmi library or python and how the variables work so that part i'm not going to explain it in this video that part is explained here if you're interested the video the link for this video is in the description below all right and also i have many videos about python oriented to networking so if you if you're interested in that you can click on the top right of this screen for the play list so now we have this i have just copy pasted from the original from my original script from some weeks ago and we're going to modify this all right so this is for the net micro library and this part is the one that allows us to actually connect to this to the switch using ssh so again this part i'm not going to explain it i'm going i'm going to modify it and we will continue so this part technically i don't need it very much i don't need it and uh this part over here i noticed that we haven't i coded but we haven't used it in the last video so what this part is doing is um you don't really need it but maybe all this code all this function is retrieving from the computer you're running this from the username for example i have a cmd here and and if i type who am i is going to give me the name of the computer in this case this computer is called this computer yes very original and the username is helping drill which is me all right so in a corporate environment in a company this is most likely the same username you use for windows so for linux is going to be the same user you are going to use for to login to us to uh different devices in the company in this case cisco uh but most likely you are going to use the same credentials normally because you are using in a company environment you have um what's called a radio server to allow you to do that right but just in case so we're going to use it in this video uh so i'm going to leave it there then we don't really need this and all this part is what allow us to connect to the switch or router using ssh so now i'm going to start uh the first thing i'm going to do i oh i forgot i have two switches uh now i'm going to ping 10 the no what am i doing 192 168 at 1.110 so i have been with this and the other switch the one i just got is 111 all right so as you can see i have pink and now i'm going to connect to one of these switches i'm going to start with 1010 so i have that and i'm going to type ssh oh and we are going to be able to do a password academy 45 exclamation this is my local my two switches are belong to me so there is this is not a network or something and i am giving away the username and password if you hear some spinning of some fans because the switches are really close to me so i'm sorry for that so enable as you can see this switch is called my id workshop number one we have work with this switch in many other videos and the password is a 2k 45 exclamation so i can show for example the version of the switch and i can check everything that i might like so i'm going to connect now to the other switch ssh um 192 168 that one that 111 so this this is the new one uh academy 45 exclamation and it's the same password educate so you can see it there so this one is called my id workshop switch number two so show ip interface brief for example and it's going to show me all that so i'm just showing you that this is um another switch and this one over here is the interface gigabit 48 the disconnect this is a trunk port that is connected to switch number one so that's why they are connected that's how they are connected so now we are going to continue uh scripting so i'm going to say i'm going to create this variable list of switches so what is going to happen is i'm going to ask the client to tell me how many switches are you going to be working with today okay so the next variable is going to be number of switches and this one is going to be an input and it's going to be um how many switches how many switches to like that so this list of switches uh is going to be it is a it is an empty list and i'm going to store the number of how many switches the the client is going to work with this time right so i have this and since this is going this is going to be a integer it's going to be one two three four five six seven one hundred i don't know i know it's going it's not going to be a string so i'm going to save it as an integer all right so we don't have problems later then we're going to do a 4 4 switch this is a variable uh in range from 1 well reach range one two this variable number of switches number of switches plus one y plus one because if you know python and you use the range function it's going to grab the first one and the last one minus one so in the case of we have two switches like i mentioned we're going to use one and two and if i leave it like this it's going to use only number one if you have ten switches you would end up with nine so that's why we're adding number one all right so the next step is going to be well we have this this four and another variable i'm going to create it's called ask and again input and here i'm going to ask enter switch number uh a space and we i'm going to concatenate it i'm going to make it string with switch so i have it like that it's not this one it's like this so i have it like that so what am i doing here so for every switch in the range which are going to be two we know that uh the computer is going to ask well the script is going to ask input enter switch number and here is going to uh show one or two if you have ten one two three four five six seven eight nine ten right and the reason i'm putting and turning in turning it into a string is because in other videos i have an error about this because you cannot concatenate and string which is this one with an integer so instead of having that error i just decided okay we're going to turn it into screen i screen a string to this one all right so that's that's pretty much and what we're going to do with that information is we're going to add it to this empty list so a list of switches lists of switches append and we're going to add that information ask so what i'm doing is every time i put something here then the name of the switch or the ip address in this case we're going to use ip addresses for the switches because i don't have a dns server therefore i cannot put um dns names it's not going to work i just i just tested it and it doesn't work so the ip address are going to be the way to go but if in a corporate environment of course you can use the switch name that way is easier to recognize instead of remembering switches ip addresses especially if you have 10 20 or something so now i'm going to populate with every switch ip address is going to be added to this empty list all right then what we're going to do we leave this as it is we don't really need it and and oh we have more questions to ask of course how did i forget so i'm going to have the user input which is going to ask the client for what username are you going to use to log into the switch or router so what's the username what's the username space and we have two other questions so what's the password what's the password and the variable is going to be pass now for these switches i have at home i have two different passwords password for the ssh and the password when you type enable so you can make configurations and all that so for that i have i'm going to call it an enable pass okay and it's going to be technically the same uh what's the enable password right so in again in a corporate environment both both the password and the enable password most likely are going to be the same which is in any case your pa your password that you use to log into windows or linux because you have a radio server so but in my case i don't have that so input we have this and well this is going to stay as it is and now this is the part where you actually connect to the to the router or switch this is what allow us besides the net micro library of course uh to connect using ssh to a cisco device all right of course the cisco switch needs or router needs to be configured in advance for ssh right so we have this and the only things we have to change here this is from the last from a few videos ago since we are asking for variables we have to change this so host is going to be um what's going to be the host well which one of my um variable is going to be switches a list of switches oh i need a four of course i'm forgetting about that local computers are made that's not very important well actually it is switches in list of switches here's my list of switches over here so once i have this the name of the names of the switches that we were asking uh over here and now it's saving the list of switches uh we are going to do all this so since we are inside of a switch uh of a four we have to do this like that that way it's not going to give us an error and these guys over here need to be like that okay so in this case this is going to be the host so the host is going to be the switch or switches in this case so we are just going to paste this variable over here because before we only have one switch so we we didn't need that so the username is going to be user over here so we are going to replace this uh the password as you can imagine is going to be password this is the ssh password uh the cisco ios doesn't change for us then the secret or the enable password is going to be this one like i said in most cases uh these two passwords are going to be the same so you are going just copy pass over here and it should work with no problems so we have that now right enable oh of course this has two no no those are variables what am i doing so we have this this part this is a variable this is a variable and this enable this part for example is a net michael library command so that you cannot change that and this part also all right so again that's explained in the video that is in the video description below now what we are going to do next is we are going to create a list of uh commands so this list of commands are going to be the commands that we're going to issue to these two switches or 10 switches or how many you may have so i don't know what we can do for example let me open up this one and uh if i kick me out okay this one is still working so let's say show running config pipe include host name so as you can see it's returning the hostname so we can use this one so we should receive uh different host names for different switches of course this is any these commands can be anything that you may like doesn't make a difference but i'm just testing those so the other command i want to run could be show version so if we run this of course i show you a few seconds ago right show version is going to give us this like that and the next command is going to be show will and brief i'm just testing i'm just going to run three commands and we have this so these these two switches are not fully configured i mean they have a few vlans the ssh the username and password blah blah blah but they are not for production they are for my home i play with these switches so they don't have really complex configurations so one thing that i know is that this switch number two has vlan 20 but switch number one doesn't have bill and 20. so that's those are one of the differences okay so we're going to check that later but it's just some the few differences and like i said show um bill so with that we are done with the commands as like i said you can have as many commands as you may need now uh we are inside of a four and we're going to do another four for command you can call it anything that you may like just a variable in list of comments so for every command in this list what are we going to do we're going to create a variable output and now we're going to actually execute the commands we're going to send the commands to be executed in the switch or in the switches using the net micro library so for that we need this guy this variable over here and now uh we are going to start uh adding the commands for nico so it's sent that no it's not that it's command command command and here is this command i know it is kind of confusing but this part over here it's how the let me call it's a command inside the net nico library so we cannot do anything about that you need this variable which is this one we have created it and this send command is from mico and this command that looks the same as this one but they are not related is the variable from here that can be anything can be i can be commands can be anything that you may like just commands make sense for me right so we we are there so this is the output by the way this part is not needed all this line is just to print it in the in the net micro what what this one is called in the in the python shell but it's not really it i'm going to show you in a few more minutes so uh after that we now we are going to save the configurations we are going to send to the switches in a txt file that is going to be saved here in this folder so this folder as you can see it's empty but then it's going to be populated with files with one file actually so this is the path and we're going to just copy paste it and a few more spaces and now we're going to say with open and this is how python allow us to open a file in the windows computer so i'm going to add the path which is this long for this folder over here but like i said in the last video we didn't use this one so now we are going to use it so this one is return back the username for this computer in this case salvador doesn't make much difference but i'm going to delete this and i'm going to add oh we have to do this plus plus like that am i doing something wrong local computer uh local computer local username and computer like let me make this a little bigger and this guy why is like this oh oh something i forgot as well every time you have one of these you have to double it because python is going to interpret it as something else if you don't do it that way that's for windows for links i i don't think it's that way i don't remember so we have that uh python txt and of course i need the name i'm going to just call it config config.txt so this config txt doesn't exist here so it's going to be created once we run the the software the script so um that's yeah that's pretty much oh this is not correct why i cannot see it [Music] so let's make it as big as possible so as you can see the icons are a little bigger i did that on purpose so you can see it better what i do so with open oh i need to close this so and now i'm going to write oh no i'm missing something i am missing something uh missing something which is this one this is just how python does it and as f this is how you do it i mean this part it's not something i'm making this up this is how python allows you to write a file so this is all python by the way this f can be anything it can be b can be c can be n can doesn't make a difference so i'm sorry right so i want some space i want to start typing in the next line um i'm sorry so if right and now switches as switches yeah that's good plus this symbol the pound symbol and plus the command then we close this and we have to keep on typing so i'm going to do this again more spaces and you're going to see how it looks so here what we're going to do is technically we need all this but i'm going to f right and we have to copy all these commands over here so i'm going to tell you i'm going to explain what it does in a few more seconds and now i'm going to copy this one more time over here so what i'm doing here is i'm going to open in all this location and now i'm using the result from this command over here which is who am i who am i um desktop python txt that's this folder and config.txt i'm going to create that folder that folder.txt file and now i have a space i'm going to print a space and then i'm doing i'm going to save switches which is what switch we are one of the two we have or can be 10 can be 100 and we're going to add like this so the reason i'm doing this is not really necessary but i want it to look like this like you see here but of course like i said i don't have a dns server so you're going to see the ip address here okay for me that looks more readable and the command we're going to execute then another space and here is i'm sending the actual command well i'm not sending it i'm saving the the result for that command all right the same that we have uh over here it's exactly the same so i'm going to save that that command and the no not the command i'm going to save the result of that command all right so now that's after inside the four and now i'm going to open open it again all these one more time outside these four and the reason for that is going to be exactly the same exactly the same and here i'm going to write another is one not not space and here this is after it finishes all the commands that it has to run in these four so all the commands it has to run it comes out yeah it doesn't have anymore so it's going to start the next switch so before it starts in its suite the next switch i want this to do something i want it to print kind of the same like this so for example every time you execute a command let's say show for example you you it's going to print this again right so i want it to look like that so that's the only again it's not necessary but for me it looks a little better so here i'm going to print this but many times and i learned this a few hours ago maybe [Music] so where is multiplication or three so this is going to print three spaces so instead of right typing this three times i can do this so that's pretty cool and after that i want to put i want to show a a command so this one is going to be like this and it's going to be end of this device so we know there is a separation end of this device and i'm going to repeat this a few times like this like this and like this and i i want many of these as well so i'm going to multiply it by i don't know 4 for example so we have this next more spaces of course more spaces and at the end of everything i wanted to print completed so this is not going to be in the file of course it's outside so complete it and that's that's it so i i believe i might have mistakes so let let's see what happens so i save it we're going to run the module and it opens it in my other screen or monitor so what is this where is this where are you where did you go okay run it again oh here you are so how many switches two switch number one 192 168 that one uh well i don't like this it's a little uh i want a space there so okay but 192.168. that one that 110 what's the number two it's technically the same plus one number the bank was the username alvendry which is me what's the password academy 45 exclamation and what's the enable password which is educate 45 and let's see if i ha if i have an error so enter done i suppose oh i didn't type print i didn't i print for this so it's not going to show anything in the cli in the python what's called in the python shell so it should print completed oh yeah it's printing completed so as you can see here this folder now has something so a file has been generated so i'm going to open it as you can see here like i said the ip address it should be the name of the switch but i don't have a dns server and the command that is running so as you can see this is what it's printing and then show version show version is very long uh until here and now show build and brief for switch 110 so all this is printing it here and as you can see here show bill and brief i it's only showing us this like i said the vlan switch 110 or switch number one my id workshop number one doesn't have vlan 20. so you don't see it here so then after it finish it it prints this which i like and then this message like end of device and the end of device end of device which is pretty cool so you know uh it has done it it's done with that switch or device so now it start switch 111 so running config you have the same hostname no problems here so now we run show version which is very long and fine i'm going to make it a little smaller even a little smaller so finally show villain brief and as you can see here we can see vlan 20 which we don't have in switch number one and at the end the end of this device end of this device that tell us hey we have finished and the complete is not being printed because that's not inside we are not saving that to the txt file so that's pretty cool so that's how it works so now you might be telling i'm going to close this you can tell me hey i want to save this but in separate folders well not folders files so i don't want everything let's say you have 20 switches or 20 routers and you don't want to save all of them in one file all right so what you would have to do it's over here config underscore you can call it anything that you may like i'm going to close and this add and what would be the variable the variable would be switches because we are looping through the switches plus tx t plus the txt file so i'm going to copy exactly the same for over here oh i need this one and what did i copy uh yeah i think it's okay so that's going to create different files so as you can see here we only have one file and let's change this because your version is very long show and we run so this command is to check the health of the fan and the temperature and the power supply so in veron show and we run what what is mv there's something with this show environment odd maybe i'm making making up a command so what's another command that could be interesting show bill and brief show ip addresses oh show cdp enabled so we are going to replace this one show cdp networks we're going to change it for this command show cdp network so we're going to have that instead of show show version so now we're going to save it and we're going to run it again run module it's in the other screen and we have to type everything again to oh i don't like this i don't like this so we are going to go up and we need a little more space so username and password where is this oh here so we save it again and run so it's in my other monitor come on why don't you come it's not it doesn't allow me to drag it so i'm going to run it again so run now nothing is running guys it's stuck what the heck why is not doing it okay so let's just kill the uh what is python python python python python that's python why are not why are you not working all right so i'm going to open python again so open um where is uh [Music] version three which is here so we're going to run it again i don't know what happened i don't know what it got stuck but we're going to run it again okay it's the same uh it's the same thing we changed this the last time so we're going to run it let's run module uh how many switches two 192 160 now now we have a space 110 192 168.111 username alvindreel password account me 45 and what's the enable password educate 35 so one thing i didn't find a way to um what's what's um to to hide these two in plain text plain text i don't know i i check many other libraries but i didn't i i'm not able to do it so i don't know what to do about that if you know a solution let me know in the comment section below so i can learn something as well so as you can see here is doing something is running the commands and again i didn't i it's not printing it so we can print it in a few more seconds so we're going to wait until this finishes and we're going to see what happens so it should be running all the commands and it's done so let's open this and as you can see i have two extra files uh is a config file for 110 and it's only doing it for that switch so 110 show the host name then we have the show bill and brief and where is the uh show cdp enabled so that's pretty cool so we can see switch number two over here in interface 48 and 111 so this one it's only for switch number two as you can see here so sleeping networks the host name and we can see switch number one in port 100 1447 so that's pretty cool so maybe we don't really need this right because it's only doing it once we don't need that separation so as you can see it's creating many many files well not many it's created files so that's how you would do it if you want to separate txt file and this is where is the part i'm looking for uh device connect device oh here's the output this is the part that is not printing so what we can do here it's well printed it so oh we have this and we can print print output print output over here and we're going to run it one last time so we can it's going to rewrite it's going to add it so we can just delete this so we delete this it's blank and we're going to run it one more time so how many switches to 192 168.110 number two is going to be kind of the same with a little variation eleven i'll then drill academy 45 and educate educate 45 so let's see what happens now it should print it in the cli let's see as you can see here it's doing it so you have all that so that's the host name i'm going to make it a little smaller because it's not fitting in the screen so total cdp so it's completed so now you can see it printed here like i said that's not really needed so these two commands are let's say optional it's just for you to check if it works and again it has created two files like as you can see here so that's what i wanted to join this video guys but maybe you want to shut down many windows computers maybe you're in a windows domain i have this video for that or also you can check these other videos that might help you to do more python projects so thank you very much for watching if you like this video please leave a like and subscribe if you have any other comment or question leave it in the comment section below and as always i'll see you in the next video
Info
Channel: My IT Workshop
Views: 2,469
Rating: undefined out of 5
Keywords: My it workshop, how to, Automation with Python - Login to Multiple Cisco Switches, cisco automation with python, configure multiple cisco devices with python, how to script with python, scripting with python, cisco catalyst 2960, network automation, python cisco, netmiko python tutorial, cisco python script examples, cisco python for network engineers, python cisco login, netmiko library, python programming language, it automation, network administration, computer networking
Id: LDEtyv4GNiI
Channel Id: undefined
Length: 37min 28sec (2248 seconds)
Published: Wed Jul 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.