Network Automation using Python(Paramiko Module) on MikroTik

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Why Python over Ansible?

👍︎︎ 5 👤︎︎ u/Dark_Nate 📅︎︎ Dec 12 2021 🗫︎ replies
Captions
hello everyone and welcome to another edition of network automation so if you have been following me i had done four videos on it already if you haven't checked it out you can click on the link above and see all the videos and what we started was with understanding why we need network automation and then we understood how we can test api through poshman postman is a great tool if you haven't used it you must start using it it's one of the best tools available to check your apis and then we learned how to use python to integrate the same kind of apis directly into a code and make an automation script and in the last video what we did was we installed netmeco now netwecho was not easy to install especially because we are working on microtech to overcome that issue what we are going to do today is going to install the parent module which netme also uses which is known as parameco so let's get into it and let's understand how to install parameco how to operate with parameco and how we can build scripts with parameco today so if you have not seen paramecos still uh you can log on to panamico.org and parameco is a pure python 2.7 3.4 plus implementation of ssh v2 protocol it provides both client and service functionality and it proves the foundation of the high level ssh library fabric so what palomico in turn really does is panomiko gives you a raw ssh connection to your device and you can execute all the commands that you execute through command line interface that is through your ssh telnet or console so parameco will help you do it so to install panamico let's see the installation instructions and it's simple itself all we need to do is pip install parameco and as always we will be using pycharm and in pycharm let's get into the terminal and let's just do pip install parameco now i had already installed it uh some time back because i had been working with it i've been trying to play around with it so if you have not it will just do a couple of installations of some repositories that it's dependent on and then the main package itself so how do we use parameco let's get into the api docs on the website and let's see how we can use parameco inside the api documentation let's get into the client because we will be using parameco as a client ssh our router is going to be the server here so it's pretty simple or we can copy paste the script or we can make our own script also let's do a couple of changes and let's make our own script so in pycharm let's import palamiko this is going to be we want to import the whole module into it because we'll be using it then let's make it object client which is going to be primaco dot ssh client now this ssh client is already defined in parameco and we will be using this to work with our client that is going to be our router so let's go back to the uh parameter documentation and see what we need to do next we can copy paste this but we are not following the same path uh because in the palamico script it's saying that to define client directly to ssh client but we are using it as an object here so there are some changes in our script here plus there is one more issue if you scroll down a bit uh there is something known as set missing host key policy now this is not defined there it is assuming that the host key that is the key of your server is already in your known host database so if that's the case that's not going to be an issue but if the host key is not present the script will not run so to avoid it what we need to define is set missing host key policy and what we are defining here is default policy of auto adding so let's add this to our script so here what we are going to do is we are going to say client dot set host key missing host key policy and we are going to do is parameco dot add auto ad policy and open and close parentheses and what this is going to do is if the host key is not present in your known host range then the ssh key the rsa key is automatically added here the next thing we are going to do is we are going to define how we want to connect with our client so with pycharm we get a very good option we understand that what we need to do so we need to give hostname port a username and password like all the ssh clients that we make what we'll do is we'll give the host first so my host is an ip so one zero three five o triple two dot nine three the next thing i'm gonna define is a username and my username is api and the next thing that you need to define is your password so here let's say my password for my case this won't matter the reason being because i'm not using password to log into my ssh so in my case i'm using a public private key interface so defining password doesn't make any sense even if i do it's not going to be looking at it what i need to define is a parameter which is key underscore file name and i give a path to my rsa key now if we see the example the example is pretty much going the straight ways we are defining the client here how to connect to the client and then what we are defining is stdn hd out and http errors so what palomeko does is palamiko gets a tuple back from the uh client and the tuple comes in the format of hddn hd out and std errors so we need to give that tuple same tuple we need to store and execute our commands with so let's do this let's copy this command and let's paste it here so our router doesn't understand what is ls hyphen l so let's do interface print so what we should get back is we should get list of interfaces back so on output is going to come out in std out so what we can do is and let's print what is the type for std out because this is not going to come out in string so there is no point of printing this it will just return an error and before we do execute this commands what we need to do is one more thing in parameco we need to close the client why we need to do this otherwise bamiko will remain connected and the session may hang so we don't want that to happen so let's close the client that we have connected to and let's try if our script runs or there are some errors so our code ran and there were no problems and we got back that the type here is parameco.channel.channelfire okay so this is something that we cannot work with right so how do we read this file what is the output that really came out from our router so to understand this let's do one thing to decode this let's do one thing and let's print out this time around what if we did an std out dot read and what happens at this time around so this time around when we ran with std out talk read we came out with the class bytes the output that is coming is in bytes now we can just simply print it but this is encoded so let's decode this and let's put another method here of decode and we are going to decode in ascii format so as key so we are going to put it in single quotes ascii is something that we will understand and let's see what the output is and this time around the class is string so now if we were to print out just the whole method and not the type and we should be able to read it out yes we are able to read everything we are getting the column names uh we are getting all the interfaces that are there on our router the mac addresses the layer 2 mtu the actual mtu the type and everything else and as we saw this is all a big bunch of string so if we were to execute a bunch of commands how we can do that so let's understand that now now that we know how to get an output of a print command we can also input some commands so let's do that so to do that what we can do is uh let's create a new list here let's call it command commands for macro take and let's run a bunch of commands say interface bridge add name is equals to testing one two three and then we want to also add a command we want to add an ip address so ip address and address equals to 1.1.1.2 for our interface testing one two three so what we are doing is we are adding an interface a testing name one two three and then we are adding an ip address to that interface that says slash 32ip address to that bridge interface for 1.1.1.2 now to execute these commands what we need to do is you've got it right for line in commands for microtech what we need to do is the same thing that we did earlier instead of just executing one command we are going to be executing a line here so the line will go through iterate through each of these items in the list and will return an output so i see that i have done an error here i need to remove a square bracket here and in the end let's do some print command and we'll iterate through that also also what we will print is interface print we want to see what all interfaces are there now in our system and the next command that we would want to see is what are the ip address in our system so let's do print of two commands and as same as before we can just copy paste this series of commands we can do a for loop here also and what we are going to do iterate through this time around is not commands for empty but we are going to iterate to print command list and the commands that are going to execute here is list command so sdin out and we are going to execute line and then we are going to be printing out the hdd out so let's print out our std out dot read and we want to decode it into ascii format so as key in single quotes and this should work fine let's test out our code and see what happens here so our code ran perfectly well what we see here is that there is a new interface added which is testing one two three which is a bridged interface and in the end there is an ip address added to it so what we have been able to achieve is very simple thing we have been able to add an interface we have been able to print the command to verify that we have added an interface now we can use the same set of commands that we use in our command line as i said before to even change a parameter of an added command so for example if you were to change testing one two three name to say something like parameco which is more meaningful we could easily do that also so with this we come to a close on the network automation in the command line interface specifically for the ssh the two modules that we can use are netmeco and parameco with netmeco we were able to achieve the similar thing but with netmeco there was a problem that the lines were not getting printed properly and we had to use a better module for that specifically for our microtics now with panameko there is no such problem we can use the stable repository and we are able to achieve the desired result next up with microtech what we want to do is we want to use the sdk that is the software development kit api interface of the microtech and get the results from there also why do we want to do this so in my last video i ended with can you make this as a structured code why did we wanted to make this as a structured code right now everything that is coming out in this code is a string since this is all coming out as a string it is not very helpful why because to search through something we'll have to put in complex codes and there is a problem that can always come with the complex code we want our code to be simple readable and efficient so to do that what we can do is to make a structured logical code we can use the sdk module also we can use the api module but that will not work in version 6. that will work in the new version 7 that came out a couple of days back so how do we gain an edge where we can use python scripts with our old microtech router os versions we don't want to upgrade everything in one go because we do not know if the 7.1 is stable or not and also if you want to upgrade we would want to upgrade it through our networking scripts and automated scripts so that we don't have to work much on our remote devices so how do we do that i'll be coming out with a video on that very soon so stick around and do let me know what you think about my today's video and do subscribe to my channel and do like this video and please do comment to this video below and let me know what you think about this series and my channel in general i would love to hear from you guys also that will be great till we meet the next time around goodbye
Info
Channel: MKSIT
Views: 401
Rating: undefined out of 5
Keywords: mikrotik, network automation, mikrotik paramiko, mikrotik api, mikrotik python automation, mikrotik nework scripting, python network automation, python paramiko, mikrotik ssh, mikrotik troubleshooting, mikrotik mcna, mikrotik training, routeros network automation, routeros paramiko, rest api, postman introduction, api testing, mikrotik routeros v7, mikrotik rest api, mikrotik automation, python rest api, ssh python, rest api automation testing, python project
Id: iKmny3v1tb8
Channel Id: undefined
Length: 16min 9sec (969 seconds)
Published: Sun Dec 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.