SIOCGIFADDR = 0x8915
def getIpAddr(iface = 'eth0'):
ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00'*14)
try:
res = fcntl.ioctl(sock, SIOCGIFADDR, ifreq)
except:
return None
ip = struct.unpack('16sH2x4s8x', res)[2]
return socket.inet_ntoa(ip)
At each step what are the return values of the functions?
And, what is SIOCGIFADDR?
Also, why [2]
has been used following the unpack()
function?