Scripting With MikroTik

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good evening everyone my name is showman Gupta Maia and I'm a micro tech certified trainer and today I would like to present to you scripting with micro take now every time I come to mom I've been here a couple of times before I try to present something new new features etc something which hasn't been circulating around much but today I have for you something that is very old it has been in there in the micro tech industry since the beginning and many experts have given presentations on this but I wanted some kind of a different approach to it because gripping can be a little complicated to those who come from a non programming or non coding background such as myself so like for example my brother who will be giving a presentation next he's from a programming background and when I showed him this script by the end of the first day he was doing better scripts than me right so I will take we can say a little class about scripting so the experts will pardon me for sticking to the basics and not going to very deep into the subject ok what is scripting a scripting language is a programming language that employs a high-level construct to interpret and execute one command at a time in general scripting languages are easier to learn and faster to code then in the more complicated ones like C C++ which are structured and compiled second is scripts or programs written for a special runtime environment that automate the execution of tasks now day to day you will have to perform many tasks in your network on basically on the router right what happens if you can automate some of them it becomes easy for you the if you do not automate they can be alternately executed by one to one by a human operator okay and they are distinct from the core code of the application so in case of even mikrotik scripting the core code of the router OS is slightly different from that of scripting because they are usually written in a different language and they are often created or at least modified by the end-user so scripting is the language meant for us the end-users to automate some functions in our network that is to automate some tasks and functions in the router itself why do we need scripting again I am repeating automation of tasks based on events not only based on events the events can be triggered automatically like a system reboot events like a day-to-day backup right automation of this that would otherwise require a person sitting in front of the system 24/7 right now repetition of regular tasks like creating same configuration for multiple devices say you have 50 devices and you want the same configuration for all of those devices so instead of doing them one by one you can use scripting to automate the tasks and batch creation and processing of lists and addresses etc like adding multiple addresses in a firewall address list right and obviously the most important of all monitoring the router is the Gateway up how much traffic is being transmitted received etc now we will start with basic scripting a simple command you know most of us have used the terminal window and used some commands time from time to time and the basic commands is just the simple one that you write in the terminal window for example the first one IP address print or slash interface Ethernet set either one disabled is equal to yes right these are the basic commands okay now there is a syntax for all programming languages so in this case we also have to follow some default syntax for this for example when you start a man often you have to start it with a cologne and end the command with a semicolon okay next variable names we use variables for various tasks and when we set the variable we just write the variable name but when we use it like printing the data of the variable printing the value of that variable we have to use a dollar sign so these are a few syntax that we need to keep in mind when we are doing the scripting okay then we have there are operators that we use with there are arithmetic operators addition subtraction binary addition binary subtraction etc and relational operators for example is equal to greater than is equal to is a if a is greater than equal to B then do a task otherwise to something else like that okay then we have + or operators okay so a and B both have to be true then some tasks will be performed or a or b any one has to be true to enable a task to be performed like this okay and finally we have got variables that we were just talking about variables are of two types rather variables over here have two types of scopes one is the first one is global and the second one is local now global variables are the ones which can be accessed by all the scripts that means once you declare a variable a global variable in one of the scripts it can be used in another script - okay the global variables can be found in a menu sub menu that we'll come back to later called environment okay you can find all the variables over there and a local variable is accessible only within the current scope that means if you're writing a script and declaring a local variable in front of it at the top of it then that variable will be can be accessed by only that particular script okay now variable declaration as I was starting we should always declare the variable at the start of the script for ease of understanding the script later on and we should always keep the scope of the variables in mind ok so if we try to access a local variable in another script then it was declared in we will not able to find the value there it will return an error right and the basics are the basics declaring variable like this the : global has to be there this is the command and where is the variable name then if you want to put the value you will print the value of the variable in a console or send it in an email we can use this command put dollar var see we are using dollar var over here not simply the name because whenever we want to use a value we have to use the dollar mark in front of the name okay then we have another command block often we want to log something into the log window it can be either stored in your disk or in the memory or sent via remote into another server so the command for that is block then the topic so if we forget that topic you will not get it in the block message though the syntax will be block then either info warning error anything any topic we want and then in within the quotations okay and finally we have got comments now comments are very important in a script because after you make a script say you are opening it and want to modify it say five days later and then you forget what the commands did what was for what so to remember or to understand the script properly you always need some comments comments are declared with a hash in the beginning of the line so even if you are writing a command and there is a hash at the beginning of the line the router we treat is as a comment and not execute that particular line okay finally we have the parenthesis or the brackets now each bracket each type of bracket has its own function the the second bracket that is the curly braces defines scope the third bracket there is a straight one defines command substitution and the third one is for grouping so for the first one the scope so the this command substitution means whenever we want to use any command inside another command we will use this bracket okay and the last one sub expression or grouping is just for example see dollar a - dollar B all we have a more complicated maybe we have a more complicated example a plus B in first brackets then again we start another bracket outside of it and again divided by C like that okay right structuring a script no this is very important when you write we are just going about the mechanics of the script right now so while we structure the script we always try to declare the variables at the top okay the first part of this writing the script is declaration of the variables then we try to set any values if we want to say initialize say I want to set the value of variable a to 1 and B 2 2 and then do some other jobs with it so the second part comes up setting or initializing the variables and finally comes the body here I'll mention that algorithms are very important that is writing in plain language the steps of the script or the program so when we start out with a start out writing a script we should always write step-by-step what we want to do with it just in plain language then it becomes easier for you to coat the whole script and then again if you have any problems with it it will make the troubleshooting easy so first declaration variables then setting of default values and then the body of the script like in this example we have the example is a TV NH example it's incomplete and it's a little complicated so just the example here the declarations then we have the whole body of it we'll go to it later on now basic scripting examples first the easiest the most basic one is regular backup schedules now everybody has got some routers or the other running and those configurations are very important for you okay so if by chance something happens to that router and you do not get it back the configurations back it is very tedious to do the whole configuration over and over again so we always take backups now backups are by default stored inside the files menu of a mikrotik router so it's there in the disk if any problem happens to the disk then you will lose the backup files so it is always good to imp export actually export means send the keep the backup backup files or archive it anywhere else you can archive by sending an email or you can upload the backup file by FTP and this has to be done from time to time say once a week or once a day even for more complicated ones the regular for the ones with more users on it maybe you need a daily backup so you can schedule a backup automatically by writing a script so in the first part of script we are just printing on the console running system backup then in the next command system backup this is the command actually ok save name is equal to rich backup that is the name of the file that we are saving it right then we again print on the console that system is backed up successfully and finally we use the tool email to send that particular backup file to a certain mail address ok so this is the just sending the back up doing the backup and sending it now we have to shedule it right the sheer dueling is done by System scheduler here is this command system scheduler add name daily backup interval is 24 hours that means every day it will make a backup of the system and send it to the given email address ok right see on event is the script that it actually runs so system script run HTS backup this is the name of the particular script now another example is changing gateway to backup interface now here in this example we are using both of the gateways that we are using are automatically added that means they're dynamic gateways they're dynamic routes they are done by either a DHCP client or a pppoe client so in this case directly disabling in the IP routes menu will not work as one of some of you will know so what we have done is we have just increased the distance of each default route to make them unavailable that is make them static not active ok so here in the first part we set default route distance of the DHCP client as 5 ok so the other one is by default 0 right say the other one is pppoe so the pppoe 1 is currently 0 so all traffic will go through the pppoe interface once we run this script ok now see another command we have over here is to send an SMS to a certain number that the Gateway has been changed now this is very useful because many a times you need to be informed of what's going on and off in your network so you can give here your own number and send a message using a 3G or 4G modem and send a message to your own number that the Gateway has been changed to 3G right so changing back how do we change back we do the same thing we said the DHCP client set 0 default route distance is 0 so now it's back the land gateway is now active okay so again we send a SMS the phone number and messages gateway change to land now advanced scripting now this was the basic zipping and most of us have done some of basic scripting so we have copied and pasted them but we need to understand the advanced scripts that are there in the system there are many scripts available in the mikrotik scripting the repository actually you have a website for that and advanced scripting starts with the first part of the advanced scripting is using loops loops are of three kinds first is the do-while loop do I'll does a set of commands while a condition is met or the other way around okay so if the do is in front and the while is back so the command will be run at least one time right but if we reverse the direction that is while some conditions do some commands so first it will check for the conditions and only then if the conditions are true the it will execute the commands okay so it can be used in both ways next we have a very common loop the for loop it's present in almost all programming languages for what it does is it repeats the loop for it repeats the loop for integer the first integer to the second integer okay so this variable can be anything for i from 1 to say 10 I want a job repeated 10 times I want a set of commands executed 10 times so my loop will be for I is equal to 1 to 10 step what is the step step normally is 1 so from 1 to 10 it will take 1 step each ok if you want to take the steps in even numbers then 2 so it will skip 1 it will do 1 and then skip 2 and then again do 3 so here the steps and finally whatever commands you want to execute ok and finally we have another one for each so say you have number of lists in your IP address list and you want a command run for each of them okay so if the lists in number of five the command will be run five times so what you do is you somehow write a script I'll go to the end later on and check the number of entries you have and for each variable that is the number of entries it will execute a command okay so this can be another variable a string or an array okay then we have our system parameters now often we require system parameters to do many jobs like we need the system times we need the system date we need the CPU load etc etcetera so various system parameters can be got through these commands system identity it will get you the name of the router that you have in slash system identity so : global system identity this is the variable name and this is the command so we have put it in the third brackets because we are executing a command within a another command okay so slash system identity is actually put in the third brackets okay then we have a system clock here you will get the date and the time if you want and finally you can get the CPU load and there are more functions that you can find easily okay functions functions can be called in a script now what are functions functions are used to make repetitive jobs easier that is doing the same task over and over again so say in five scripts you need to give you need the time and dev stem okay so why write it again and again why write the commands again and again so what you do is you create as function called say time depth stem time/date stamp and put these commands the first one the system identity or the system clock two of them into it and you can call that function in any script you want okay so you don't have to repeat these commands for each and every right this is an example global that is the function scope of the function like a variable a function also has a scope so a local function can be only is called in the particular script so generally we define global functions so global my func do return something the return command will actually return a set of values executed from it so here it will return the value of a plus B the sum of a plus B and we can put it the output will be 8c the dollar a and dollar B are actually taken from the this is equal to something and musicals so we when we use the function command we have to give the argument values that is the value of the variables inside it okay now we have more commands in advanced gripping the a very useful one is resolve it returns the IP address resolution for a DES net DNS lookup we have an example for this resolve command it is very useful where you want to know if you have the DNS name and you want to know which IP it is actually pointing to you can type resolve and it will give you the IP address okay then you have fine if you want to say parts along or parts through a list go through a list and find something find will find the first occurrence of the search term and the command is like fine then string then to find that is where so example put interface fine name ether so this command will find all the Ethernet interface that you have all that are named with either right so if you want to set certain values to each and every interface you can use the find command to find the ether once put it in a loop and give the same use the same values for all of them okay append is another command which appends data to a previous file this is very important say you are putting some system values or some CPU load and voltage time to time in a fight okay appending a file means the first time you use it you will get the system date and time the next time you use it the second time the first value will still be there that means it will just keep adding on in the file okay just keep adding on in the text or whatever file you one quick returns a specific section of a provided value that is say pick string start and so if I put pick ABCDE then 1 & 3 then it will start from the first one that means it will pick a then go to the third one that means it will pick a B and C and leave out D and E this is also important if you are trying to get something get some values from a two strings say you have a registration table entry there is a long registration table entry and you just want the pxrx value from it write d-x signal rx signal values from it so what you do is pick and you know the number of characters so place the character and you can get the exact TX and rx value of that particular client write client entry okay another one is get get is just ID the parameter and value this is to get various parameters from your various system properties right so say get ether one MTU right you can find it out from using this kit command more commands are there for example beep the first one makes a beep sound delay creates a delay of mention millisecond or second before executing the next command right so first one then there is a first command then there is a delay of 10 seconds then there is the next command this is also very useful and Len returns the number of characters in a string or a variable or even an array type of is the data type of the variable see there are multiple types of variable as I have mentioned before that any errors there can be strings there can be integers there can be IP addresses so Len will this type of will actually get the data type of the variable then we have the time that is okay the system time execute execute a script in the background and environment environment is where you declare the global variables and check the value of them okay we have more set parts except etc error very important error is a very interesting functionality where you can actually execute a set of commands if there are any errors in the script so if you are doubtful of executing a command say when you are executing something and it returns an error the script will stop in the middle so what we can do is put it in a do and while right and then on error we can put a different set of commands say we can print there is an error in the script okay or we can go to the next step of the script without doing any harm right without stopping it actually so example of a loop now I'll give you some advanced scripting examples an example of a loop is deleting all the received messages from the inbox so you have a SMS inbox in your router where when you receive the SMS is you can keep there is a maximum value of it I think about 15 SMS can be stored in it so if you want if you are receiving messages from time to time sometimes you need your inbox cleared so they it can be done using a loop see local SMS count local SMS count is a variable declared we are setting the SMS count how we are checking it out see in the in the sub-command tool SMS inbox print count only that means it will only give you the number of SMS is that are there in the inbox now what I will do I'll declare a local variable that is I which will be set to 1 and finally we'll start a loop forget the text dot dot lock will start a 4 Loup from Jade is from I that is one to SMS count that means say you have ten sms's so the loop will go from one to ten ten times and each time it will remove the number zero the first the top one SMS from it so it will remove all the sms's one by one okay another example which uses some loop and also many other things is blocking websites using DNS address now the latest version I think has the option of mikrotik router os has the option of entering DNS names in the IP address lists and the IP firewall rules but still it's useful you can actually create the address list within this example we have to create the eggs address list first so IP firewall address list add address we are keeping the address list when we are creating it we're keeping it as 0.0.0.0 okay and we are giving a comment on what the host name we are looking for say www.blender.org underscore blink net then we are creating a filter rule in the filter in the chain forward the destination address loop list that is all the addresses contained in the list host underscore blink net will be brought dropped or blocked okay action is drop now we are going for the script then we are so in the script we are defining the variables and then we are looping through each entry in the address list so look at this comment if the comment was not there it will be difficult for you to understand what we are actually trying to do so always comment in your script so for each I in IP firewall address list fine okay so the each number of address list entries are there the command will be executed do set list pick IP firewall address get dollar i'll is zero five that means it will get the number of lists that are the addresses that are there and for each list which contains the host in it the word host in it it will execute a set of commands what will you do it will set the comment in a variable then it will change the IP address and how to change the IP address it will resolve the comment that means we have wrote www.engvid.com in the comment right so this part it will look for the comment okay resolve what is written in the comment that means will resolve double double double dot blink Netcom so it will get an IP and put the IP in the address list okay so we are just entering the dns names in the comments and we are getting those sites blocked okay another example is fetching system information this is very easy setting parameters to variables so I have declared each of a one variable for each parameter that is local CPU load up time version and then I have set initialized the value by taking from the resource system resource get CPU load so you have used the get command over here for getting parameters from the system also and then finally we are displaying in console so set so each time we are here sorry put so here where we are putting we are actually displaying the whole the all of the information that we are fetched that is the router name uptime CPU load up version free memory CPU frequency etcetera in the console and finally we can also SMS it using the SMS tool to any number you want okay right so what can be achieved by using scripts it's only limited to imagination because you can have automated system monitoring you can have advanced load balancing then you can have extended functionality like you can have ddns using a script right and we can ease our repetitive jobs that is configuring all routers one by one better use a script one script to configure all of them at once okay and we can do much more only limited by our imagination and a lot of resources are available in the web so thank you for your attention this is all I have with the old and I think my brother will continue with something new with scripting and SMS so if you have any questions please so most of you are bored by now and looking and just wondering where the drinks that were promised are they will be here in a moment don't worry so no questions yeah please compatible is SMS modem actually there are a lot of compatible SMS modems available in India but the problem is that there the problem is with the setting okay so if you want any detail about the compatible modems and the you can have a list of them in mikrotik I think but most of them are compatible but there is a setting we have to actually disable the autoplay you see you know whenever you enter the modem in the system there is an autoplay you have to disable it so yeah yeah actually what happens is that I tried this you can actually make all modems compatible but then you can you have to enter the modem using hyper terminal then you have to use modem specific commands okay so that is a very critical thing to explain to you right now yeah actually what you need to do is you need to go to the hardware vendors specific hardware vendor pages and they have the data sheets over there in the data sheets the commands are mentioned actually mode mode command I can tell I can give you a list of compatible modems which can use you can just by I think the data photon data photon plus that one works okay then we have some wood upon modems also I will just give you a little tell you separately later on okay any more questions okay thank you for attention have a good evening hello I'm shota gotta and today I'm gonna give a presentation on router management with SMS so we all know we have a tool called SMS in the router OS but most of us while setting it up face a lot of problems so I'm going to talk about it so firstly what is a router management
Info
Channel: MikroTik
Views: 7,298
Rating: undefined out of 5
Keywords: mikrotik, routerboard, routeros, latvia
Id: oTtJ5Mvxp8g
Channel Id: undefined
Length: 37min 17sec (2237 seconds)
Published: Fri Sep 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.