librouteros: добавить правило брандмауэра в определенную позицию
Как добавить и переместить правило брандмауэра в определенную позицию в Mikrotik с помощью Python и библиотеки librouteros? Пример кода:
from librouteros import connect
from librouteros.query import Key
# Connect to the MikroTik router
api = connect(
username='Admin',
password='Password',
host='192.168.88.1',
port=8728
)
def move_firewall_rule(api, rule_id, new_position):
# Fetch all firewall rules mikrotik
firewall_rules = api.path('ip', 'firewall', 'filter')
# Find the rule to be moved
rule_to_move = None
for rule in firewall_rules:
print(rule)
if rule['.id'] == rule_id:
rule_to_move = rule
# print(rule_to_move)
break
# sys.exit(1)
if rule_to_move is None:
raise ValueError("Rule ID not found")
# Remove the rule to be moved
firewall_rules.remove(rule_id)
# Re-insert the rule at the desired position
rules_list = list(firewall_rules)
rules_list.insert(new_position, rule_to_move)
# Clear all current rules
for rule in firewall_rules:
firewall_rules.remove(rule['.id'])
# Re-add all rules in the new order
for rule in rules_list:
firewall_rules.add(**rule)
# Example usage:
move_firewall_rule(api, '*2D', 2) # Move rule with ID '*2D' to position 2
# Close the connection
api.close()
Программа выдает ошибку:
File "d:\Python\VCS_MSSQL\Mikrotik.py", line 395, in move_firewall_rule firewall_rules.add(**rule)
librouteros.exceptions.TrapError: unknown parameter