How to add IP address to MikroTik router using Python & routeros_api
This implements a add if not exists function for adding an IP address to a MikroTik router using the routeros_api
Python library.
def add_ip_address(api, network):
"""
Add an IP address to the bridge interface.
:param api: The RouterOS API object.
:param network: The network address to add (e.g., '10.1.2.3/24').
"""
# Add an IP address to the bridge interface
print(f"Adding IP address {network} to bridge interface")
try:
api.get_resource('/ip/address').add(address=network, interface='bridge')
except routeros_api.exceptions.RouterOsApiCommunicationError as e:
if "already have such address" in str(e):
print("IP address already exists")
else:
raise e