How to disable all DHCP servers using Python RouterOS-api
Use the following Python code to iterate over all DHCP servers using the Python RouterOS-api package and disable them:
# Access the DHCP server resource
dhcp_server_resource = api.get_resource('/ip/dhcp-server')
# Get all DHCP servers
dhcp_servers = dhcp_server_resource.get()
# Loop through all DHCP servers and disable each one
for dhcp_server in dhcp_servers:
print(f"Disabling DHCP server {dhcp_server['name']} on interface {dhcp_server['interface']}")
server_id = dhcp_server['id']
dhcp_server_resource.set(id=server_id, disabled='yes')
Full example
The following example works for me with a factory-configured MikroTik wAP-LTE kit.
#!/usr/bin/env python3
import routeros_api
connection = routeros_api.RouterOsApiPool(
'192.168.88.1', username='admin', password='L9TYAUV7R8',
plaintext_login=True
)
api = connection.get_api()
# Access the DHCP server resource
dhcp_server_resource = api.get_resource('/ip/dhcp-server')
# Get all DHCP servers
dhcp_servers = dhcp_server_resource.get()
# Loop through all DHCP servers and disable each one
for dhcp_server in dhcp_servers:
print(f"Disabling DHCP server {dhcp_server['name']} on interface {dhcp_server['interface']}")
server_id = dhcp_server['id']
dhcp_server_resource.set(id=server_id, disabled='yes')
Example output
Disabling DHCP server defconf on interface bridge