我已经在 pip 中安装了 routeros,但仍然显示错误。从我的前辈那里得到了这个代码。我正在尝试收集网络中的连接状态。
import routeros_api
import json
import datetime
import sys
#make a connection to MikorTik Router
connection = routeros_api.RouterOsApiPool('IP', username='name', password='mypassword', plaintext_login=True, port=8888)
api = connection.get_api()
#command to retreive data
list_queue = api.get_resource('/queue/tree')
QueueItems = list_queue.get() #get output from Mikrotik command
Uptime = api.get_resource('system/resource')
UptimeItems = Uptime.get() #get output form Mikoritk command
SystemClock = api.get_resource('/system/clock')
SystemClockItems = SystemClock.get()
#list System Clock items
for item in SystemClockItems:
time = item['time']
date = item['date']
#list uptime of MikorTik router
for item in UptimeItems:
range = item["uptime"] #stores uptime from mikroitk in 2d3h30m format
CurrentUpTime = range[0] # stores only the 1st digit of Mikortik uptime output whihc corresponds to DAYS
data = {} #create dictionary to store
data["time"] = [] #create list in the dictionary to store time key value pair
data["users"] = [] #create lsit in the dictionary to stor users key value pair
data2 = {} #create dictionary to store new data after mikoritk restarts
data2["time"] = [] #create list in the dictionary to store time key value pair
data2["users"] = [] #create lsit in the dictionary to stor users key value pair
#open output file to compare Uptime of Mirkotik
with open('output.txt','r') as file:
data = json.load(file)
for p in data["time"]:
PreviousUpTime = p['currentuptime']
#Take data from Queue and store it in file output.txt in json format
data = {} #create dictionary to store
data["system"] = [] #create list in the dictionary to store time key value pair
data["users"] = [] #create lsit in the dictionary to stor users key value pair
with open('output.txt','w') as f: # open file where to dump data
data['system'].append({"currentuptime" : CurrentUpTime}) #add current uptime of Mikrotik
data['system'].append({"time" : time})
data['system'].append({"date" : date})
for item in QueueItems: #loop through mikoritk ouptu for each user
if item["parent"] == "Sharing policy": #only show user downloads data under this tree
GigaBytes = int(item["bytes"]) / 1073741824 #convert data to Gigbaytes
temp_dict = { "name" : item["name"], "Gigabytes" : GigaBytes}
data['users'].append(temp_dict)
json.dump(data,f,indent=6) #dump data into file opened above in json format
我得到的输出是
import routeros_api
ModuleNotFoundError:没有名为“routeros_api”的模块