Advertisement
schoolpiclub

Email IP address on start up

Apr 20th, 2014
2,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import time
  2. import commands
  3. import re
  4. import smtplib
  5.  
  6. ####--[CONFIGURATION]
  7. server = 'smtp.gmail.com' #smtp server address
  8. server_port = '587' #port for smtp erver
  9.  
  10. username = '' #gmail account
  11. password = '' #password for that gmail account
  12.  
  13. fromaddr = '' #address to send from
  14. toaddr = '' #address to send IP to
  15. message = 'RPi\'s address: ' #message that is sent
  16. ####--[/CONFIGURATION]
  17.  
  18. #the interface may be wifi and it needs time to initialize
  19. #so wait a little bit before parsing ifconifg
  20. time.sleep(30)
  21.  
  22. #extract the ip address (or addresses) from the ifconfig
  23. found_ips = []
  24. ips = re.findall( r'[0-9]+(?:\.[0-9]+){3}', commands.getoutput("/sbin/ifconfig"))
  25. for ip in ips:
  26. if ip.startswith("255") or ip.startswith("127") or ip.endswith("255"):
  27. continue
  28. found_ips.append(ip)
  29.  
  30. message += ", ".join(found_ips)
  31. headers = ["From: " + fromaddr,
  32. "To: " + toaddr,
  33. "MIME-Version: 1.0",
  34. "Content-Type: text/html"]
  35. headers = "\r\n".join(headers)
  36.  
  37. server = smtplib.SMTP(server + ':' + server_port)
  38. server.ehlo()
  39. server.starttls()
  40. server.ehlo()
  41. server.login('gmailusername','gmailpassword') #insert your gmail account username and password
  42. server.sendmail(fromaddr, toaddr, headers + "\r\n\r\n" + message)
  43. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement