Created
January 31, 2016 02:41
-
-
Save csabatini/104231686c513d9a20fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| import requests | |
| import json | |
| import subprocess | |
| def evaluate_ips(): | |
| r = requests.get('http://ipinfo.io') | |
| current_ip = json.loads(r.text)['ip'] | |
| f = open('/root/last_ip.txt', 'r+') | |
| last_ip = f.read().strip() | |
| f.seek(0) | |
| f.write(current_ip + '\n') | |
| f.truncate() | |
| f.close() | |
| if current_ip != last_ip: | |
| compose_email( current_ip, last_ip ) | |
| send_email() | |
| def compose_email( current, last ): | |
| with open('/root/mail.txt', 'w') as f: | |
| f.write('From: "alarmpi" <alarm3dot14[AT]gmail.com>\n') | |
| f.write('To: "Caleb Sabatini" <calebsabatini[AT]gmail.com>\n') | |
| f.write('Subject: Public IP change detected\n\n') | |
| f.write('Detected by raspberry pi on LAN\n') | |
| f.write('Last IP: ' + last + '\nCurrent IP: ' + current + '\n') | |
| def send_email(): | |
| bashCmd = """curl --url smtps://smtp.gmail.com:465 --ssl-reqd \ | |
| --mail-from "alarm3dot14[AT]gmail.com" --mail-rcpt "calebsabatini[AT]gmail.com" \ | |
| --upload-file /root/mail.txt --user "user:pass" --insecure""" | |
| subprocess.call(bashCmd, shell=True) | |
| evaluate_ips() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment