Redirect dynamic dhcp leases

Z Wiki UnArt Slavičín
Skočit na navigaciSkočit na vyhledávání

Skript prochází tabulku dhcp výpujček a nové (dynamické) výpujčky přesměruje na cílovou ip a port.

Otevřeme si ve winboxu Nový terminál (nebo se na mikrotik připojíme přes SSH) a pridáme pravidlo


# Script for redirect dynamic dhcp leases
# author: SevcikRoman
# for MikroTik 2.9.X

:set time-of-repeat 1h;
:set destination-ip "212.111.30.118";
:set destination-port "143";

:while true do {
#Erase dynamic address list
:foreach i in=[/ip firewall address-list find list="dynamic"] do=[ /ip firewall address-list remove $i ];

#Remove firewall rule for redirection to target ip and port.
:foreach i in=[/ip firewall nat find comment="Redirect dynamic ip addresses"] do=[ /ip firewall nat remove $i ];

#Fill dynamic address list by addresses of dynamic leases.
:foreach i in=[/ip dhcp-server lease find dynamic=yes]\ 
 do={
 :log info ("Redirect dynamic ip lease for ip: " . [/ip dhcp-server lease get $i address]);
 /ip firewall address-list add list=dynamic comment="Dynamic ip leases list" address=[/ip dhcp-server lease get $i address];
 }

#Create firewall rule for redirection to target ip and port.
/ip firewall nat add chain=dstnat protocol=tcp dst-port=80 src-address-list=dynamic action=dst-nat to-addresses=$destination-ip \
to-ports=$destination-port comment="Redirect dynamic ip addresses"; 

:delay $time-of-repeat;
}


Nastavíme spuštění skriptu po startu.

/system scheduler add name="start_redirect_script" on-event=dynamic_leases_redirection start-time=startup interval=0s \
   comment="Start script for redirecting dynamic dhcp leases" disabled=no