Tag Archives: mikrotik

Mikrotik Dyndns update IP address

:global ddnsuser “dyndnsuser”
:global ddnspass “dyndnspass”
:global ddnshost “mycam.dvrdns.org”
:global theinterface strongvpn-germany

:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info (“DynDNS: No ip address on $theinterface .”)
} else={
:for i from=( [:len $ipfresh] – 1) to=0 do={
:if ( [:pick $ipfresh $i] = “/”) do={
:set ipfresh [:pick $ipfresh 0 $i];
}
}

:if ($ipddns != $ipfresh) do={
:log info (“DynDNS: IP-DynDNS = $ipddns”)
:log info (“DynDNS: IP-Fresh = $ipfresh”)
:log info “DynDNS: Update IP needed, Sending UPDATE…!”
:global str “/nic/update\?hostname=$ddnshost&myip=$ipfresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG”
/tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \
password=$ddnspass dst-path=(“/DynDNS.”.$ddnshost)
:delay 1
:global str [/file find name=”DynDNS.$ddnshost”];
/file remove $str
:global ipddns $ipfresh
:log info “DynDNS: IP updated to $ipfresh!”
} else={
:log info “DynDNS: dont need changes”;
}
}

Advertisements

Mktik Failover Script

# ——————- header ——————-
# Script by Tomas Kirnak, version 1.0.7
# If you use this script, or edit and
# re-use it, please keep the header intact.
#
# For more information and details about
# this script please visit the wiki page at
# http://wiki.mikrotik.com/wiki/Failover_Scripting
# ——————- header ——————-

# ————- Modification ——————-
# Modified by Andrés López
# Failover using Firewall Mangle Rules
# ———————————————-

# Please fill the WAN interface names
:local canal_a ether1
:local canal_b ether2
:local canal_c ether3

# routing net marks
:local redadmin 0
:local redinvitados 1

# check gateways
:local PingTarget 8.8.8.8
:local FailTreshold 10

# Declare the global variables
:global PingFailCountISP1
:global PingFailCountISP2

# Status
:global canal_a_estado
:global canal_b_estado

# This inicializes the PingFailCount variables, in case this is the 1st time the script has ran
:if ([:typeof $PingFailCountISP1]=”nothing”) do={:set PingFailCountISP1 0}
:if ([:typeof $PingFailCountISP2]=”nothing”) do={:set PingFailCountISP2 0}

# This variable will be used to keep results of individual ping attempts
:local PingResult

# Check ISP1
:set PingResult [ping $PingTarget count=1 interface=$canal_a]
:put $PingResult

:if ($PingResult = 0) do={
:if ($PingFailCountISP1 < ($FailTreshold+2)) do={
:set PingFailCountISP1 ($PingFailCountISP1 + 1)

:if ($PingFailCountISP1 = $FailTreshold) do={
:log warning “Canal a is offline”
:set canal_a_estado 1
}
}
}

:if ($PingResult = 1) do={
:if ($PingFailCountISP1 > 0) do={
:set PingFailCountISP1 ($PingFailCountISP1-1)

:if ($PingFailCountISP1 = ($FailTreshold – 1)) do={
:log warning “Canal a is back”
:set canal_a_estado 0
}
}
}

# Check ISP2
:set PingResult [ping $PingTarget count=1 interface=$canal_b]
:put $PingResult

:if ($PingResult = 0) do={
:if ($PingFailCountISP2 < ($FailTreshold+2)) do={
:set PingFailCountISP2 ($PingFailCountISP2 + 1)

:if ($PingFailCountISP2 = $FailTreshold) do={
:log warning “Canal b is offline”
:set canal_b_estado 1
}
}
}

:if ($PingResult = 1) do={
:if ($PingFailCountISP2 > 0) do={
:set PingFailCountISP2 ($PingFailCountISP2-1)

:if ($PingFailCountISP2 = ($FailTreshold – 1)) do={
:log warning “Canal b is back”
:set canal_b_estado 0
}
}
}

# Indication flags
:put $canal_a_estado
:put $canal_b_estado

# Failover actions
:if ($canal_b_estado=1) do={
:log warning “Enviando redinvitados por canal c”
ip firewall mangle set new-routing-mark=canal_c $redinvitados
}

:if ($canal_a_estado=1) do={
:log warning “Enviando redadmin por canal c”
ip firewall mangle set new-routing-mark=canal_c $redadmin
}

:if ($canal_b_estado=0) do={
:log warning “Canal b estable”
ip firewall mangle set new-routing-mark=canal_b $redinvitados
}

:if ($canal_a_estado=0) do={
:log warning “Canal a estable”
ip firewall mangle set new-routing-mark=canal_a $redadmin
}

Mikrotik Dual Wan

# NAT -----------------
# Enmascarar cualquier tráfico saliente por las interfaces wan:
# ether1 = canal_primario
# ether2 = canal_secundario
# ether3 = canal_terciario


ip firewall nat add chain=src-nat out-interface=ether1 action masquerade
ip firewall nat add chain=src-nat out-interface=ether2 action masquerade
ip firewall nat add chain=src-nat out-interface=ether3 action masquerade

# MANGLE -------------

# Marco el tráfico entrante de la red lan 10.2.59.0/24 para utilizar el canal primario

ip firewall mangle add src-address=10.2.59.0/24 action=mark-routing new-routing-mark=canal_primario chain=prerouting

# Marco el tráfico entrante de la red lan 10.2.60.0/24 utilizar el canal secundario

ip firewall mangle add src-address=10.2.60.0/24 action=mark-routing new-routing-mark=canal_secundario chain=prerouting

# Marco el tráfico entrante de la red lan 10.2.61.0/24 para utilizar el canal terciario

ip firewall mangle add src-address=10.2.61.0/24 action=mark-routing new-routing-mark=canal_terciario chain=prerouting

# Marco el tráfico saliente del router Mktik hacia los proveedores de servicio (en este caso 3 wans)

ip firewall mangle add src-address=192.168.56.2 action=mark-routing new-routing-mark=canal_primario chain=output
ip firewall mangle add src-address=192.168.57.2 action=mark-routing new-routing-mark=canal_secundario chain=output
ip firewall mangle add src-address=192.168.58.2 action=mark-routing new-routing-mark=canal_terciario chain=output


# ROUTE ----------------
# Estas son las reglas de enrutamiento basadas en marcas

ip route add gateway=192.168.56.1 routing-mark=canal_primario
ip route add gateway=192.168.57.1 routing-mark=canal_secundario
ip route add gateway=192.168.58.1 routing-mark=canal_terciario
ip route rule add dst-address=0.0.0.0/0 routing-mark=canal_primario table=canal_primario action=lookup
ip route rule add dst-address=0.0.0.0/0 routing-mark=canal_secundario table=canal_secundario action=lookup
ip route rule add dst-address=0.0.0.0/0 routing-mark=canal_terciario table=canal_terciario action=lookup