I have a JSON file that is placed in the /etc/ folder of a Linux system and contains the properties of a Router. Lets say the user wants to change the configuration of the Router and changes the IP of an interface. I am working on a python script that keeps polling the file for changes in the JSON file and extract the new data that has changed for further processing.
"device":
{
"device_image": "",
"password": "",
"interface": [
{
"interface_ip": "",
"interface_type": "",
"interface_name": "",
"interface_uid": ""
},
{
"interface_ip": "",
"interface_type": "",
"interface_name": "",
"interface_uid": ""
},
{
"interface_ip": "",
"interface_type": "",
"interface_name": "",
"interface_uid": ""
},
{
"interface_ip": "",
"interface_type": "",
"interface_name": "",
"interface_uid": ""
}
],
"username": "",
"device_id": "",
"ipaddress": "",
"gateway": "",
"device_name": "R1"
}
Is there a easy way to do this?
A loop opening the file and cheking for differences should work:
from time import sleep
from os import listdir
sleeptime = 5
if "old_json.json" in listdir("/path/"):
with open("/path/old_json.json/", "r") as f: old_json = f.read()
else:
with open("/path/json.json/", "r") as f: old_json = f.read()
while True:
with open("/path/json.json/", "r") as f: json = f.read()
if json != old_json:
print "json changed!"
old_json = json
with open("/path/old_json.json/", "w") as f: f.write(old_json)
sleep(sleeptime)
sleeptime is in seconds
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.