#!/usr/bin/env python2 import os import urllib2 import smtplib import time from datetime import datetime from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText site= "https://api.ipify.org" hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive'} req = urllib2.Request(site, headers=hdr) current_ip = "" while (True): fqn = os.uname()[1] external_ip = urllib2.urlopen(req).read() #print(external_ip) if current_ip != external_ip: current_ip = external_ip fromaddr = "FROMADDR" toaddr = "TOADDR" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "IP Address change" body = 'A new IP has been detected: %s' % str(external_ip) msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('mail.domain.de', 587) server.starttls() server.login(fromaddr, "Passwort") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit() time.sleep(600)