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 skript v /system/script

  • Verze 1

Skript se spouští hned po startu Mikrotiku a dál je udržován nekonečným cyklem. Je nezávyslí na systémovém čase

# Script for redirect dynamic dhcp leases
# author: Sevcik Roman
# 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
  • Verze 2

Skript se spouští pomocí scheduleru každou hodinu.

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

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

#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"; 


/system scheduler 
  add name="start_redirect_script" on-event=dynamic_leases_redirection start-date=jan/01/1970 start-time=00:00:00 \
  interval=1hs comment="Start script for redirecting dynamic dhcp leases" disabled=no