homework 4.2: add question 4

This commit is contained in:
2022-04-11 10:33:47 +07:00
parent 4ab000a022
commit 744437c4d6
3 changed files with 106 additions and 24 deletions

View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python3
import json
import io
import socket
filename = 'hosts.json'
hostsList = {
"drive.google.com",
"mail.google.com",
"google.com"
}
with open(filename, 'r+') as file:
jsonStr = file.read()
try:
jsonObj = json.load(io.StringIO(jsonStr))
except BaseException as err:
jsonObj = dict({})
print('error {}'.format(err))
exit(1)
file.truncate(0)
file.seek(0)
for hostname in hostsList:
ipAddr = socket.gethostbyname(hostname)
prevIpAddr = jsonObj.get(hostname)
if prevIpAddr is None or prevIpAddr == '':
prevIpAddr = ipAddr
print('{} - {}'.format(hostname, ipAddr))
if ipAddr != prevIpAddr:
print('[ERROR] {} IP mismatch: {} {}'.format(hostname, ipAddr, prevIpAddr))
jsonObj[hostname] = ipAddr
file.write(json.dumps(jsonObj))