Guest User

Untitled

a guest
Nov 18th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #!/usr/bin/python
  2. # The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
  3. # (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
  4. #
  5. # This script emails you the IP address if it detects an ethernet address other than it's usual address
  6. # that it normally has, i.e. on your home network.
  7.  
  8. import smtp, string
  9.  
  10. ################################################
  11. ###### Remember to set all constants here ######
  12. ################################################
  13. FIXED_IP = '10.0.1.2'
  14. IP_FILEPATH = '/home/pi/current_ip.txt'
  15. SMTP_USERNAME = 'feersumendjinn@gmail.com'
  16. SMTP_PASSWORD = 'password'
  17. SMTP_RECIPIENT = 'feersumendjinn@gmail.com'
  18. SMTP_SERVER = 'smtp.gmail.com'
  19. SSL_PORT = 465
  20. ################################################
  21.  
  22.  
  23. ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
  24. subprocess.call(ipaddr_string, shell=True)
  25. inet_string = ''
  26.  
  27. ip_file = file(IP_FILEPATH, 'r')
  28. for line in ip_file:
  29.  
  30. if 'eth0:' in line:
  31. inet_line = ip_file.next()
  32. _time = time.asctime()
  33. inet_string = inet_line[9:(inet_line.index('/'))]
  34.  
  35. if inet_string != FIXED_IP:
  36.  
  37. SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
  38. TO = SMTP_RECIPIENT
  39. FROM = SMTP_USERNAME
  40. text = 'The IP address is: %s' % inet_string
  41. BODY = string.join((
  42. 'From: %s' % FROM,
  43. 'To: %s' % TO,
  44. 'Subject: %s' % SUBJECT,
  45. '',
  46. text
  47. ), '\r\n')
  48. server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
  49. server.login(SMTP_USERNAME, SMTP_PASSWORD)
  50. server.sendmail(FROM, [TO], BODY)
  51. server.quit()
  52. print '[' + '\033[36;1m' + 'info' + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
  53.  
  54. elif inet_string == FIXED_IP:
  55. print '[' + '\033[32;1m' + ' ok ' + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'
  56.  
  57. else:
  58. print '[' + '\033[31;1m' + 'warning' + '\033[0m' + ']' + ' eth0 not found in file!'
  59.  
  60.  
  61. ip_file.close()
Add Comment
Please, Sign In to add comment