Script MikroTik RouterOS (RouterBOARD o CHR)

()

Script di provisioning automatico per un router MikroTik RouterOS (versione 7+) tramite CLI o terminale remoto, pensato per ambienti enterprise con:

  • VLAN per utenti e server
  • DHCP per ogni VLAN
  • NAT per accesso Internet
  • VPN IPsec
  • Hardening sicurezza
  • Backup automatico

=== Impostazioni iniziali ===

/system identity set name=”Router-Core”
/interface ethernet set [find default-name=ether1] name=WAN
/interface ethernet set [find default-name=ether2] name=LAN

=== VLAN + indirizzi IP ===

/interface vlan add name=vlan10 vlan-id=10 interface=LAN comment=”Utenti”
/interface vlan add name=vlan20 vlan-id=20 interface=LAN comment=”Server”
/ip address add address=192.168.10.1/24 interface=vlan10
/ip address add address=192.168.20.1/24 interface=vlan20

=== DHCP Server per ogni VLAN ===

/ip pool add name=pool_utenti ranges=192.168.10.100-192.168.10.200
/ip pool add name=pool_server ranges=192.168.20.100-192.168.20.200
/ip dhcp-server add name=dhcp_utenti interface=vlan10 address-pool=pool_utenti disabled=no
/ip dhcp-server add name=dhcp_server interface=vlan20 address-pool=pool_server disabled=no
/ip dhcp-server network add address=192.168.10.0/24 gateway=192.168.10.1 dns-server=8.8.8.8
/ip dhcp-server network add address=192.168.20.0/24 gateway=192.168.20.1 dns-server=8.8.4.4

=== NAT per accesso Internet ===

/ip firewall nat add chain=srcnat out-interface=WAN action=masquerade

=== IPsec Site-to-Site VPN ===

/ip ipsec peer add address=198.51.100.1/32 exchange-mode=main secret=”StrongSecret123″ enc-algorithm=aes-256
/ip ipsec policy add src-address=192.168.10.0/24 dst-address=192.168.30.0/24 sa-dst-address=198.51.100.1 sa-src-address=0.0.0.0 tunnel=yes action=encrypt

=== Sicurezza (Hardening) ===

/ip service disable telnet,ftp,www
/ip service set ssh port=2222
/system logging add topics=info action=memory
/user set admin password=”Sup3rSicuro456″
/ip firewall filter add chain=input protocol=tcp dst-port=2222 src-address=192.168.10.0/24 action=accept
/ip firewall filter add chain=input in-interface=WAN action=drop

=== Backup giornaliero automatico ===

/system scheduler add name=”BackupGiornaliero” interval=1d on-event=”/system backup save name=backup_\$[/system clock get date]”

/system clock set time-zone-name=Europe/Rome


Come usarlo

  • Puoi incollarlo in Winbox, SSH o WebFig > Terminal.
  • Personalizza IP, password, segmenti VLAN e indirizzi peer VPN.
  • Verifica sempre la configurazione con comandi tipo /interface print o /ip address print.

script MikroTik RouterOS

per implementare un failover WAN automatico, ideale per CHR o RouterBOARD con due interfacce WAN (es. ether1 come primario e ether2 come secondario):

Esempio Script MikroTik per Failover WAN

Obiettivo: monitorare la connettività su ether1 e passare a ether2 se la connessione primaria cade, con ritorno automatico.

#Imposta gli indirizzi IP di destinazione da pingare per verificare la connessione (es. Google DNS)

:local primaryGateway 8.8.8.8
:local secondaryGateway 1.1.1.1

#Pinga il gateway della WAN primaria

:if ([/ping $primaryGateway count=3] = 0) do={

# Nessuna risposta: imposta la route tramite WAN secondaria
/ip route set [find comment="default-route"] gateway=192.168.2.1

# Log dell’evento
/log warning "WAN primaria non raggiungibile. Failover attivo su WAN secondaria."

} else={

# Se WAN primaria è ok, ripristina la route principale
/ip route set [find comment="default-route"] gateway=192.168.1.1

# Log dell’evento
/log info "WAN primaria OK. Routing ripristinato sulla connessione principale."

}


/ 5
Grazie per aver votato!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?