Advertisement
SH1NU11b1

e-mailing you an IP

Nov 6th, 2014
383
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. to = 'me@example.com'
  44. gmail_user = 'test@gmail.com'
  45. gmail_password = 'yourpassword'
  46. smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
  47. smtpserver.ehlo()
  48. smtpserver.starttls()
  49. smtpserver.ehlo
  50. smtpserver.login(gmail_user, gmail_password)
  51. today = datetime.date.today()
  52. # Very Linux Specific
  53. arg='ip route list'
  54. p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
  55. data = p.communicate()
  56. split_data = data[0].split()
  57. ipaddr = split_data[split_data.index('src')+1]
  58. my_ip = 'Your ip is %s' % ipaddr
  59. msg = MIMEText(my_ip)
  60. msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
  61. msg['From'] = gmail_user
  62. msg['To'] = to
  63. smtpserver.sendmail(gmail_user, [to], msg.as_string())
  64. smtpserver.quit()
  65. Save this script using a nice name like 'startup_mailer.py' and make note of its path (like /home/pi/Code/startup_mailer.py)
  66.  
  67. For good measure, make the script executable
  68.  
  69. sudo chmod +x startup_mailer.py
  70. Edit /boot/boot.rc
  71. 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:
  72.  
  73. sudo nano /boot/boot.rc
  74. Add the following at the end of the file, making changes to the path for your directory tree and save.
  75.  
  76. #Script to email ip address upon reboot
  77. python /home/pi/Code/startup_mailer.py
  78. Alternative if using Rasbian
  79. If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:
  80.  
  81. sudo nano /etc/rc.local
  82. Add the python line so the file now looks like this:
  83.  
  84. # rc.local
  85. #
  86. # This script is executed at the end of each multiuser runlevel.
  87. # Make sure that the script will "exit 0" on success or any other
  88. # value on error.
  89. #
  90. # In order to enable or disable this script just change the execution
  91. # bits.
  92. #
  93. # By default this script does nothing.
  94. # Print the IP address if it doesn't work ad sleep 30 before all your code
  95. _IP=$(hostname -I) || true
  96. if [ "$_IP" ]; then
  97. printf "My IP address is %s\n" "$_IP"
  98. python /home/pi/Code/startup_mailer.py
  99. fi
  100. exit 0
  101. Finish up
  102. Reboot your Pi and you should receive an email with your ip address
  103.  
  104. Trouble shooting
  105. + 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.
  106.  
  107. + 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
  108.  
  109. $ msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')
  110.  
  111. Category: RaspberryPi
  112. Navigation menu
  113. Create accountLog inPageDiscussionReadView sourceView history
  114.  
  115. Main Page
  116. Community portal
  117. Current events
  118. Recent changes
  119. Help
  120. Volunteering
  121. Popular Pages
  122. Bug Tracker
  123. Tools
  124. What links here
  125. Related changes
  126. Special pages
  127. Printable version
  128. Permanent link
  129. Page information
  130. This page was last modified on 2 January 2014, at 17:04.
  131. Content is available under a Creative Commons Attribution-ShareAlike 3.0 Unported License unless otherwise noted.
  132. Privacy policyAbout eLinux.orgDisclaimersa Creative Commons Attribution-ShareAlike 3.0 Unported License Powered by MediaWiki
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement