SH1NU11b1

e-mailing you an IP

Nov 6th, 2014
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1.  
  2. RPi Email IP On Boot Debian
  3. Back to RPi Guides.
  4.  
  5. Send Email Containing IP Address On Boot
  6.  
  7. Contents [hide]
  8. 1 What does it do?
  9. 2 What do you need?
  10. 3 What skill level is required?
  11. 4 Overview of this guide
  12. 5 Let's Do It
  13. 5.1 Create the python script
  14. 5.2 Edit /boot/boot.rc
  15. 5.3 Alternative if using Rasbian
  16. 5.4 Finish up
  17. 5.5 Trouble shooting
  18. What does it do?
  19. This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)
  20.  
  21. What do you need?
  22. A working, and network enabled Raspberry Pi
  23.  
  24. What skill level is required?
  25. Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).
  26.  
  27. Overview of this guide
  28. You need to
  29.  
  30. Create a python script and store it in a Directory
  31. Make python script executable
  32. Edit /boot/boot.rc
  33. Let's Do It
  34. Create the python script
  35. Copy and paste the following code into a text editor (I'm a nano man myself)
  36.  
  37. import subprocess
  38. import smtplib
  39. import socket
  40. from email.mime.text import MIMEText
  41. import datetime
  42. # Change to your own account information
  43. gmail_user = '[email protected]'
  44. gmail_password = 'yourpassword'
  45. smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
  46. smtpserver.ehlo()
  47. smtpserver.starttls()
  48. smtpserver.ehlo
  49. smtpserver.login(gmail_user, gmail_password)
  50. today = datetime.date.today()
  51. # Very Linux Specific
  52. arg='ip route list'
  53. p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
  54. data = p.communicate()
  55. split_data = data[0].split()
  56. ipaddr = split_data[split_data.index('src')+1]
  57. my_ip = 'Your ip is %s' % ipaddr
  58. msg = MIMEText(my_ip)
  59. msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
  60. msg['From'] = gmail_user
  61. msg['To'] = to
  62. smtpserver.sendmail(gmail_user, [to], msg.as_string())
  63. smtpserver.quit()
  64. Save this script using a nice name like 'startup_mailer.py' and make note of its path (like /home/pi/Code/startup_mailer.py)
  65.  
  66. For good measure, make the script executable
  67.  
  68. sudo chmod +x startup_mailer.py
  69. Edit /boot/boot.rc
  70. Using your text editor once again, edit /boot/boot.rc (this assumes you have already renamed this file to boot.rc If not, see RPi_Advanced_Setup). For example:
  71.  
  72. sudo nano /boot/boot.rc
  73. Add the following at the end of the file, making changes to the path for your directory tree and save.
  74.  
  75. #Script to email ip address upon reboot
  76. python /home/pi/Code/startup_mailer.py
  77. Alternative if using Rasbian
  78. If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:
  79.  
  80. sudo nano /etc/rc.local
  81. Add the python line so the file now looks like this:
  82.  
  83. # rc.local
  84. #
  85. # This script is executed at the end of each multiuser runlevel.
  86. # Make sure that the script will "exit 0" on success or any other
  87. # value on error.
  88. #
  89. # In order to enable or disable this script just change the execution
  90. # bits.
  91. #
  92. # By default this script does nothing.
  93. # Print the IP address if it doesn't work ad sleep 30 before all your code
  94. _IP=$(hostname -I) || true
  95. if [ "$_IP" ]; then
  96. printf "My IP address is %s\n" "$_IP"
  97. python /home/pi/Code/startup_mailer.py
  98. fi
  99. exit 0
  100. Finish up
  101. Reboot your Pi and you should receive an email with your ip address
  102.  
  103. Trouble shooting
  104. + you may add (sleep 30) in the etc/rc.local just after the # if you don't get your email notification hang your pi to a monitor and check at bootup if it shows My ip adresss is .... if it doesn't do that! sleep 30. etc/rc.local is linked to etc/init.d/rc.local it's an special service that starts on bootup but it seems there are some bugs in it.
  105.  
  106. + If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor. Also in etc/hosts on the bottom of the page
  107.  
  108. $ msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')
  109.  
  110. Category: RaspberryPi
  111. Navigation menu
  112. Create accountLog inPageDiscussionReadView sourceView history
  113.  
  114. Main Page
  115. Community portal
  116. Current events
  117. Recent changes
  118. Help
  119. Volunteering
  120. Popular Pages
  121. Bug Tracker
  122. Tools
  123. What links here
  124. Related changes
  125. Special pages
  126. Printable version
  127. Permanent link
  128. Page information
  129. This page was last modified on 2 January 2014, at 17:04.
  130. Content is available under a Creative Commons Attribution-ShareAlike 3.0 Unported License unless otherwise noted.
  131. Privacy policyAbout eLinux.orgDisclaimersa Creative Commons Attribution-ShareAlike 3.0 Unported License Powered by MediaWiki
Advertisement
Add Comment
Please, Sign In to add comment