-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhatsmyip.py
executable file
·43 lines (38 loc) · 1.07 KB
/
whatsmyip.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
#!/usr/bin/python
import httplib
import ConfigParser
import os
import boto.ses
def send_email(old, new):
conf = ConfigParser.ConfigParser()
conf.read(os.path.expanduser('~') + "/.currentip.config")
addr = conf.get("email","address")
conn = boto.ses.connect_to_region('us-east-1')
conn.send_email(
addr,
'[Updated IP Address]',
'old ip: %s, new ip: %s' % (old, new,),
[addr]
)
def stored_ip(update=None):
with open(os.path.expanduser('~') + "/.currentip.txt", "r+") as f:
if update:
f.seek(0)
f.write(update)
f.truncate()
else:
content = f.readlines()
return content[0].strip()
def main():
old_ip = stored_ip()
h1 = httplib.HTTPConnection('icanhazip.com')
h1.request("GET", "/")
resp = h1.getresponse()
if resp.status != 200:
return
current_ip = resp.read().strip()
if current_ip and old_ip != current_ip:
send_email(old_ip, current_ip)
stored_ip(update=current_ip)
if __name__ == "__main__":
main()