Automation Using Python | SSH to Cisco Device

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Ida pros some welcome back to my IT workshop this is Alva drill and in this video I'm going to show you how to create a script using Python of course to connect to a Cisco device in this case is going to be a switch but I have also tested on a router well on routers alright the first thing we are going to need is as you can imagine Python if you don't have Python it's so you have to go to this website and python.org any link I talked about or I showed you in this video is going to be in the video description below okay so you come here to downloads and you just click here download for windows again not again I'm using Windows 10 for this but I call so if this can also be done in windows 8 to Windows so so the latest version of fighting as of today is version 3.8 that three so you click on this it's around 25 megabytes you can imagine I already have it I already have install but it's all here it's here may be happening so but you have to verify this so you double click on it and you can see here install python 3.8 dot 3 if i'm not mistaken the library we need is net Mikko and it works on all version 3 so i'm i tested in version 3.5 and it works well okay so here and you sell it but you have to click this add python 3.8 to path and if maybe you haven't insolvent if you haven't done this you most likely won't be able to download the library which is met Michael and 2 in there is a way to add it even if you have Python installed but in my opinion it's too too much too much of a problem so I just decided well no decided I would just rather to initial it an Excel fight on again so this has to be selected ok don't forget this this has to be selected and then you click and so I'm going to cancel it because of course I already have it in sort of my computer so the next step is when you have installed with the path box checked you open a CMD and premiere you have to download the name the net miko library so in order to do that you have to type it three install - you met Michael and you press Enter and the library is going to be downloaded to you if you press ENTER and it tells you that the command is not recognizable that means you don't have python added to your path remember that check when we were installing it you have to check on that okay well you have to check that box all right the other option is this is the one that work for me but this is another that it's supposed to work as well install net nickel and you press ENTER and it's going to solve the installation lasts less than 15 seconds so it's pretty quick and it's just going to tell you that Michael has been install that that's it then you have any saw so the next step is I'm going to show you that I have pink with my switch 182 and 68 at 100 110 so I have paint with it so now I'm going to connect using SSH okay of course the video is about connected to a Cisco device using SSH a way to do one in the future for telnet so that way you can do it but as you know telnet is not secure SSH is secure so now I'm going to do the same and it's going to ask me for my password so I just type it and there you go mice which is called my IT workshop number one so I'm going to enable it password it's not the same for security reasons and now I have this switch I am just going to show you the version enter enter and this is the model of the switch so it's a Cisco Catalyst 2960 48 ports and you can see they're there they I use version so I have done I've been in the past I have to upgrade switches Cisco switches if you want to know how to do that you can click the top right of the screen so now these switches I have used it for I have the switches at home because I use them when I was studying for my cisco certification you can also oh they that's why I have I have them I don't have many things connected to them so I'm going to show you so IP interface brief and as you can see the only thing active the only port active sport number one which is connected to my home router okay to my service provider as you can see everything else is down and you can also notice that these over here are administratively it down that means I went to the actual port and shut it down I type shut down so that's why you can see that me if you can add something to this it's not going to work however if you connect something to this is going to work even though it says down but it's not administratively yeah okay so this problem is from port 15 to 20 we're going to use that information in a few more minutes this one uses Gigabit Ethernet and this is the format that the the ports are used okay so see some later switches some newer switches use 10 Ethernet 10 gigabit right so just want to keep that in mind ok so I'll show you that this is a real switch is not emulated if you hear some noise is the fine spinning because they have it really close to me so I'm sorry for that so I'm just going to exit the switch and there we go so the next step is going to be to open Python I have it here as if you are noticing that the background is different is because I change I change it to darker color because I don't like very bright colors just because they don't just don't like it but this is how it looks for me so now I'm going to click Edit new file and it's going to show like this but I really have one open and created this one over here as you can see here it doesn't have anything I just renamed it I just not ring I just name it automate one so it's ready so now that you have Python installed then you have the net miko library downloaded like as i show you a few minutes so now we are going to start coding so now I'm going to type from well I can make that way from let me code that's the library I'm sorry up forgetting this let me cuz a library created by kirok buyers he is CC CC ie so kind of the kind of the higher tier in Cisco Network certifications and I'm going to let make us a big big big library I don't know I don't know all about nomicon I just know the things that I need and the things that are useful to me for Cisco because what I work we use Cisco everything Cisco but I know I have a check but I know according to their to the document the documentation that you can also use net mica for Aruba which is HP Dell switches or routers and what is disorder juniper that's what comes to my mind right now okay so you can use it with those as well but for me it's just Cisco alright so I'm going to leave the link in description for all the documentation so you can read further if you're interested so Frenette miko import import connect so I'm saying that from the library net we co import this what would it be we call a function I think it's a focus to the function I don't know exactly I don't remember the name right now what is called but you are calling a subpart a component of the big nickname eco library for this alright you can also call all the library as well you can type an important Michael but this is my opinion better so now we're going I'm going to define a variable network world device these can be anything you can name it be you can name a my switch my router whatever you want okay so it needs to be a dictionary so we're going to create dictionary with the keys and host keys and values so the first one is going to be host no Jose host which is my IP address 192 that I showed you this also works by the way we the DNS name in my case I'm going to use the da day just the IP address then we are going to go to the second which is username and my username I look up see we start with drill like the name I'd use in this channel then come oh I made a mistake here this has to be like that and Comic Con so next is going to be a password the password is going to be this one Academy 45 exclamation then missing a comma next one is going to be device type in this case make a mistake in this case is Cisco IOS and the last one is not when the last one we need I'm going to explain in a few second and the secret for the password to go to the global configuration in the street or router for me it's this one summation can be the same as the one above like Academy 45 but for security you know we're using SSH for security I'm using different passwords okay by the way these two passwords I create them for my switch I don't use them in any account on the internet so I just want to make that clear now the important part all these guys all these keys in the dictionary host username password device type secret you cannot you cannot change them change them it has to be like this okay all these the IP address or DNS name the username the password oh this is a mistake Cisco IOS like that I will leave here you can type juniper del Aruba or any other brand that is supported by the library and the secret password these can be changed but these over here this cannot be changed if you change it I have that if you change this by just let let's say instead of password you just type ass it's going to give you an error okay so these keys they cannot change all right so now the next steps which we are going to define another variable Connect connect connect this is net Miko one hundred percent and star star net network device so what I'm doing here is I am unpacking the dictionary if this is very confusing I made an example I have an online compiler again link in the description below I created a dictionary simple dictionary I call it with - with - what - kiss x and y and x is equal to 10 and Y is equal to 3 so I define a function which is obstruction so you imagine what's going to happen we're going to subtract X man from Y okay so 10 minus 3 so here's interesting part I'm going to call the function I'm here I'm using the same as you have seen in the script and simple dictionary so I'm calling and I am in the dictionary alright so I'm going just going to running and the result as you can imagine it's 7 so good to change this very cool for you to see it's going to be 13 so I just want to show you how it works what I'm doing alright so with that on the side this is the variable I created and this is the let me go 100% okay so next these are not these two are not related by the way these are not the same I just chose to call it connect because it's general so now I'm going to connect this is the variable enable okay so this guy over here should be this guy over here and it until here technically we have done so to save it if you don't know you have to save with always run module save it yes so let me bring it let me bring it to this screen we wait a few seconds and it's working I know I know it doesn't show anything how come it's working I'm going to show you but I know I don't have any error I know it's working so now this part over here is what you need to connect to any Cisco device well I did not say any routers switches I haven't tried on firewalls but it's supposed to work okay that's what you need now we're going to code whatever we may like well this part is kind of whatever whatever we want but by the way these are not the only keys we you ha you can have more but I don't need them for this example now I'm just going to define another variable that is comment you know what better I'm going to name another other thing I'm going just to name it to send right so it doesn't make it doesn't confuse if you are watching so the command I want to send better to issue to issue is better comment which the committee went to issue is going to be show feel and brief right so this is a cisco comment and now i'm going to print it print this is the interesting part connect that sent the underscore comment and this guy over here to issue all right you can actually type this guy instead of to issue but i'm just doing it in order so now we're going to save that ctrl s and we're going to run it we're going to see we have an error so we're waiting we're waiting let's wait and it's telling me that I have an error a tribute ever Cisco IOS what did I take their device type Cisco I was that's correct what's giving me an error Oh calm and maybe this is the error command set command to issue ok run run module again let's see if it works this time we wait a few seconds until it tries to connect to the switch and there you go we have seen the result here we can see all the villains that we have the bin and one is by default and I created these two villains for you to see so bid on 50 and below 100 as you can see will and 50 has poured gigabyte gigabit 22 so that's good and villain 100 has threads it's just an example so you know it's working so I can close this and that's how you connect to switch but we're going to do something more interesting now I'm going to comment this because I don't need them for now I'm going to leave this and we are going to replace the show feelin brief so we're going to go for a few ports what if you want to see the state of a port is it is it up is it down what's going on so we're going to do that for that I'm going to declare this variable first port and that's going to be saying you can't use anything that you may like but in this case my first day first of all I want to check is 10 and the last port is going to be 2 any alright so now I'm going to say what I'm going to use a for loop for a port in range my wrench is going to be the first port over here I'm just going to copy paste it so it's easier my aunt range is going to be last port but if you know Python you know I have to cut what why is that because the the way the ranch function works is that it's going to take the first port which is 10 but the last port is not going to be twin it's going to be 20 minus 1 ok so it's going to be 19 that's the way PI 2 works so that way I'm adding 1 so it's going to go all until port 20 all right so enter and we're going to print something here so the print is going to be technically the same as here well actually I'm going to copy it that's way easier now what I'm doing so print 2 issue but what we need to change this is the command to check the ports so what's the command well that there are many combinations but when I'm going to use this show IP interface I'd remember in the in the switch it follows this I'm going to copy this alright paste interface chikipede but I don't want 38 I'm just going to leave it like this and now what we are going to do here because we have replaced the command 4 to issue and here is where we are going to do some I'm about to say magic but some interesting thing so I'm going to add I'm going to add the port but it's going to give me an error because this one is an integer and you oh you can only concatenate strings so STR if you do it without STR is going to give an error you cannot concatenate integers ok no problem so we're going to turn it into in turn unstring and we're going to turn it into a string so we have that we don't need these we don't need that and we should get the estate of all the ports between 10 and 20 let's see if that works and if I if I have done it correctly so run module do you want to save it yes so let's see what happens now we wait we wait we wait invalid marker Y is given as invalid marker so there is something there is going to repeat it for 10 times now gigabit show IP interface Gigabit Ethernet maybe if we come here how did we do it I believe there has to be a space here so to issue Plus that let's put it this way let's see what happens run and let's see if it works this time so we wait you can see it's working so we have the result from we are getting the result automatically from port number 10 to 20 so you can see it's down it's down from 15 as you if you remember they are administratively down until 20 so we can check that so we can automate that kind of things instead of checking port by port or all that and the next thing you have to do is to LIKE this video and subscribe to the channel if you haven't done so yet so that's how you can connect to to a Cisco in this case a Cisco device again I have tested in a router it works with no problems run a Cisco router okay so that's how you can connect in the next video I'm going to do for two switches or can I get the other switch I'm not sure if I can get another switch but if not I'm going to do more commands we're going to do kind of a menu if you want to do use this command at work and all that and share it with another people all right so with that in this video I have show you have to connect to a Cisco router switch using the using Python but we are using also the net mica library if you liked this video please leave a like and subscribe if you have any other common or questions leave it in the comment section below and as always I'll see you in the next video
Info
Channel: My IT Workshop
Views: 9,720
Rating: undefined out of 5
Keywords: My it workshop, how to, python cisco, network automation, network programmability, cisco network automation, how to become a developer, how to write python, how to write python code, how to write python scripts, how to write python code for beginners, Automation Using Python, SSH to Cisco Device, connect to a cisco switch using python, netmiko python tutorial, netmiko ssh example, netmiko python, alvendril, connect to cisco router with python, cisco and python
Id: lwlg5wAhpPk
Channel Id: undefined
Length: 21min 36sec (1296 seconds)
Published: Sat Jun 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.