0

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!

Tomer Amir
  • 150
  • 5

1 Answers1

1

I can't help with your Python, but this strikes me as using a sledgehammer to crack a nut.

I have avahi on my Pi so I can "discover" the IP with hostname.local. I am pretty sure avahi starts automatically on newer kernels, or by dhcpcd.

I connect with ssh pi@hostname.local.

This may not work with older Windows (due to the non standard use of .local), but it is worth a try.

Milliways
  • 62,573
  • 32
  • 113
  • 225