Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import socket
  3. import os
  4. import subprocess
  5. import smtplib
  6. from email.mime.text import MIMEText
  7. import datetime
  8.  
  9. gw = os.popen("ip -4 route show default").read().split()
  10. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  11. s.connect((gw[2], 0))
  12. ipaddr = s.getsockname()[0]
  13. gateway = gw[2]
  14. host = socket.gethostname()
  15. print ("IP:", ipaddr, " GW:", gateway, " Host:", host)
  16.  
  17.  
  18. # Change to your own account information
  19. to = 'me@example.com'
  20. gmail_user = 'test@gmail.com'
  21. gmail_password = 'yourpassword'
  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 ip is %s' % ipaddr
  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
Advertisement