-
Notifications
You must be signed in to change notification settings - Fork 0
/
j2monitor.py
77 lines (63 loc) · 2.37 KB
/
j2monitor.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
j2monitor.py
Jenifer Liddle <[email protected]>
This is a bit Heath-Robinson, but the way this works is:
jtwo.org sends an email to jsquared.co.uk
This code then ckecks syslog for the email to extract the IP address.
It then checks it against a stored copy of the IP address to see
if it has changed.
If it has, it sends an email and uses the Mythic Beasts API to update
the DNS entries for jtwo.org and hydranet.co.uk
'''
import smtplib
import sys
import re
import subprocess
import urllib
import urllib2
''' Send email to [email protected] with new IP address'''
def sendMail(newip):
s = smtplib.SMTP('localhost')
msg = "Subject: New IP address for jtwo\nIP address for jtwo is: "+newip
s.sendmail("[email protected]",["[email protected]"],msg)
''' Update single DNS record '''
def updateDNS(values):
url = 'https://dnsapi.mythic-beasts.com/'
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
''' Update DNS records for jtwo.org and hydranet.co.uk
using the Mythic Beasts API
'''
def updateAllDNS(newip, password):
values = {'domain' : 'jtwo.org', 'password' : password, 'command' : 'REPLACE @ 86400 A '+newip }
updateDNS(values)
values = {'domain' : 'jtwo.org', 'password' : password, 'command' : 'REPLACE www 86400 A '+newip }
updateDNS(values)
values = {'domain' : 'hydranet.co.uk', 'password' : password, 'command' : 'REPLACE @ 86400 A '+newip }
updateDNS(values)
values = {'domain' : 'hydranet.co.uk', 'password' : password, 'command' : 'REPLACE www 86400 A '+newip }
updateDNS(values)
values = {'domain' : 'hydranet.co.uk', 'password' : password, 'command' : 'REPLACE dev 86400 A '+newip }
updateDNS(values)
'''
Parse syslog to extract the IP address of jtwo.org, and see if it has changed
'''
password = sys.argv[1]
verbose = (len(sys.argv) > 2 and sys.argv[2] == '-v')
txt = subprocess.check_output(["tail","-800","/var/log/syslog"])
m=re.search('\n.*root@jtwo.*\n(.*)\n',txt)
if (m and len(m.groups()) > 0):
x = m.group(0)
if verbose: print "Found : \n", x
m=re.search("client_address=(\d+\.\d+\.\d+\.\d+)",x)
if (m and len(m.groups()) > 0):
newip = m.group(1)
if verbose: print "Found newip:", newip
oldip = subprocess.check_output(["cat","/etc/jtwo_ip"])
if (oldip != newip):
sendMail(newip)
updateAllDNS(newip,password)
f = open("/etc/jtwo_ip","w")
f.write(newip)
f.close()