skanc.com Linux, Microsoft, Raspberry Pi, Python, Android

22Aug/121

Raspberry Pi – Tips & Tricks 9 – Sending External IP To via Gmail

nano external_ip
------------------------------------------------------------------------------------------------------
import urllib
import smtplib
import re

#External IP Address
f = urllib.urlopen("http://www.canyouseeme.org/")
html_doc = f.read()
f.close()
m = re.search('(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-

5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)',html_doc)
#print m.group(0)
data=m.group()

#Mail send
fromaddr = 'youraccount@gmail.com'
toaddrs  = ['test@gmail.com']
msg = 'Raspberry Pi External IP Addreds is ' + data + ' ...!'

# Credentials (if needed)
username = 'youraccount@gmail.com'
password = 'yourpassword'

# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
for toad in toaddrs:
    server.sendmail(fromaddr, toad, msg)
server.quit()
------------------------------------------------------------------------------------------------------

chmod a+x external_ip