Redirect dynamic dhcp leases

Z Wiki UnArt Slavičín
Verze z 4. 12. 2008, 14:52, kterou vytvořil Quimi (diskuse | příspěvky)
(rozdíl) ← Starší verze | zobrazit aktuální verzi (rozdíl) | Novější verze → (rozdíl)
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

Pro přidání skriptu můžene vložit příkaz do konsole. Zatím nevím jak do závorek vložit celý kód ,tak je potřeba jej přidat přes winbox.

/system script add name="dynamic_leases_redirection" source={}

Verze 1, pro MikroTik 2.9.X

Skript se spouští hned po startu Mikrotiku a dál je udržován nekonečným cyklem. Je nezávislý 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 "145";

: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, pro MikroTik 2.9.X

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

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

:set destination-ip "212.111.30.118";
:set destination-port "145";

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

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

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


Verze 3, pro MikroTik 3.X

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

# Script for redirect dynamic dhcp leases
# authors: Sevcik Roman, Kliment Michal
# for MikroTik 3.x

:global destinationip "212.111.30.118";
:global destinationport "145";
:global sourceip;

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

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

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

#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=$destinationip \
to-ports=$destinationport comment="Redirect dynamic ip addresses"; 


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

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