From my previous code on How To Print Active Network Interface List in Linux using Python, I got this question.
Question from jimchris:
Nice work, now how do we get ip address from ifacedata?
My Answer:
Here's how... Add this function to your python code:
def getifip(ifn):
import socket, fcntl, struct
sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(sck.fileno(),0x8915,struct.pack('256s', ifn[:15]))[20:24])
Then use it in your previous code like this:
print getifip(ifacedata[0])
Update: you can also download the python source code here
Wish you luck and Happy Coding!
