-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipupdate.py
59 lines (55 loc) · 1.75 KB
/
ipupdate.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
#!/usr/bin/env python
#
# ipupdate.py
# Check public IP, on change send out sms and email notification
# http://linux-101.org
#
import string
import httplib
import smtplib
from sms import Sms
from email.Utils import formatdate
# Set some variables
smtpServer = "smtp.gmail.com"
smtpUser = "[email protected]"
smtpPass = "00190LZLNGZKLISORHMQZECTTNVZLF"
fromAddress = "[email protected]"
toAddress = ("[email protected]")
mobileNumber = "4125822951"
logFile = "/var/log/ip-log.log"
try:
conn = httplib.HTTPConnection("ip.appspot.com")
conn.request("GET", "/")
r1 = conn.getresponse()
# print r1.status, r1.reason
# If IP is unchanged, close and exit.
if oldip.strip() == straddr:
logfile.close()
conn.close()
else:
# If new IP, first write to log, then send SMS, then send email
logfile = open(logFile,"a")
logfile.write(straddr + "\n")
logfile.close()
conn.close()
sms = TextMessage(recipient=mobileNumber, message="New IP address is: \r" + straddr)
sms.connectPhone()
sms.sendMessage()
sms.disconnectPhone()
s = smtplib.SMTP(smtpServer)
s.set_debuglevel(0)
s.ehlo()
s.starttls()
s.ehlo()
# Hash out below line if login is not needed
s.login(smtpUser, smtpPass)
emailDate = formatdate(localtime=True)
emailMsg = """From: Home PC <[email protected]>
Date: """ + emailDate + """
Subject: IP Address has changed.
New IP address is: """ + straddr + """
"""
s.sendmail(fromAddress, toAddress, emailMsg)
s.close()
except:
print "Exception raised! Something's not working."