Getting IP address of the beagleboard using python

2019-05-27 11:34发布

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?

1条回答
SAY GOODBYE
2楼-- · 2019-05-27 11:35

SIOCGIFADDR : is stands for get internet interface address means 'eth0'. This is CPU macro which is written at address 0x8915. You cant take access of that cpu address so you have to go through pack and unpack function using parameters "16sH2x4s8x" IP address which u want from machine it have 4 fields like "192.168.5.20" so that (4*4) 16 is required likewise search more fields of pack unpack functions.

查看更多
登录 后发表回答