Community discussions

MikroTik App
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

help getting id in python api

Thu Aug 06, 2020 10:25 am

Hello ,
I have start working with Python API
using routeros_api
I want to change a nar rule according to it's name
I manage to do this
connection = routeros_api.RouterOsApiPool('10.0.0.138', username='admin', password='password',plaintext_login=True)
api = connection.get_api()
NatRule =  api.get_resource('/ip/firewall/nat')
print(NatRule.get(comment=RDP'))
this is the resualt:
[{'id': '*12', 'chain': 'dstnat', 'action': 'dst-nat', 'to-addresses': '10.0.0.100', 'to-ports': '3389', 'protocol': 'tcp', 'dst-address': 192.168.5.20', 'in-interface': 'all-ppp', 'dst-port': '3389', 'log': 'false', 'log-prefix': '', 'bytes': '44', 'packets': '1', 'invalid': 'false', 'dynamic': 'false', 'disabled': 'true', 'comment': 'RDP'}]
in order to change make enable\disable I need to know the id (in this case it's '*12')
NatRule.set(id='*12',disabled='false')
how can I save only this data ?
in another router it would be another id

is there a faster way to get this data using the API?

Thanks ,
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: help getting id in python api

Thu Aug 06, 2020 10:28 am

NatRule.get(comment=RDP')[0]["id"]
 
sid5632
Long time Member
Long time Member
Posts: 554
Joined: Fri Feb 17, 2017 6:05 pm

Re: help getting id in python api

Thu Aug 06, 2020 10:46 am

NatRule.get(comment='RDP')[0]["id"]
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1424
Joined: Sun Sep 18, 2011 7:00 pm

Re: help getting id in python api

Sat Aug 08, 2020 6:36 pm

it's working !

Thank you both!

Who is online

Users browsing this forum: No registered users and 35 guests