Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Author: Jen [jen.development@gmail.com]
- # Checks the 'internal' IP of the device. If it has changed since the last
- # time it was checked, sends an email to a mail address with the new IP.
- #
- # It was originally designed to use with the RaspberryPi and be able to connect
- # to the device in LAN.
- import smtplib
- import subprocess
- import time
- def GetInternalIP(interface):
- output=subprocess.check_output(["ifconfig", interface])
- output=output.decode('utf-8')
- # Parse ifconfig output
- lines=output.split("\n")
- ipAddr=lines[1].strip().split(" ")[0].split(":")[1]
- return ipAddr
- def SendMail():
- dateAndTime=time.strftime("%A, %d %m %Y %H:%M:%S (%Z)", time.localtime())
- subject="Raspberry Pi event [{}]".format(ipAddr)
- body="Current raspberry IP is: {}\n\n\n{}".format(ipAddr, dateAndTime)
- msg="From: {0}\nTo: {1}\nSubject: {2}\n\n{3}".format(fromAddr, toAddr[0], subject, body)
- # Create SMTP Object
- smtpObj=smtplib.SMTP(smtpServer, smtpPort)
- smtpObj.ehlo() # Identify client
- smtpObj.starttls()
- smtpObj.ehlo() # Identify client under TLS
- smtpObj.login(smtpUser, smtpPass)
- smtpObj.sendmail(fromAddr, toAddr, msg)
- smtpObj.quit()
- return
- interface="wlan0"
- prevIP=None #NO TOCAR
- smtpServer="smtp.gmail.com"
- smtpPort=587
- smtpUser="tu.correo@gmail.com"
- smtpPass="contraseñadelcorreo"
- fromAddr="<tu.correo@gmail.com>"
- toAddr=["<tu.correo@gmail.com>"]
- # Workaround to gain time while some necessary network resources are being prepared
- time.sleep(30)
- while True:
- ipAddr=GetInternalIP(interface)
- if ipAddr != prevIP:
- SendMail()
- prevIP=ipAddr
- time.sleep(900) # 15 minutes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement