Show colors in messages

This commit is contained in:
clamsawd 2015-10-29 16:31:45 +01:00
parent 50db350618
commit b9e4cf9535

View File

@ -139,15 +139,32 @@ 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
def GreenColor():
os.system("color 2")
def RedColor():
os.system("color 4")
def OrangeColor():
os.system("color 6")
#Run ip2e daemon #Run ip2e daemon
ClearScreen() ClearScreen()
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix":
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)") print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
if os.name == "posix": if os.name == "posix":
print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["HOME"]+"/.ip2e/ip2e.log") print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] Log in "+os.environ["HOME"]+"/.ip2e/ip2e.log")
elif os.name == "nt": 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")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" > ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" > ip2e.log")
if os.name == "posix":
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
elif os.name == "nt":
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds... >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds... >> ip2e.log")
if os.name == "posix": if os.name == "posix":
@ -161,6 +178,10 @@ 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":
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
elif os.name == "nt":
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...") print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] IP Updating... >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] IP Updating... >> ip2e.log")
NewIPRaw = os.popen('curl -s icanhazip.com').read() NewIPRaw = os.popen('curl -s icanhazip.com').read()
@ -173,9 +194,14 @@ while PublicIP <= 2:
GetCurrentIP += 2 GetCurrentIP += 2
else: else:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix":
print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
elif os.name == "nt":
RedColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP") print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Error getting IP >> ip2e.log")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Error getting IP >> ip2e.log")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds... >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds... >> ip2e.log")
if os.name == "posix": if os.name == "posix":
os.system("sleep 5") os.system("sleep 5")
@ -184,10 +210,18 @@ while PublicIP <= 2:
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":
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed") print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] IP has not changed >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] IP has not changed >> ip2e.log")
else: else:
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix":
print (chr(27)+"[1;32m"+"[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP) print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
os.system("echo [ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+" >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+" >> ip2e.log")
SendEmailOK = 1 SendEmailOK = 1
@ -200,15 +234,24 @@ while PublicIP <= 2:
if ErrorSendEmail == 0: if ErrorSendEmail == 0:
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":
print (chr(27)+"[1;32m"+MailMessage+" ("+ToEmail+")")
elif os.name == "nt":
GreenColor()
print (MailMessage+" ("+ToEmail+")") print (MailMessage+" ("+ToEmail+")")
os.system("echo "+MailMessage+" to "+ToEmail+" >> ip2e.log") os.system("echo "+MailMessage+" to "+ToEmail+" >> ip2e.log")
SendEmailOK += 2 SendEmailOK += 2
else: else:
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":
print (chr(27)+"[1;31m"+MailMessage+" ("+ToEmail+")")
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
elif os.name == "nt":
RedColor()
print (MailMessage+" ("+ToEmail+")") print (MailMessage+" ("+ToEmail+")")
os.system("echo "+MailMessage+" to "+ToEmail+" >> ip2e.log")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...") print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds...")
os.system("echo "+MailMessage+" to "+ToEmail+" >> ip2e.log")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds... >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] Retrying in 5 seconds... >> ip2e.log")
if os.name == "posix": if os.name == "posix":
os.system("sleep 5") os.system("sleep 5")
@ -228,6 +271,10 @@ while PublicIP <= 2:
createNewip2eIPcf() createNewip2eIPcf()
writeNewip2eIPcf() writeNewip2eIPcf()
CurrentTime = time.strftime("%H:%M") CurrentTime = time.strftime("%H:%M")
if os.name == "posix":
print (chr(27)+"[1;33m"+"[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
elif os.name == "nt":
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...") print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
os.system("echo [ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes... >> ip2e.log") os.system("echo [ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes... >> ip2e.log")
WaitTenMinutes() WaitTenMinutes()