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/
Code: Select all
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"])
Code: Select all
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)