I'm trying to show my local IP on an LCD during startup. My script worked fine during normal testing via the command line, but when called at startup it doesn't seem to be able to fetch the IP.
I tried to call the script via crontab and init.d. Both sequence locations didn't return the IP. My obvious guess is that the interface isn't up yet. But when/where exactly can I call my python script so I can get the IP address and use it as a string?
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
print get_ip_address('eth0')
# prints the local IP when running via command line
# gives error when running during startup/boot:
# -->(struct.pack('256s', ifname[:15]) / IOError: [Errno 99] Cannot assign requested address)