gbudny93.RouterOS_Useful_Sc.../README.md

378 lines
11 KiB
Markdown
Raw Permalink Normal View History

2020-04-19 18:08:17 +02:00
## RouterOS_Useful_Scripts
2020-02-02 17:47:24 +01:00
> MikroTik RouterOS Scripts for various use and shared among different projects. Repository contains ready to use scripts as well functions ready to use in larger projects.
**Check also another repository with scripts related strictly to MikroTik CAPsMAN automation :link: [CAPsMAN Automation Scripts](https://github.com/gbudny93/MikroTik_CAPsMAN_Automation).**
**Here you can find script template that I use :link: [RouterOS_Script_Template](https://gist.github.com/gbudny93/8ad0899576407e5efe9323febb368796).**
2019-06-15 00:17:35 +02:00
2019-06-21 13:31:54 +02:00
![](https://img.shields.io/badge/scripting-routeros-important.svg)
2019-08-04 21:38:20 +02:00
![](https://img.shields.io/badge/mikrotik-routerBOARD-yellow)
![](https://img.shields.io/badge/network-automation-informational)
2019-06-18 13:04:28 +02:00
2020-04-19 18:08:17 +02:00
#### Change log
2020-07-03 11:30:16 +02:00
- 7/3/2020
- RouterOS_All_Software_Download.rsc - typos fix
- RouterOS_Array_Append.rsc
2020-04-19 18:08:17 +02:00
- 4/19/2020
- RouterOS_All_Software_Download.rsc
- RouterOS_Dual_WAN_Failover.rsc
2019-10-01 13:30:14 +02:00
2020-03-23 21:41:53 +01:00
- 23/3/2020
- RouterOS_Config_Fetch.rsc
- RouterOS_Measure_Execution_Time.rsc
- RouterOS_RB750GR3_DUDE_Setup.rsc
- 15/2/2020
2020-03-15 19:41:05 +01:00
- RouterOS_Cloud_Backup.rsc
- RouterOS_FTP_Backup.rsc
- RouterOS_Function_Template.rsc
- RouterOS_PPP_Disconnect_Active_Sessions.rsc
- RouterOS_Recreate_Bridge.rsc
2020-02-02 11:45:44 +01:00
- 12/4/2019
- RouterOS_RadiusMonitor.rsc
- RouterOS_User_Alerts.rsc
2019-12-04 19:29:51 +01:00
- 12/4/2019
- RouterOS_Auto_Firmware_Upgrade.rsc
- RouterOS_Port_Knock.rsc
2019-11-24 18:23:19 +01:00
- 11/24/2019
- RouterOS_R_ARP.rsc
- RouterOS_Format_Drive.rsc
- RouterOS_Substring_Variable.rsc
- RouterOS_System_Status.rsc
2019-10-14 20:53:50 +02:00
- 10/14/2019
2019-10-14 20:54:54 +02:00
- RouterOS_Mail_Backup.rsc
- RouterOS_Cloud_Backup.rsc
- RouterOS_File_Screening.rsc
- RouterOS_Renew_License.rsc
- RouterOS_Script_Fetch.rsc - Update
- RouterOS_Auto_Upgrade.rsc - Update/Clean ups
2019-10-14 20:53:50 +02:00
2019-10-01 13:30:14 +02:00
- 10/1/2019
- RouterOS_Auto_Upgrade.rsc
- Fixed missing auto check for updates to get variables values at first
2019-06-15 00:17:35 +02:00
2019-08-21 14:25:53 +02:00
- 8/21/2019
- RouterOS_Script_Fetch.rsc
- Fetches script from git to RouterOS directory
2019-08-14 11:09:44 +02:00
- 8/14/2019
- RouterOS_Auto_Upgrade.rsc
- Upgrades RouterOS if new package found when run
2019-08-03 10:48:07 +02:00
- 8/3/2019
- RouterOS_String_Generator.rsc
- Generates 6 char string based on dictionary
- RouterOS_Reset_Interface.rsc
- Resets interface if particular IP does not respond
2019-07-29 16:06:09 +02:00
- 7/29/2019
- RouterOS_Modulo.rsc
- Modulo operation
- RouterOS_Load_Script.rsc
- Loads RouterOS script to environment. If file is missing adds log entry
- 7/19/2019
2019-07-25 15:11:50 +02:00
- FTP_Backup_Template.rsc
- Schedules generation of *.backup* and *.rsc* file on RouterOS. Create FTP user dedicated for automatic backups. More on automatic RouterOS
2019-07-19 15:17:01 +02:00
backups can be found under dedicated project: :link: [Backup_mt by gbudny93](https://github.com/gbudny93/Backup_mt)
2019-07-25 15:11:50 +02:00
- RouterOS_Auto_Upgrade.rsc
- in progress
- RouterOS_Create_Directory.rsc
- Creates directory with specific name in RouterOS
- RouterOS_Log_Filtering.rsc
- Filters logs based on specified criteria: time or message and put in desired output
- RouterOS_Log_To_Alert.rsc
- Sends an email alert based on log message match
- RouterOS_Send_Email.rsc
- Sends email via SMTP server with common email parameters as function parameters
- RouterOS_File_Logging.rsc
- Adds log entry if file was added or removed
- RouterOS_LCD_Change.rsc (Applies only to physical devices with LCD)
- Changes LCD mode from dark to light and vice versa based on schedule set up
- RouterOS_Low_Disk_Space.rsc
- Sends email alert if disk space is under specified treshhold
- RouterOS_File_Screening.rsc
- in progress
- RouterOS_Eth_Errors_Handling.rsc
- in progress
- RouterOS_Config_Check.rsc
- in progress
2019-07-19 15:17:01 +02:00
2019-07-29 16:06:09 +02:00
- 6/14/2019 first release
2019-06-15 00:17:35 +02:00
- FTP_Backup_Template.rsc
- Schedules generation of *.backup* and *.rsc* file on RouterOS. Create FTP user dedicated for automatic backups. More on automatic RouterOS
backups can be found under dedicated project: :link: [Backup_mt by gbudny93](https://github.com/gbudny93/Backup_mt)
- RouterOS_Auto_Upgrade.rsc
- in progress
- RouterOS_Create_Directory.rsc
- Function that creates directory with specific name in RouterOS
- RouterOS_Log_Filtering.rsc
- in progress
- RouterOS_Log_To_Alert.rsc
- in progress
- RouterOS_Send_Email.rsc
- Fucntion sending email via SMTP server with common email parameters as function parameters
2020-04-19 18:08:17 +02:00
### Prerequisites
2019-06-15 00:17:35 +02:00
- :white_check_mark: RouterOS v6.40 or higher
2019-07-19 15:17:01 +02:00
- :white_check_mark: MikroTik CRS or CCR with LCD for RouterOS_LCD_Change.rsc
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
### How to use
2019-07-19 15:17:01 +02:00
> Use the following scripts to make your RouterOS management easier or use them in various larger scripts or projects
2019-06-15 00:17:35 +02:00
:+1:
2020-04-19 18:08:17 +02:00
#### FTP_Backup_Template.rsc
2019-06-15 00:17:35 +02:00
> This script creates two files via RouterOS scheduler as well creates FTP user and group:
- *.backup* file
- *.rsc* file
Script is a part of :link: [Backup_mt by gbudny93](https://github.com/gbudny93/Backup_mt) project.
2020-04-19 18:08:17 +02:00
#### RouterOS_All_Software_Download.rsc
> Automated download of all latest RouterOS standard packages. Tool recommended for CAPsMANs and DUDE servers
2020-07-03 11:30:16 +02:00
### RouterOS_Array_Append.rsc
> Wrapper to append array elements
2020-04-19 18:08:17 +02:00
#### RouterOS_Auto_Firmware_Upgrade.rsc
2019-12-04 19:29:51 +01:00
> Checks if latest firmware is available. Installs it and sends email notification
**Example**
```
$AutoFirmwareUpgrade smtpServer=smtpServer smtpPort=smtpPort domain=example.com \
recipient=recipient@example.com;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Auto_Software_Upgrade.rsc
2019-12-01 11:28:00 +01:00
> Checks if latest package is available. Downloads it, installs and sends email notification
2019-06-15 00:17:35 +02:00
2019-12-01 11:28:00 +01:00
**Example**
```
$PackageAutoDownload userName=userName password=password packagePath=path \
smtpServer=ipAddress smtpPort=poty domain=@example.com \
recipient=recipient@example.com;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Cloud_Backup.rsc
2019-12-01 11:28:00 +01:00
> Creates new backup file and uploads it to MikroTik Cloud
**Example**
```
2020-03-15 19:41:05 +01:00
$CloudBackup password=password;
2019-12-01 11:28:00 +01:00
```
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_Create_Directory.rsc
2019-06-15 00:17:35 +02:00
> RouterOS function that creates directory with defined name in the system. Can be used just to create directory or be a part of larger project.
**Example**
2019-12-01 11:28:00 +01:00
```
$CreateDirecotry userName=UserName password=Password directoryName=DirectoryName;
2019-06-15 00:17:35 +02:00
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Dual_WAN_Failover.rsc
> Simple setup for DUAL WAN failover. Main WAN must be commented as WAN1. Secondary WAN must be commented as WAN2
#### RouterOS_Log_Filtering.rsc
2019-12-01 11:28:00 +01:00
> RouterOS function for log messages filtering based on message or time match stored in chosen output.
2019-07-19 15:17:01 +02:00
2019-12-01 11:28:00 +01:00
**Example**
2019-07-19 15:17:01 +02:00
```
$LogFilter;
```
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_Log_To_Alert.rsc
2019-07-19 15:17:01 +02:00
> RouterOS function sending mail alert if log entry matches message criteria.
2019-12-01 11:28:00 +01:00
**Example**
2019-07-19 15:17:01 +02:00
```
$LogToAlert message="message" fileName="fileName" smtpServer=smtpServer smtpPort=smtpPort domain="@example.com" \
recipient="recipient@example.com";
```
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_Send_Email.rsc
2019-06-15 00:17:35 +02:00
> RouterOS function to send email via SMTP server. Function contains all common email fields like *to*, *cc*, *subject*, *body* and more. Function can be called itself in RouterOS or by other script.
**Example**
```
2019-12-01 11:28:00 +01:00
$SendEmail smtpServer=SMTPServer smtpPort=SMTPPort from=From to=To subject=Subject body=Body;
2019-06-15 00:17:35 +02:00
```
2020-04-19 18:08:17 +02:00
#### RouterOS_File_Logging.rsc
2019-07-19 15:17:01 +02:00
> RouterOS function adding log entry if file was added or removed.
2019-12-01 11:28:00 +01:00
**Example**
2019-07-19 15:17:01 +02:00
```
$FileToLog;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_LCD_Change.rsc
2019-07-19 15:17:01 +02:00
> RouterOS function changing LCD mode from dark to light and vice versa based on schedule setup.
2019-12-01 11:28:00 +01:00
**Example**
2019-07-19 15:17:01 +02:00
```
$ChangeLcd lightModeStartTime="08:00:00" darkModeStartTime="17:00:00";
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Low_Disk_Space.rsc
2019-07-29 16:11:19 +02:00
> RouterOS function generating mail alert if disk space is below defined treshhold.
2019-12-01 11:28:00 +01:00
**Example**
2019-07-19 15:17:01 +02:00
```
$LowDiskSpace treshhold=free_disk_space_in_%;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Mail_Backup.rsc
2019-12-01 11:28:00 +01:00
> Creates RouterOS backup and config file and sends them via email
2019-07-19 15:17:01 +02:00
2019-12-01 11:28:00 +01:00
**Example**
```
$MailBackup configName=configName backupName=backupName smtpServer=smtpServer smtpPort=smtpPort domain=@example.com \
recipient=recipient@example.com;
```
2019-07-19 15:17:01 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_File_Screening.rsc
2019-12-01 11:28:00 +01:00
> Removes files by specific name or file extansion
2019-07-19 15:17:01 +02:00
2019-12-01 11:28:00 +01:00
**Example**
```
$FileScreening keyWord=".txt";
```
2019-07-19 15:17:01 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_Format_Drive.rsc
2019-12-01 11:28:00 +01:00
> Formats RouterOS additional drive
2019-07-19 15:17:01 +02:00
2019-12-01 11:28:00 +01:00
**Example**
```
$FormatDrive drive=0 fileSystem=ext3 label=data;
```
2019-07-19 15:17:01 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_Modulo.rsc
2019-07-29 16:11:19 +02:00
>Modulo function.
2019-12-01 11:28:00 +01:00
**Example**
2019-07-29 16:11:19 +02:00
```
$Modulo number=number_to_be_devided modulo=modulo_value;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Port_Knock.rsc
2019-12-04 19:29:51 +01:00
> Port Knock from RouterOS
**Example**
```
:global knockBase {"sourceIP1"="port23"; \
"sourceIP2"="port22" \
};
$PortKnock base=$knockBase destinationAddress=destinationIP;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_R_ARP.rsc
2019-12-01 11:28:00 +01:00
> ARP/RARP Function (shortange syntax)
**Example**
```
$R_ARP mode=mode;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Radius_Monitor.rsc
2020-02-02 11:45:44 +01:00
> Sends Radius statistics via email
**Example**
```
$RadiusMonitor smtpServer=smtpServer smtpPort=smtpPort domain="@example.com" recipient="recipient@example.com";
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Renew_License.rsc
2019-12-01 11:28:00 +01:00
> Renews RouterOS License
**Example**
```
$RenewLicense account=account password=password level=level;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Load_Script.rsc
2019-12-01 11:28:00 +01:00
> Loads function to RouterOS environment
2019-07-29 16:11:19 +02:00
2019-12-01 11:28:00 +01:00
**Example**
2019-07-29 16:11:19 +02:00
```
$LoadScript scriptName=scriptName.rsc;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_String_Generator.rsc
2019-08-03 10:48:07 +02:00
> Generates 6 char string based on dictionary
2019-12-01 11:28:00 +01:00
**Example**
2019-08-03 10:48:07 +02:00
```
$GenerateString;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Reset_Interface.rsc
2019-08-03 10:48:07 +02:00
> Resets interface if particular IP does not respond
2019-12-01 11:28:00 +01:00
**Example**
2019-08-03 10:48:07 +02:00
```
$ResetInterface ipAddress=ipAddress_To_Monitor interfaceName=interface_name;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Script_Fetch.rsc
2019-08-21 14:25:53 +02:00
> Fetches script from git to RouterOS directory
2019-12-01 11:28:00 +01:00
**Example**
2019-08-21 14:25:53 +02:00
```
$FetchScript url="https://example.repo.com" destinationPath="FolderName" destinationFileName="ScriptName"
$FetchScript url="https://example.repo.com" destinationPath="FolderName" destinationFileName="ScriptName" isRun=true
$FetchScript url="https://example.repo.com" destinationPath="FolderName" destinationFileName="ScriptName" isSchedule=true\
interval="24" taskName="TaskName" startTime="startup"
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Substring_Variable.rsc
2019-12-01 11:28:00 +01:00
> Substrings variable
2019-06-15 00:17:35 +02:00
2019-12-01 11:28:00 +01:00
**Example**
```
$Substring findMode=true string="string" signA="A" signB "b";
```
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
#### RouterOS_User_alert.rsc
2020-02-02 11:45:44 +01:00
> After defined number of logon failure attempts in 1 hour sends email alert and blocks source IP on firewall
**Example**
```
$UserAlert message="message" treshhold=3 fileName="fileName" smtpServer=smtpServer smtpPort=smtpPort domain="@example.com" \
recipient="recipient@example.com";
```
2020-04-19 18:08:17 +02:00
#### RouterOS_FTP_Backup.rsc
2020-03-15 19:41:05 +01:00
> Creates backup files and sends them via FTP to FTP server
**Example**
```
$FTPBackup configName=configName backupName=backupName smtpServer=smtpServer smtpPort=smtpPort domain=@example.com \
recipient=recipient@example.com destPath=destPath ftpUser=user ftpPassword=password ftpServer=ftpserver;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Function_Template.rsc
2020-03-15 19:41:05 +01:00
> Function template that I use
2020-04-19 18:08:17 +02:00
#### RouterOS_PPP_Disconnect_Active_Sessions.rsc
2020-03-15 19:41:05 +01:00
> Disconnects all PPP active sessions at once
**Example**
```
$DisconnectPPPActveSessions;
```
2020-04-19 18:08:17 +02:00
#### RouterOS_Recreate_Bridge.rsc
2020-03-15 19:41:05 +01:00
> Recreates bridge if broken or misconfigured
2020-04-19 18:08:17 +02:00
#### More scripts comming soon :)
2019-06-15 00:17:35 +02:00
2020-04-19 18:08:17 +02:00
### Authors
2019-06-15 00:17:35 +02:00
2019-12-01 11:28:00 +01:00
- Grzegorz Budny