-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_dns_afraid.py
executable file
·46 lines (38 loc) · 1.1 KB
/
update_dns_afraid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
import requests as r
import re
import datetime
import os
url = 'https://www.whatismyip.org/my-ip-address'
root_path = '/tmp/myip'
ip_path = '/tmp/myip/current_ip'
log_path = '/tmp/myip/log'
saved_ip = ''
#create directory
if not os.path.exists(root_path):
os.makedirs(root_path)
try:
os.environ['UPDATE_DNS']
except KeyError:
print('You need to set UPDATE_DNS before running this script')
exit()
try:
req = r.get(url)
ip = re.search(r'\d+\.\d+\.\d+\.\d+', req.text).group()
if os.path.isfile(log_path):
log_file = open(log_path, 'a')
else:
log_file = open(log_path, 'w')
if os.path.isfile(ip_path):
ip_file = open(ip_path, 'r')
saved_ip = ip_file.read()
ip_file.close()
if ip != saved_ip:
ip_file = open(ip_path, 'w')
ip_file.write(ip)
log_file.write('{} ip changed from {} to {} \n'.format(datetime.datetime.now(), saved_ip, ip))
else:
log_file.write('{} ip didnt change \n'.format(datetime.datetime.now()))
except:
with open(log_path) as log_file:
log_file.write('{} couldnt reach {} \n'.format(datetime.datetime.now(), url))