I'm trying to make my RP accessible over shh on WIFI, but I don't have control over my router (I live in student dorms) and I cannot config a static IP, so I wrote a python script that will get the current IP and send it to my gmail account.
Then I added the script to crontab
.
Here is the script:
#!/usr/bin/python
import socket
import struct
import fcntl
import smtplib
from email.mime.text import MIMEText
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'wlan0'[0:15])[20:24]))
msg = MIMEText(ip)
msg['Subject'] = 'RP IP'
msg['From'] = '****@gmail.com'
msg['To'] = '****@gmail.com'
try:
mail = smtplib.SMTP('smtp.gmail.com', 587)
#Login to SMTP server
mail.starttls()
mail.login('****@gmail.com', my_pass)
mail.sendmail(msg['From'], msg['To'], 'Subject: RP IP\n\nThe Current IP
mail.quit
print "Success!"
except SMTPException:
print "Error"
The problem is that the scripts returns an error:
Traceback (most recent call last):
File "/home/pi/Programming/python/send_ip_by_mail.py", line 11, in <module>
ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'wlan0'[0:15])[20:24]))
IOError: [Errno 99] Cannot assign requested address
When I run the script myself it works fine, but on startup it does not. What is the problem?
Thanks!