Import native OS color scheme

This commit is contained in:
clamsawd 2015-10-30 08:45:47 +01:00
parent db9959b61e
commit af9f3479ca
3 changed files with 43 additions and 65 deletions

View File

@ -5,7 +5,7 @@
# ip2e (IP to email) - Create the configuration file. | # ip2e (IP to email) - Create the configuration file. |
# Created by clamsawd (clamsawd@openmailbox.org) | # Created by clamsawd (clamsawd@openmailbox.org) |
# Licensed by GPL v.3 | # Licensed by GPL v.3 |
# Last update: 29-10-2015 | # Last update: 30-10-2015 |
# | # |
# Dependences: curl or wget | # Dependences: curl or wget |
# Compatible with Python 3.x | # Compatible with Python 3.x |

View File

@ -5,7 +5,7 @@
# ip2e (IP to email) - Run ip2e daemon. | # ip2e (IP to email) - Run ip2e daemon. |
# Created by clamsawd (clamsawd@openmailbox.org) | # Created by clamsawd (clamsawd@openmailbox.org) |
# Licensed by GPL v.3 | # Licensed by GPL v.3 |
# Last update: 29-10-2015 | # Last update: 30-10-2015 |
# | # |
# Dependences: curl or wget | # Dependences: curl or wget |
# Compatible with Python 3.x | # Compatible with Python 3.x |
@ -107,13 +107,25 @@ except:
#Import variables from ip2e-conf.py #Import variables from ip2e-conf.py
exec(open("ip2e-conf.py").read()) exec(open("ip2e-conf.py").read())
#Import Windows colors scheme #Import native OS color scheme
def GreenColor(): def GreenColor():
os.system("color 2") if os.name == "posix":
GreenColor = (chr(27)+"[1;32m")
print (GreenColor+"", end="")
elif os.name == "nt":
os.system("color 2")
def RedColor(): def RedColor():
os.system("color 4") if os.name == "posix":
RedColor = (chr(27)+"[1;31m")
print (RedColor+"", end="")
elif os.name == "nt":
os.system("color 4")
def OrangeColor(): def OrangeColor():
os.system("color 6") if os.name == "posix":
OrangeColor = (chr(27)+"[1;33m")
print (OrangeColor+"", end="")
elif os.name == "nt":
os.system("color 6")
#Check if exists a previous log.file. #Check if exists a previous log.file.
if os.path.isfile("ip2e.log"): if os.path.isfile("ip2e.log"):
@ -125,22 +137,15 @@ createlog.close()
ClearScreen() ClearScreen()
editlog=open('ip2e.log','a') editlog=open('ip2e.log','a')
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
if os.name == "posix": if os.name == "posix":
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)") print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["HOME"]+"/.ip2e/ip2e.log")
elif os.name == "nt": elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
if os.name == "posix":
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["HOME"]+"/.ip2e/ip2e.log")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["USERPROFILE"]+"\.ip2e\\ip2e.log") print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["USERPROFILE"]+"\.ip2e\\ip2e.log")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+"\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+"\n")
if os.name == "posix": OrangeColor()
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
elif os.name == "nt":
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...\n")
editlog.close() editlog.close()
time.sleep(60) time.sleep(60)
@ -151,11 +156,8 @@ while PublicIP <= 2:
GetCurrentIP = 1 GetCurrentIP = 1
while GetCurrentIP <= 2: while GetCurrentIP <= 2:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix": OrangeColor()
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] IP Updating...") print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
elif os.name == "nt":
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
editlog=open('ip2e.log','a') editlog=open('ip2e.log','a')
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP Updating...\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP Updating...\n")
NewIPRaw = os.popen('curl -s icanhazip.com').read() NewIPRaw = os.popen('curl -s icanhazip.com').read()
@ -168,32 +170,22 @@ while PublicIP <= 2:
GetCurrentIP += 2 GetCurrentIP += 2
else: else:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix": RedColor()
print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Error getting IP") print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
elif os.name == "nt":
RedColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Error getting IP\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Error getting IP\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...\n")
time.sleep(5) time.sleep(5)
exec(open("current-ip.py").read()) exec(open("current-ip.py").read())
if CurrentIP == NewIP: if CurrentIP == NewIP:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix": GreenColor()
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] IP has not changed") print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP has not changed\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP has not changed\n")
else: else:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix": GreenColor()
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP) print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
editlog.write("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+"\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+"\n")
SendEmailOK = 1 SendEmailOK = 1
while SendEmailOK <= 2: while SendEmailOK <= 2:
@ -209,13 +201,9 @@ while PublicIP <= 2:
server.ehlo() server.ehlo()
server.login(FromEmailUser,FromEmailPass) server.login(FromEmailUser,FromEmailPass)
except: except:
if os.name == "posix": RedColor()
print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")") print ("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")")
print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]") print ("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]")
elif os.name == "nt":
RedColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")")
print ("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]\n")
exit(1) exit(1)
@ -225,23 +213,16 @@ while PublicIP <= 2:
server.quit() server.quit()
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Email was sent successfully" MailMessage="[ip2e-daemon] ["+CurrentTime+"] Email was sent successfully"
if os.name == "posix": GreenColor()
print (chr(27)+"[1;32m"+MailMessage+" ("+ToEmail+")") print (MailMessage+" ("+ToEmail+")")
elif os.name == "nt":
GreenColor()
print (MailMessage+" ("+ToEmail+")")
editlog.write(MailMessage+" to "+ToEmail+"\n") editlog.write(MailMessage+" to "+ToEmail+"\n")
SendEmailOK += 2 SendEmailOK += 2
except SMTPException: except SMTPException:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Fail to send email" MailMessage="[ip2e-daemon] ["+CurrentTime+"] Fail to send email"
if os.name == "posix": RedColor()
print (chr(27)+"[1;31m"+MailMessage+" ("+ToEmail+")") print (MailMessage+" ("+ToEmail+")")
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
elif os.name == "nt":
RedColor()
print (MailMessage+" ("+ToEmail+")")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
editlog.write(MailMessage+" to "+ToEmail+"\n") editlog.write(MailMessage+" to "+ToEmail+"\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...\n")
time.sleep(5) time.sleep(5)
@ -256,11 +237,8 @@ while PublicIP <= 2:
createNewip2eIPcf() createNewip2eIPcf()
writeNewip2eIPcf() writeNewip2eIPcf()
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix": GreenColor()
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...") print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...\n") editlog.write("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...\n")
editlog.close() editlog.close()
time.sleep(600) time.sleep(600)

View File

@ -5,7 +5,7 @@
# ip2e (IP to email) - See the log file. | # ip2e (IP to email) - See the log file. |
# Created by clamsawd (clamsawd@openmailbox.org) | # Created by clamsawd (clamsawd@openmailbox.org) |
# Licensed by GPL v.3 | # Licensed by GPL v.3 |
# Last update: 29-10-2015 | # Last update: 30-10-2015 |
# | # |
# Dependences: curl or wget | # Dependences: curl or wget |
# Compatible with Python 3.x | # Compatible with Python 3.x |