7.1 KiB
7.1 KiB
Script to change the IP
Set the first 3 octets of the new ips
:global ipPrefix "10.1.30"
To run the change of the IPs:
/system script run ip-config-changer
Script:
# Define the global variable ipPrefix if it doesn't exist
:global ipPrefix;
# Check if ipPrefix is set and not empty
:if ([:typeof $ipPrefix] != "str" || [:len $ipPrefix] = 0) do={
:put "Please set the prefix first using: :global ipPrefix \"192.168.2\"";
:error "ipPrefix is not defined or is empty";
}
# Display the current ipPrefix for debugging
:put ("Current ipPrefix: " . $ipPrefix);
# Update the IP pools with valid ranges
/ip pool set [find name="ethernet-pool"] ranges=($ipPrefix . ".131-" . $ipPrefix . ".131")
/ip pool set [find name="wifi"] ranges=($ipPrefix . ".200-" . $ipPrefix . ".220")
# Display the assigned values for debugging
:put ("Ethernet Pool Ranges: " . ($ipPrefix . ".131-" . $ipPrefix . ".131"))
:put ("WiFi Pool Ranges: " . ($ipPrefix . ".200-" . $ipPrefix . ".220"))
# Update the IP address of the ether2 interface
/ip address set [find where interface="ether2"] address=($ipPrefix . ".252/24") network=($ipPrefix . ".0")
# Display the assigned IP address for debugging
:put ("Ether2 IP Address: " . ($ipPrefix . ".252/24"))
# Update the DHCP server network settings
/ip dhcp-server network set [find] address=($ipPrefix . ".0/24") gateway=($ipPrefix . ".252") dns-server=($ipPrefix . ".252")
# Display the DHCP configuration for debugging
:put ("DHCP Network Address: " . ($ipPrefix . ".0/24"))
:put ("DHCP Gateway: " . ($ipPrefix . ".252"))
:put ("DHCP DNS Server: " . ($ipPrefix . ".252"))
# Update the local address in the PPP profile
/ppp profile set [find name="pptp-bridge"] local-address=($ipPrefix . ".252")
# Display the local address of the PPP profile for debugging
:put ("PPP Local Address: " . ($ipPrefix . ".252"))
# Restart the Wi-Fi interface
/interface wireless disable [find name="wlan1"]
:delay 2
/interface wireless enable [find name="wlan1"]
# Display a message indicating the Wi-Fi interface has been restarted
:put "Wi-Fi interface 'wlan1' has been restarted."
# Clear existing DHCP leases
/ip dhcp-server lease remove [find]
# Display a message indicating DHCP leases have been cleared
:put "All DHCP leases have been cleared."
# Display the current configuration
:put "=== Current Configuration ==="
:put ("Ethernet Pool: " . [/ip pool get [find name="ethernet-pool"] ranges])
:put ("WiFi Pool: " . [/ip pool get [find name="wifi"] ranges])
:put ("Ether2 IP: " . [/ip address get [find where interface="ether2"] address])
:put ("DHCP Network: " . [/ip dhcp-server network get [find] address])
:put ("PPP Local Address: " . [/ppp profile get [find name="pptp-bridge"] local-address])
###############################################################################
# Script: ether5-subnet-changer.rsc
# Purpose: Change the subnet (IP/route/NAT) associated with bridge_ether5
# Usage:
# 1) Defina la variable global newPrefix (tipo ip-prefix, sin comillas) ANTES de importar el script:
# :global newPrefix 192.168.254.0/24
# 2) /import file=ether5-subnet-changer.rsc
###############################################################################
# ---------------------------------------------------------------------------
# 1. Define and validate the variable │
# ---------------------------------------------------------------------------
:global newPrefix; # Debe existir. Ej: 192.168.254.0/24
:if ([:typeof $newPrefix] != "ip-prefix") do={
:put "Defina la variable como tipo ip-prefix, por ejemplo: :global newPrefix 192.168.xxx.0/24";
:error "newPrefix no es de tipo ip";
}
# ---------------------------------------------------------------------------
# 2. Calculate derived values │
# ---------------------------------------------------------------------------
:local newSubnet $newPrefix; # ip-prefix de la nueva red
:put ("newSubnet: " . $newSubnet);
# Extraer la parte de red sin máscara y convertirla a ip
:local netStr [:pick $newPrefix 0 [:find $newPrefix "/"]];
:put ("netStr: " . $netStr);
:local netIP [:toip $netStr];
:put ("netIP: " . $netIP);
:local gatewayIP ($netIP + 1); # Primer host
:put ("gatewayIP: " . $gatewayIP);
:local newIP ([:tostr $gatewayIP] . "/24"); # IP del router en ether5
:put ("newIP: " . $newIP);
:local oldNet [/ip address get [find interface=bridge_ether5] network];
:put ("oldNet: " . $oldNet);
:local oldSubnet ($oldNet . "/24");
:put ("oldSubnet: " . $oldSubnet);
:put ("Subnet change: " . $oldSubnet . " ➝ " . $newSubnet);
# ---------------------------------------------------------------------------
# 3. IP Address on bridge_ether5 │
# ---------------------------------------------------------------------------
/ip address
:foreach id in=[find interface=bridge_ether5] do={ :do { remove $id } on-error={} }
add address=$newIP interface=bridge_ether5 comment="bridge_ether5 gateway"
:put ("New IP applied: " . $newIP);
# ---------------------------------------------------------------------------
# 4. Routes │
# ---------------------------------------------------------------------------
/ip route
:foreach id in=[find dst-address=$oldSubnet] do={ :do { remove $id } on-error={} }
add dst-address=$newSubnet gateway=bridge_ether5 comment="bridge_ether5 route"
:put ("New route applied: " . $newSubnet . " via bridge_ether5");
# ---------------------------------------------------------------------------
# 5. NAT Rules │
# ---------------------------------------------------------------------------
/ip firewall nat
:foreach id in=[find src-address=$oldSubnet] do={ :do { remove $id } on-error={} }
:foreach id in=[find dst-address=$oldSubnet] do={ :do { remove $id } on-error={} }
# 5.a Masquerade outgoing traffic from the new subnet
add chain=srcnat action=masquerade src-address=$newSubnet \
comment="masq bridge_ether5 outgoing"
# 5.b (Optional) Masquerade from PLC (192.168.193.0/24) to the new subnet
add chain=srcnat action=masquerade src-address=192.168.193.0/24 \
dst-address=$newSubnet comment="masq PLC→bridge_ether5"
:put ("NAT rules updated for " . $newSubnet);
# ---------------------------------------------------------------------------
# 6. Summary │
# ---------------------------------------------------------------------------
:put "================ SUMMARY ================";
:put ("New IP : " . $newIP);
:put ("New Subnet : " . $newSubnet);
:put ("Route : " . $newSubnet . " → bridge_ether5");
:put "NAT rules created: masquerade (src) and (opt) PLC→subnet";
:put "========================================";
:log info ("bridge_ether5 changed to " . $newSubnet);
###############################################################################