Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import time
  4.  
  5. time.sleep(10)
  6.  
  7. import smtplib
  8. import email
  9. from email.mime.text import MIMEText
  10. import datetime
  11.  
  12. import urllib.request as urllibReq
  13.  
  14. import pathlib
  15. from os import path
  16.  
  17. from uptime import uptime
  18.  
  19. STORAGE_FILE_PATH = pathlib.Path(path.expanduser('~')) / '.ip'
  20.  
  21. def read_ip() -> str:
  22. try:
  23. return STORAGE_FILE_PATH.read_text()
  24. except:
  25. print('CONFIG NOT FOUND')
  26. return ''
  27.  
  28. def set_ip(new_ip):
  29. STORAGE_FILE_PATH.write_text(new_ip)
  30.  
  31. ip = ""
  32.  
  33. f = urllibReq.urlopen('http://ip.42.pl/raw')
  34. newIP = f.read().decode('utf-8')
  35. f.close()
  36.  
  37. subject: str = None
  38.  
  39. if newIP != read_ip():
  40. subject = 'changed'
  41.  
  42. if uptime() < 60:
  43. subject = 'powerup'
  44.  
  45. if subject is not None:
  46.  
  47. server = smtplib.SMTP('smtp.gmail.com', 587)
  48. server.ehlo()
  49. server.starttls()
  50. server.ehlo()
  51. server.login("automaticraspberrypi@gmail.com", "p8T-Lfg-vnp-KUb")
  52.  
  53. msg_content = datetime.datetime.now().isoformat() + "<br>" + newIP
  54. message = MIMEText(msg_content, 'html')
  55.  
  56. message['From'] = 'Himbeerkuchen <automaticraspberrypi@gmail.com>'
  57. message['To'] = 'Hans <hans.schuelein@web.de>'
  58. message['Subject'] = f'IP REPORT - pi {subject}'
  59.  
  60. msg_full = message.as_string()
  61.  
  62. server.sendmail("automaticraspberrypi@gmail.com", "hans.schuelein@web.de", msg_full)
  63.  
  64. print("sent new IP: " + newIP)
  65.  
  66. set_ip(newIP)
  67.  
  68.  
  69. """
  70. #!/usr/bin/env python3
  71.  
  72. import time
  73.  
  74. time.sleep(10)
  75.  
  76. import smtplib
  77. import email
  78. from email.mime.text import MIMEText
  79. import datetime
  80.  
  81. import urllib.request as urllibReq
  82.  
  83. ip = ""
  84.  
  85. while True:
  86. f = urllibReq.urlopen('http://ip.42.pl/raw')
  87. newIP = f.read().decode('utf-8')
  88. f.close()
  89.  
  90. if newIP != ip:
  91.  
  92.  
  93. server = smtplib.SMTP('smtp.gmail.com', 587)
  94. server.ehlo()
  95. server.starttls()
  96. server.ehlo()
  97. server.login("automaticraspberrypi@gmail.com", "p8T-Lfg-vnp-KUb")
  98.  
  99. msg_content = datetime.datetime.now().isoformat() + "<br>" + newIP
  100. message = MIMEText(msg_content, 'html')
  101.  
  102. message['From'] = 'Himbeerkuchen <automaticraspberrypi@gmail.com>'
  103. message['To'] = 'Hans <hans.schuelein@web.de>'
  104. if ip == "":
  105. message['Subject'] = 'IP REPORT - pi powerup'
  106. else:
  107. message['Subject'] = 'IP REPORT - ip changed'
  108.  
  109. msg_full = message.as_string()
  110.  
  111. server.sendmail("automaticraspberrypi@gmail.com", "hans.schuelein@web.de", msg_full)
  112. # server.sendmail("automaticraspberrypi@gmail.com", "gabriel.moser@web.de", msg_full)
  113.  
  114.  
  115. print("sent new IP: "+newIP)
  116.  
  117. ip = newIP
  118.  
  119. time.sleep(60*10)
  120. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement