如何使用 Python RouterOS-api 禁用所有 DHCP 服务器
使用以下 Python 代码通过 Python RouterOS-api 包遍历所有 DHCP 服务器并禁用它们:
disable_all_dhcp.py
# 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')完整示例
以下示例适用于工厂配置的 MikroTik wAP-LTE 套件。
disable_all_dhcp_full_example.py
#!/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')示例输出
disable_all_dhcp_output.txt
Disabling DHCP server defconf on interface bridgeCheck out similar posts by category:
MikroTik, Networking
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow