Retrieving network mask in Python

前端 未结 8 1036
不知归路
不知归路 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:22

    Using the python pyroute2 library you can get all network element attributes:

    from pyroute2 import IPRoute
    ip = IPRoute()
    info = [{'iface': x['index'], 'addr': x.get_attr('IFA_ADDRESS'), 'mask':  x['prefixlen']} for x in ip.get_addr()]
    

    More information available here:http://pyroute2.org/pyroute2-0.3.14p4/iproute.html

提交回复
热议问题