Getting IP address of the beagleboard using python

断了今生、忘了曾经 提交于 2019-12-10 10:42:55

问题


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:


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.



来源:https://stackoverflow.com/questions/29726213/getting-ip-address-of-the-beagleboard-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!