粉丝1153获赞7303

now that rottress has its own scripting language most things that you can imagine automating on your router can be scripted little niche features that would be nice to have just be added by yourself and you don't need a computer size degree if you have a basic understanding of how scripts work then you can script and rodras so how does rodra scripping work similar to popular languages like bash and python you can script directly into the terminal which is convenient when you are trying things out and checking if a particular command would work as well as when you want to execute multiple commands in order, but whenever you come up with something work well of course you want to store it as a script so that you can execute it whenever you want to in route rests instead of creating script files you store your scripts in this system script section this just helps with permissions and also keeps things organized let's just go ahead and add a little script so that i can show you the very basic principles all that is needed for now is a name for the script as you can see a bunch of other parameters got generated, but we don't have to understand them we can just begin editing our source now that puts us in the built inscript editor nothing fancy just a text editor that will highlight the syntax here we can enter any routerous commands just like we would in the terminal so the most basic script could consist of multiple lines of configuration, but the possibilities are pretty broad to say the you are mainly limited by your imagination here as we have all the fundamental building box available starting with variables there are 11 and data types that you can use, but you don't have to specifically declare them beforehand they are automatically recognized when you assign value to the variable what we have to specifically declare however, is the scope as we have two local and global to create a local variable type comb symbol followed by the word local space and the name of the variable you can leave it like that or add a value after that a number will declare it as a manager similarly characters would create a string and typing through or false wood result in a boolean the global scope variables are declared just the same next let's bring them using the put command as we'd all write rest commands it has to start with the column symbol and to get the value of a particular variable we have to use the dollar symbol without a dollar put would simply output the name of the variable as a string now we can save and close the script using ctrl plus oh and then we can run it as you can see both variables got printed the same, but the difference here is that after running the script the local variable got removed, but the global is still stored in the iran you can verify that in the environment subsection if i issue upput command it still returns the value the global variable could be handy when you are building your own little ecosystem of scripts, but needlessly using it is not a good practice and should be avoided to clear the variable from the ram we can reboot the device or issue a remove command the local variable is the safe option we want to use whenever possible the only thing you have to keep in mind is the limited scope if i put curly brackets around a local variable like this anything calling it outside those brackets will fail so this put command would not work anymore roger scripting supports all the typical arithmetic operators so i could multiply this number by two for example the set command lets you change the value of the variable just make sure you place your mathematical operation in regular brackets multiplication works fine, but arithmetic operators are not the only ones available for example there is no power operator and if you want to use the typical symbol it will actually perform a bit wise gazor operation this script now returns 121 because the bit resembling to got flipped down on that variable all sorts of quirky things can be done i won't cover all the upraiters now you can look them up on the help page if i want to actually square this variable i have to simply multiply it with itself next i could do a conditional statement after the if you could do the condition in brackets and then you type do equals and place any actions inside curly brackets these can be multiline action i will leave it as a one liner and run it works just how it should finally this wouldn't be scripting if we didn't do loops and functions so let's edit the script one more time and create a function that will be called by a loop functions are defined the same way as variables, but in place of value we place a do action let's output the value of dollar one which is the first argument that the function receives followed by the squared value of that number notice there is a dollar sign before the bracket with the multiplication this is necessary to execute an operation in the middle of a string then let's do a simple for loop with the integer eye going from one to nine and have it call our function using the value of eye now when i save it and run it i get a cute multiplication table when you consider that generated variables like this could be used to automate configuration and rotres has built in script trigger mechanisms this scheduler did not watch and dhcp actions then you realize that you could script almost any unique feature or router behavior that you need many people have written useful scripts for route rest and if you search online you will find countless form articles and even entire github repositories to learn more about different scripting commands and operators visit our help page the link is in the description。