Obsidean_VM/01-Documentation/Routers/Mikrotik - Network Redes/Mikrotik Scripts.md

2.7 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])