Retrieving network mask in Python

前端 未结 8 959
不知归路
不知归路 2021-01-05 07:59

How would one go about retrieving a network device\'s netmask (In Linux preferably, but if it\'s cross-platform then cool)? I know how in C on Linux but I can\'t find a way

8条回答
  •  孤城傲影
    2021-01-05 08:07

    This works for me in Python 2.2 on Linux:

    iface = "eth0"
    socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', iface))[20:24])
    

提交回复
热议问题