Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import time
- time.sleep(10)
- import smtplib
- import email
- from email.mime.text import MIMEText
- import datetime
- import urllib.request as urllibReq
- import pathlib
- from os import path
- from uptime import uptime
- STORAGE_FILE_PATH = pathlib.Path(path.expanduser('~')) / '.ip'
- def read_ip() -> str:
- try:
- return STORAGE_FILE_PATH.read_text()
- except:
- print('CONFIG NOT FOUND')
- return ''
- def set_ip(new_ip):
- STORAGE_FILE_PATH.write_text(new_ip)
- ip = ""
- f = urllibReq.urlopen('http://ip.42.pl/raw')
- newIP = f.read().decode('utf-8')
- f.close()
- subject: str = None
- if newIP != read_ip():
- subject = 'changed'
- if uptime() < 60:
- subject = 'powerup'
- if subject is not None:
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.ehlo()
- server.starttls()
- server.ehlo()
- server.login("automaticraspberrypi@gmail.com", "p8T-Lfg-vnp-KUb")
- msg_content = datetime.datetime.now().isoformat() + "<br>" + newIP
- message = MIMEText(msg_content, 'html')
- message['From'] = 'Himbeerkuchen <automaticraspberrypi@gmail.com>'
- message['To'] = 'Hans <hans.schuelein@web.de>'
- message['Subject'] = f'IP REPORT - pi {subject}'
- msg_full = message.as_string()
- server.sendmail("automaticraspberrypi@gmail.com", "hans.schuelein@web.de", msg_full)
- print("sent new IP: " + newIP)
- set_ip(newIP)
- """
- #!/usr/bin/env python3
- import time
- time.sleep(10)
- import smtplib
- import email
- from email.mime.text import MIMEText
- import datetime
- import urllib.request as urllibReq
- ip = ""
- while True:
- f = urllibReq.urlopen('http://ip.42.pl/raw')
- newIP = f.read().decode('utf-8')
- f.close()
- if newIP != ip:
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.ehlo()
- server.starttls()
- server.ehlo()
- server.login("automaticraspberrypi@gmail.com", "p8T-Lfg-vnp-KUb")
- msg_content = datetime.datetime.now().isoformat() + "<br>" + newIP
- message = MIMEText(msg_content, 'html')
- message['From'] = 'Himbeerkuchen <automaticraspberrypi@gmail.com>'
- message['To'] = 'Hans <hans.schuelein@web.de>'
- if ip == "":
- message['Subject'] = 'IP REPORT - pi powerup'
- else:
- message['Subject'] = 'IP REPORT - ip changed'
- msg_full = message.as_string()
- server.sendmail("automaticraspberrypi@gmail.com", "hans.schuelein@web.de", msg_full)
- # server.sendmail("automaticraspberrypi@gmail.com", "gabriel.moser@web.de", msg_full)
- print("sent new IP: "+newIP)
- ip = newIP
- time.sleep(60*10)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement