Created
October 26, 2020 14:21
-
-
Save giammin/d174c0db2b39dcf2ba68f8b3df02f4f9 to your computer and use it in GitHub Desktop.
python script to log public ip changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from datetime import datetime | |
import os | |
import requests | |
LOG = 'ip.log' | |
URL = 'https://ipinfo.io/ip' | |
try: | |
print('{} getting ip from {}'.format(datetime.now(), URL)) | |
r = requests.get(URL, timeout=10) | |
print('{} response {}'.format(datetime.now(), r.status_code)) | |
if r.status_code == 200: | |
ip = r.content.decode('ascii').rstrip('\n') | |
last_ip = None | |
if os.path.exists(LOG): | |
f = open(LOG, 'r') | |
last_ip = f.readlines()[-1].split()[-1] | |
f.close() | |
if ip != last_ip: | |
f = open(LOG, 'a+') | |
f.write("{} {}\n".format(datetime.now(), ip)) | |
print('{} ip:{} lastip:{}'.format(datetime.now(), ip, last_ip)) | |
else: | |
msg="{} {} {}\n".format(datetime.now(), r.status_code, r.reason) | |
print(msg) | |
f = open(LOG, 'a+') | |
f.write(msg) | |
except Exception as inst: | |
msg="{} No connection {} {}\n".format(datetime.now(), type(inst), inst.args) | |
print(msg) | |
f = open(LOG, 'r') | |
if 'No connection' not in (f.readlines()[-1]): | |
f = open(LOG, 'a+') | |
f.write(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment