from librouteros import connect, exceptions
# Establish the connection to the Mikrotik router
try:
api = connect(host='****, username='****', password='****')
# Retrieve all firewall rules
print("Retrieving firewall rules...")
rules = list(api('/ip/firewall/filter/print'))
# Print all rules for debugging
print("Firewall rules:")
for r in rules:
print(r)
# Find the firewall rule with the specified comment
rule_id = None
for r in rules:
if r.get('comment') == 'IP Services HTTP':
rule_id = r['.id']
break # Assuming there's only one rule with that comment
if rule_id:
print(f"Found rule with ID {rule_id}. Enabling it...")
# Enable the rule using its ID
api('/ip/firewall/filter/enable', numbers=rule_id)
print("Firewall rule enabled successfully.")
else:
print("No firewall rule found with the comment 'IP Services HTTP'.")
except exceptions.TrapError as e:
print(f"Failed to enable firewall rule: {e}")
finally:
# Close the connection
api.close()
I can connect to the router through API, but the python script is not enabling the rule.
What am I doing wrong?