Community discussions

MikroTik App
 
crowfeatherjoe
just joined
Topic Author
Posts: 11
Joined: Mon Jan 07, 2019 8:09 pm
Location: California

Python API Parsing Errors?

Mon May 22, 2023 12:05 am

Using ROS 7.9 and Python 3.11.3 on Windows 11

Hi all, I am new to Python and the API. I am simply trying to get my script to output specific values from my Mikrotik by leveraging the API. Currently, I am able to list the interfaces of the routerboard and print the "link-down" values for each of them using HTTP GET. I went on to try to do the same for some values in /system/resources and was faced with what I imagine is probably a pretty basic error. For the life of me though, I can't seem to figure out how to make it output the data that I want. Again, I am not a Mikrotik or Python professional, but I would certainly appreciate someone fixing this code, or even just directing me to the proper resource with a decent layman's explanation of what I'm doing wrong.

Here is my code that correctly outputs the "Name" and "link-downs" from rest/interface/
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib3

urllib3.disable_warnings

#Variables for user input to be implemented later to access different Routerboards with REST API enabled:
'''
ipadress = input("Please enter the IP address of the router from which you want to list its bridge interfaces: ")
username = input("Username: ")
password = input("Password: ")
'''
#preconfigured router and credentials for quicker API testing
ipadress = "192.168.x.x"
username = 'api'
password = 'password'

#variable for shortening API request commands
url = 'http://' + ipadress + '/rest'

#GET request returning interface library
response = requests.get(url+'/interface', auth=HTTPBasicAuth(username,password), verify=False)

#print entire interface library, uncomment to see library output JSON
#print(json.dumps(response.json(), indent=4))

print()

#parsing GET request response to show only bridge interface ID and Name values
for interface in response.json():
    print(interface["name"] + " Link Downs: " + interface["link-downs"])
    
And here is the code where I try to leverage the same method in order to return rest/system ["uptime"]
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib3

urllib3.disable_warnings

#Variables for user input to be implemented later to access different Routerboards with REST API enabled:
#ipadress = input("Please enter the IP address of the router from which you want to list its bridge interfaces: ")
#username = input("Username: ")
#password = input("Password: ")

#preconfigured router and credentials for quicker API testing
ipadress = "192.168.x.x"
username = 'api'
password = 'password'

#variable for shortening API request commands
url = 'http://' + ipadress + '/rest'

#GET request returning interface library
#response = requests.get(url+'/interface', auth=HTTPBasicAuth(username,password), verify=False)

#print entire interface library, uncomment to see library output JSON
#print(json.dumps(response.json(), indent=4))

print()

#parsing GET request response to show only bridge interface ID and Name values
'''
for interface in response.json():
    if interface["type"]=="bridge":
        print("The ID of the bridge interface is: " + interface[".id"] + ". The NAME of the interface is: " + interface["name"])
        print()
'''

response = requests.get(url+'/system/resource', auth=HTTPBasicAuth(username,password), verify=False)
print(json.dumps(response.json(), indent=4))
uptime = response["uptime"]
print(uptime)
Please note: It is extremely difficult for me to ask this question anywhere else because I don't find a lot of people using Python and Mikrotik API together. There are some older repositories in GitHub that seem to make this problem a whole lot simpler, yet for some reason, they don't work. I tried to use LAiArturs-API for Mikrotik but it seems incompatible with ROS 7.9.
 
crowfeatherjoe
just joined
Topic Author
Posts: 11
Joined: Mon Jan 07, 2019 8:09 pm
Location: California

Re: Python API Parsing Errors?

Mon May 22, 2023 12:10 am

I just noticed I didnt provide the output for each code so here they are:

Code1:
ether1 Link Downs: 0
ether2 Link Downs: 2
wlan1 Link Downs: 142
wlan2 Link Downs: 0
wlan3 Link Downs: 0
SLO CORE VPN Link Downs: 0
Wired Devices Link Downs: 0
zerotier1 Link Downs: 0
Code 2:
{
    "architecture-name": "arm",
    "bad-blocks": "0",
    "board-name": "Audience",
    "build-time": "May/02/2023 05:35:06",
    "cpu": "ARM",
    "cpu-count": "4",
    "cpu-frequency": "672",
    "cpu-load": "5",
    "factory-software": "6.45.4",
    "free-hdd-space": "92151808",
    "free-memory": "172916736",
    "platform": "MikroTik",
    "total-hdd-space": "134479872",
    "total-memory": "268435456",
    "uptime": "1d19h44m12s",
    "version": "7.9 (stable)",
    "write-sect-since-reboot": "2515",
    "write-sect-total": "66954"
}
Traceback (most recent call last):
  File "c:\Users\Patrick\OneDrive - Surfnet Communications\Joe's Files\SURFNET\Joe's Scripts and Firmware\Open-Projects\Mikrotik\CAPsMAN\API\Network Automation using Python on Mikrotik (REST API part1).py", line 39, in <module>
    uptime = response["uptime"]
             ~~~~~~~~^^^^^^^^^^
TypeError: 'Response' object is not subscriptable
Another note: the second code is also just printing the total output of /system/resource as a placeholder to show what Objects I can actually find within it. The target attribute is the value for /resource "Uptime"
 
zainarbani
Frequent Visitor
Frequent Visitor
Posts: 54
Joined: Thu Jul 22, 2021 9:42 am
Location: Pati, Indonesia

Re: Python API Parsing Errors?

Wed May 24, 2023 7:43 am

import json
import requests

#Variables for user input to be implemented later to access different Routerboards with REST API enabled:
#ipadress = input("Please enter the IP address of the router from which you want to list its bridge interfaces: ")
#username = input("Username: ")
#password = input("Password: ")

#preconfigured router and credentials for quicker API testing
ipaddress = '192.168.x.x'
username = 'api'
password = 'password'

#variable for shortening API request commands
url = 'http://' + ipaddress + '/rest'

#Generate session
req = requests.Session()
req.auth = (username, password)

#JSON object response
res = req.get(url + '/interface').json()

#parsing GET request response to show only bridge interface ID and Name values
for i in res:
    print(i["name"] + " Link Downs: " + i["link-downs"])

print('\n')

#JSON object response
res = req.get(url + '/system/resource').json()

#Pretty-print JSON object (this is not a valid JSON object)
print(json.dumps(res, indent=4))

print('\n')

#Get 'uptime' from JSON object
uptime = res['uptime']
print(uptime)

Who is online

Users browsing this forum: No registered users and 13 guests