itxakapi

external_ip.py

Dec 22nd, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import bs4
  2. import subprocess
  3. import smtplib
  4. import socket
  5. from email.mime.text import MIMEText
  6. import datetime
  7. import urllib2
  8.  
  9. f = urllib2.urlopen("http://www.whatismyip.net")
  10. data = f.read()
  11.  
  12. soup = bs4.BeautifulSoup(data)
  13.  
  14. external_ip = soup.strong.string
  15.  
  16.  
  17.  
  18. # Change to your own account information
  19. to = ''
  20. gmail_user = ''
  21. gmail_password = ''
  22. smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
  23. smtpserver.ehlo()
  24. smtpserver.starttls()
  25. smtpserver.ehlo
  26. smtpserver.login(gmail_user, gmail_password)
  27. today = datetime.date.today()
  28. # Very Linux Specific
  29. arg='ip route list'
  30. p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
  31. data = p.communicate()
  32. split_data = data[0].split()
  33. ipaddr = split_data[split_data.index('src')+1]
  34. my_ip = 'Your local ip is %s \nYour external ip is %s' % (ipaddr, external_ip)
  35. msg = MIMEText(my_ip)
  36. msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
  37. msg['From'] = gmail_user
  38. msg['To'] = to
  39. smtpserver.sendmail(gmail_user, [to], msg.as_string())
  40. smtpserver.quit()
Advertisement
Add Comment
Please, Sign In to add comment