-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcheck.py
71 lines (68 loc) · 1.12 KB
/
ipcheck.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
#! /usr/bin/env python
import os
import sys
import subprocess
import smtplib
from email.mime.text import MIMEText
#
#IP File
#
FILE = ''
#
#SMTP Options
#
EMAIL = ''
SMTPHOST = ''
SENDER = ''
#
#SCP Options
#
#RSERVER = ''
#RPATH = ''
#RUSER = ''
#SCPCOM = 'scp '+FILE+' '+RUSER+'@'+RSERVER+':'+RPATH
#test output
#print "Your current IP is: "
try:
ipfile = open(FILE, 'r+')
except:
#print "file not found"
exit(1)
currentip = ipfile.readline()
#print (currentip)
#
#use a subprocess to query your
#current IP and place it into
#a string
#
proc = subprocess.Popen(["curl http://ifconfig.me/ip"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
#test output
#print str(out)
#
#Check if IP has changed
#
if currentip != out:
#print ("your ip has changed")
ipfile.seek(0)
ipfile.write(out)
#
#send email
#
msg = MIMEText(out)
msg['Subject'] = 'Your IP has changed'
msg['From'] = 'Host'
msg['To'] = EMAIL
s = smtplib.SMTP(SMTPHOST)
s.sendmail(SENDER, [EMAIL], msg.as_string())
s.quit()
#
#Send file with SCP
#
#os.system(SCPCOM)
#else:
#print ("Your ip has not changed")
#
#Close file
#
ipfile.close()