I wrote a simple python script which, when executed, gets my BeagleBone Black's ip address and sends it to my email address, preventing me from having to plug the board into my laptop, get the ip, unplug it, and then remotely SSH in. Here is the script
#!/usr/bin/python
"""Find own ip address and email it."""
import socket
import datetime, time
import sys
failed = 1
while(failed):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
my_ip = s.getsockname()[0]
s.close()
failed = 0
except SocketError:
print sys.exc_info()[0]
except:
print error
time.sleep(5)
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
msg = MIMEText("cta_BBB_1 ip address: %s" % my_ip)
me = '<outgoing message email address>'
you = '<incoming message receiver>'
msg['Subject'] = 'cta_BBB_1 ip address at ' + str(datetime.datetime.now())
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('<server_name>', <server_port))
s.login('<login>', '<password>')
s.sendmail(me, [you], msg.as_string())
s.quit()
The script works perfectly if I just run it on the BBB. I then made the script executable and wrote a cron job to fire it, which looks like this (ignore the second line, that deals with resetting the date/time on the BBB):
@reboot /usr/bin/python /home/root/cta_stuff/cta_boot_send_ip.py
30 * * * * /usr/bin/ntpdate-sync silent
Which does show up if I run crontab -l
.
So, when I hard reboot (via the reset button), reboot via ssh, or halt the board and then turn it back on, the cron job does not fire the script (i.e. no email is received with the ip address). I have checked formatting, permissions, etc., on everything I can think of involved in this task on the BBB. If anyone has any idea why it is not working, I would really appreciate some help, as I am totally stumped.
Also, I am currently using the BBB-eMMC-flasher-2013.06.06.img
image for Angstrom.
Problem solved: I am an angstrom newb (previous linux experience is related to ubuntu exclusively), and not much experience with linux generally. So, I had been indiscriminately running opkg upgrade
, following opkg update
, without specifying what was being upgraded, and therefore likely introducing issues throughout my entire build. So, I started over with the 6/20 image, flashed the board, reconfigured everything, and added this to the python script (above the while
loop at the top of the script):
time.sleep(45) #to avoid timing issues
i then added this in cron:
@reboot /home/root/boot_send_ip.py
And now the cron job fires on reboot and I get the IP emailed to me. YAY!
The BB google group has suggested using udhcpc
to handle this type of service, which I will look into in the future, but for now this is all working via cron.