2015-10-27 12:25:17 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# --------------------------------------------------------------
|
|
|
|
# ip2e (IP to email) - Run ip2e daemon. |
|
|
|
|
# Created by clamsawd (clamsawd@openmailbox.org) |
|
|
|
|
# Licensed by GPL v.3 |
|
2015-10-31 19:14:48 +01:00
|
|
|
# Last update: 31-10-2015 |
|
2015-10-27 12:25:17 +01:00
|
|
|
# |
|
|
|
|
# Compatible with Python 3.x |
|
|
|
|
# --------------------------------------------------------------
|
2015-10-30 17:42:25 +01:00
|
|
|
version="1.0"
|
2015-10-27 12:25:17 +01:00
|
|
|
|
|
|
|
#Import python-modules
|
2015-10-30 17:35:01 +01:00
|
|
|
import urllib
|
|
|
|
import urllib.request
|
2015-10-27 12:25:17 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2015-10-29 11:31:16 +01:00
|
|
|
import time
|
2015-10-29 21:21:16 +01:00
|
|
|
import smtplib
|
2015-10-27 12:25:17 +01:00
|
|
|
|
|
|
|
#Check if your system use Python 3.x
|
|
|
|
if sys.version_info<(3,0):
|
|
|
|
print ("")
|
|
|
|
print ("You need python 3.x to run this program.")
|
|
|
|
print ("")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
#Function to clear screen
|
|
|
|
def ClearScreen():
|
2015-10-31 19:14:48 +01:00
|
|
|
if sys.platform == "cygwin":
|
|
|
|
print (300 * "\n")
|
|
|
|
elif os.name == "posix":
|
2015-10-27 12:25:17 +01:00
|
|
|
os.system("clear")
|
|
|
|
elif os.name == "nt":
|
|
|
|
os.system("cls")
|
|
|
|
else:
|
|
|
|
print ("Error: Unable clear screen")
|
2015-10-29 16:37:32 +01:00
|
|
|
|
2015-10-27 12:25:17 +01:00
|
|
|
#Detect system & PATH of user folder
|
|
|
|
if os.name == "posix":
|
|
|
|
os.chdir(os.environ["HOME"])
|
|
|
|
print ("POSIX detected")
|
|
|
|
elif os.name == "nt":
|
|
|
|
os.chdir(os.environ["USERPROFILE"])
|
|
|
|
print ("Windows detected")
|
|
|
|
|
|
|
|
if not os.path.exists(".ip2e"):
|
|
|
|
os.makedirs(".ip2e")
|
|
|
|
os.chdir(".ip2e")
|
|
|
|
if os.path.exists(".ip2e"):
|
|
|
|
os.chdir(".ip2e")
|
|
|
|
|
|
|
|
#Check if exists 'ip2e-conf.py'
|
|
|
|
def createip2ecf():
|
|
|
|
ip2ecf=open('ip2e-conf.py','w')
|
|
|
|
ip2ecf.close()
|
|
|
|
def writeip2ecf():
|
|
|
|
ip2ecf=open('ip2e-conf.py','a')
|
|
|
|
ip2ecf.write('# sample configuration file of ip2e\n')
|
|
|
|
ip2ecf.write('\n')
|
|
|
|
ip2ecf.write('FromEmail="ip2e@daemon"\n')
|
|
|
|
ip2ecf.write('FromEmailUser="unknown"\n')
|
|
|
|
ip2ecf.write('FromEmailPass="password-here"\n')
|
|
|
|
ip2ecf.write('SmtpFromEmail="smtp.email.com"\n')
|
|
|
|
ip2ecf.write('ToEmail="unknown@email.com"\n')
|
|
|
|
ip2ecf.close()
|
|
|
|
|
|
|
|
if os.path.isfile("ip2e-conf.py"):
|
|
|
|
print ("ip2e-conf.py exists")
|
|
|
|
else:
|
|
|
|
print ("ip2e-conf.py created")
|
|
|
|
createip2ecf()
|
|
|
|
writeip2ecf()
|
|
|
|
|
|
|
|
#Check if exists 'current-ip.py'
|
|
|
|
def createip2eIPcf():
|
|
|
|
ip2eIPcf=open('current-ip.py','w')
|
|
|
|
ip2eIPcf.close()
|
|
|
|
def writeip2eIPcf():
|
|
|
|
ip2eIPcf=open('current-ip.py','a')
|
|
|
|
ip2eIPcf.write('CurrentIP="0.0.0.0"\n')
|
|
|
|
ip2eIPcf.close()
|
|
|
|
|
|
|
|
if os.path.isfile("current-ip.py"):
|
|
|
|
print ("current-ip.py exists")
|
|
|
|
else:
|
|
|
|
print ("current-ip.py created")
|
|
|
|
createip2eIPcf()
|
|
|
|
writeip2eIPcf()
|
|
|
|
|
|
|
|
#Import variables from ip2e-conf.py
|
|
|
|
exec(open("ip2e-conf.py").read())
|
|
|
|
|
2015-10-30 08:45:47 +01:00
|
|
|
#Import native OS color scheme
|
2015-10-31 21:31:52 +01:00
|
|
|
try:
|
|
|
|
def GreenColor():
|
|
|
|
if os.name == "posix":
|
|
|
|
GreenColor = (chr(27)+"[1;32m")
|
|
|
|
print (GreenColor+"", end="")
|
|
|
|
elif os.name == "nt":
|
|
|
|
os.system("color 2")
|
|
|
|
def RedColor():
|
|
|
|
if os.name == "posix":
|
|
|
|
RedColor = (chr(27)+"[1;31m")
|
|
|
|
print (RedColor+"", end="")
|
|
|
|
elif os.name == "nt":
|
|
|
|
os.system("color 4")
|
|
|
|
def OrangeColor():
|
|
|
|
if os.name == "posix":
|
|
|
|
OrangeColor = (chr(27)+"[1;33m")
|
|
|
|
print (OrangeColor+"", end="")
|
|
|
|
elif os.name == "nt":
|
|
|
|
os.system("color 6")
|
|
|
|
except:
|
|
|
|
print ("Error importing native color scheme")
|
2015-10-29 16:31:45 +01:00
|
|
|
|
2015-10-29 18:47:09 +01:00
|
|
|
#Check if exists a previous log.file.
|
|
|
|
if os.path.isfile("ip2e.log"):
|
|
|
|
os.remove("ip2e.log")
|
|
|
|
createlog=open('ip2e.log','w')
|
|
|
|
createlog.close()
|
|
|
|
|
2015-10-27 12:25:17 +01:00
|
|
|
#Run ip2e daemon
|
|
|
|
ClearScreen()
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog=open('ip2e.log','a')
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
GreenColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
|
2015-10-27 12:25:17 +01:00
|
|
|
if os.name == "posix":
|
2015-10-30 08:45:47 +01:00
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["HOME"]+"/.ip2e/ip2e.log")
|
2015-10-29 16:31:45 +01:00
|
|
|
elif os.name == "nt":
|
2015-10-29 11:31:16 +01:00
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["USERPROFILE"]+"\.ip2e\\ip2e.log")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+"\n")
|
2015-10-30 08:45:47 +01:00
|
|
|
OrangeColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...\n")
|
|
|
|
editlog.close()
|
2015-10-29 16:37:32 +01:00
|
|
|
time.sleep(60)
|
2015-10-27 12:25:17 +01:00
|
|
|
|
|
|
|
PublicIP = 1
|
|
|
|
|
|
|
|
while PublicIP <= 2:
|
|
|
|
GetCurrentIP = 1
|
|
|
|
while GetCurrentIP <= 2:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
OrangeColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog=open('ip2e.log','a')
|
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP Updating...\n")
|
2015-10-30 17:31:42 +01:00
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen('http://icanhazip.com')
|
|
|
|
#response = urllib.request.urlopen('http://ip.appspot.com/')
|
|
|
|
#response = urllib.request.urlopen('http://ident.me')
|
|
|
|
NewIPRaw = response.read()
|
|
|
|
NewIP = NewIPRaw.strip().decode('utf-8')
|
2015-10-27 12:25:17 +01:00
|
|
|
GetCurrentIP += 2
|
2015-10-30 17:31:42 +01:00
|
|
|
except:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
RedColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
|
2015-10-30 09:33:52 +01:00
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Error getting IP\n")
|
2015-10-30 09:33:52 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...\n")
|
|
|
|
time.sleep(10)
|
2015-10-27 12:25:17 +01:00
|
|
|
exec(open("current-ip.py").read())
|
|
|
|
if CurrentIP == NewIP:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
GreenColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP has not changed\n")
|
2015-10-27 12:25:17 +01:00
|
|
|
else:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
GreenColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+"\n")
|
2015-10-27 20:21:14 +01:00
|
|
|
SendEmailOK = 1
|
|
|
|
while SendEmailOK <= 2:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-29 21:21:16 +01:00
|
|
|
#Sending email using smtplib
|
|
|
|
SmtpSubject = "[ip2e-daemon] ["+CurrentTime+"] IP has changed"
|
2015-10-29 21:26:12 +01:00
|
|
|
SmtpHeader = "From: "+FromEmail+"\n"+"To: "+ToEmail+"\n"+"Subject: "+SmtpSubject+"\n"
|
2015-10-29 21:21:16 +01:00
|
|
|
SmtpBodyMessage = SmtpHeader+"\n"+"[ip2e] New IP is "+NewIP+"\n\n"
|
2015-10-30 09:33:52 +01:00
|
|
|
#Check sending errors
|
2015-10-29 22:01:25 +01:00
|
|
|
try:
|
|
|
|
server = smtplib.SMTP(SmtpFromEmail)
|
|
|
|
server.ehlo()
|
|
|
|
server.starttls()
|
|
|
|
server.ehlo()
|
|
|
|
server.login(FromEmailUser,FromEmailPass)
|
2015-10-29 21:21:16 +01:00
|
|
|
server.sendmail(FromEmail, ToEmail, SmtpBodyMessage)
|
|
|
|
server.quit()
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
|
|
|
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Email was sent successfully"
|
2015-10-30 08:45:47 +01:00
|
|
|
GreenColor()
|
|
|
|
print (MailMessage+" ("+ToEmail+")")
|
2015-10-30 09:33:52 +01:00
|
|
|
editlog.write(MailMessage+" ("+ToEmail+")\n")
|
2015-10-27 20:21:14 +01:00
|
|
|
SendEmailOK += 2
|
2015-10-30 09:33:52 +01:00
|
|
|
except:
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
RedColor()
|
2015-10-30 09:33:52 +01:00
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")")
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Check your settings or your connection")
|
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")\n")
|
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Check your settings or your connection\n")
|
|
|
|
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Failed to send email"
|
2015-10-30 08:45:47 +01:00
|
|
|
print (MailMessage+" ("+ToEmail+")")
|
2015-10-30 09:33:52 +01:00
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...")
|
|
|
|
editlog.write(MailMessage+" ("+ToEmail+")\n")
|
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...\n")
|
|
|
|
time.sleep(10)
|
2015-10-29 16:56:05 +01:00
|
|
|
os.remove("current-ip.py")
|
2015-10-27 12:25:17 +01:00
|
|
|
def createNewip2eIPcf():
|
|
|
|
ip2eIPcf=open('current-ip.py','w')
|
|
|
|
ip2eIPcf.close()
|
|
|
|
def writeNewip2eIPcf():
|
|
|
|
ip2eIPcf=open('current-ip.py','a')
|
|
|
|
ip2eIPcf.write('CurrentIP="'+NewIP+'"\n')
|
|
|
|
ip2eIPcf.close()
|
|
|
|
createNewip2eIPcf()
|
|
|
|
writeNewip2eIPcf()
|
2015-10-29 11:31:16 +01:00
|
|
|
CurrentTime = time.strftime("%H:%M")
|
2015-10-30 08:45:47 +01:00
|
|
|
GreenColor()
|
|
|
|
print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
|
2015-10-29 18:47:09 +01:00
|
|
|
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...\n")
|
|
|
|
editlog.close()
|
2015-10-29 16:37:32 +01:00
|
|
|
time.sleep(600)
|