Wie man eine IP-Adresse zu einem MikroTik-Router mit Python & routeros_api hinzufügt

English Deutsch

Dies implementiert eine Hinzufügen-wenn-nicht-vorhanden-Funktion zum Hinzufügen einer IP-Adresse zu einem MikroTik-Router mit der routeros_api-Python-Bibliothek.

add_ip_address.py
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

Check out similar posts by category: RouterOS, MikroTik, Python