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:


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'

       mail = smtplib.SMTP('smtp.gmail.com', 587)

        #Login to SMTP server
        mail.login('****@gmail.com', my_pass)

        mail.sendmail(msg['From'], msg['To'], 'Subject: RP IP\n\nThe Current IP 

        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?


  • Welcome to RPi.se! The problem you are facing is a generic Unix/Linux issue, not specific to the Raspberry Pi. I am flagging this question for migration to Unix & Linux StackExchange.
    – Phil B.
    Oct 24 '15 at 2:19
  • I'm not a python user but I am sure the code as posted has more explicit errors in it than that one, and would not work to do anything no matter where or when you invoked it. I'm also going to hazard a guess that you cargo culted it from somewhere, because if you understood it properly yourself, I think you would be asking a somewhat different question. I suggest you try to decipher that line by consulting python docs and figure out what that error means, which should be a big clue as to why it happens. In any case, python programming questions belong on Stack Overflow.
    – goldilocks
    Oct 24 '15 at 3:58
  • Yeah, no need for a sledgehammer. Just use the bash trick of reverse apostrophes like so: echo `ifconfig` | ssmtp myusername@gmailcom and it will do an ifconfig and send you the results. --> instructions for setting up ssmtp are found in the bottom answer at: raspberrypi.stackexchange.com/questions/12405/… - those apostrophes are found on the tilde key.
    – SDsolar
    Jul 31 '17 at 12:27
  • Of course, your OP doesn't specify why you want this, and what format you would prefer them to be in, or any of that. Sending ifconfig will tell you the answer, but yet may not solve your unspecified problem. So I can't post this as an answer because I am not really sure what you are asking for.
    – SDsolar
    Jul 31 '17 at 12:31

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.

  • I will check it out and come back to you.
    – Tomer Amir
    Oct 24 '15 at 7:28
  • It's working! but I have a problem with it... I installed a mysql server on it, and now I cannot access it with the avahi url. Can you help me with that? or should I open a new question?
    – Tomer Amir
    Oct 24 '15 at 8:22
  • Again, I don't know mysql, but once you are connected it is easy to get the IP e.g. ping pi@hostname.local You should ask a new question (with more detail) if you want help.
    – Milliways
    Oct 24 '15 at 9:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.