Guest User

Untitled

a guest
Jan 7th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. __author__ = 'Cody Giles'
  2. __license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License"
  3. __version__ = "1.0"
  4. __maintainer__ = "Cody Giles"
  5. __status__ = "Production"
  6.  
  7. import subprocess
  8. import smtplib
  9. from email.mime.text import MIMEText
  10. import datetime
  11.  
  12. def connect_type(word_list):
  13. """ This function takes a list of words, then, depeding which key word, returns the corresponding
  14. internet connection type as a string. ie) 'ethernet'.
  15. """
  16. if 'wlan0' in word_list or 'wlan1' in word_list:
  17. con_type = 'wifi'
  18. elif 'eth0' in word_list:
  19. con_type = 'ethernet'
  20. else:
  21. con_type = 'current'
  22.  
  23. return con_type
  24.  
  25. # Change to your own account information
  26. # Account Information
  27. to = 'username@email.com' # Email to send to.
  28. gmail_user = 'username@gmail.com' # Email to send from. (MUST BE GMAIL)
  29. gmail_password = 'gmailpassword' # Gmail password.
  30. smtpserver = smtplib.SMTP('smtp.gmail.com', 587) # Server to use.
  31.  
  32. smtpserver.ehlo() # Says 'hello' to the server
  33. smtpserver.starttls() # Start TLS encryption
  34. smtpserver.ehlo()
  35. smtpserver.login(gmail_user, gmail_password) # Log in to server
  36. today = datetime.date.today() # Get current time/date
  37.  
  38. arg='ip route list' # Linux command to retrieve ip addresses.
  39. # Runs 'arg' in a 'hidden terminal'.
  40. p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
  41. data = p.communicate() # Get data from 'p terminal'.
  42.  
  43. # Split IP text block into three, and divide the two containing IPs into words.
  44. ip_lines = data[0].splitlines()
  45. split_line_a = ip_lines[0].split()
  46. split_line_b = ip_lines[1].split()
  47.  
  48. # con_type variables for the message text. ex) 'ethernet', 'wifi', etc.
  49. ip_type_a = connect_type(split_line_a)
  50. ip_type_b = connect_type(split_line_b)
  51.  
  52. """Because the text 'src' is always followed by an ip address,
  53. we can use the 'index' function to find 'src' and add one to
  54. get the index position of our ip.
  55. """
  56. ipaddr_a = split_line_a[split_line_a.index('src')+1]
  57. ipaddr_b = split_line_b[split_line_b.index('src')+1]
  58.  
  59. # Creates a sentence for each ip address.
  60. my_ip_a = 'Your %s ip is %s' % (ip_type_a, ipaddr_a)
  61. my_ip_b = 'Your %s ip is %s' % (ip_type_b, ipaddr_b)
  62.  
  63. # Creates the text, subject, 'from', and 'to' of the message.
  64. msg = MIMEText(my_ip_a + "\n" + my_ip_b)
  65. msg['Subject'] = 'IPs For RaspberryPi on %s' % today.strftime('%b %d %Y')
  66. msg['From'] = gmail_user
  67. msg['To'] = to
  68. # Sends the message
  69. smtpserver.sendmail(gmail_user, [to], msg.as_string())
  70. # Closes the smtp server.
  71. smtpserver.quit()
Add Comment
Please, Sign In to add comment