利用第三方模块netifaces收集网卡信息
利用第三方模块netifaces对网卡信息的分析比较简单。
1 import netifaces 2 3 def get_interface_list(): 4 return netifaces.interfaces() 5 6 7 def get_addresses(interface): 8 result = netifaces.ifaddresses(interface) 9 address_dict = {} 10 for k,v in result.items(): 11 if k == 17: 12 mac_address = v[0]['addr'] 13 address_dict['mac_address'] = mac_address 14 elif k == 2: 15 ip_address = v[0]['addr'] 16 net_mask = v[0]['netmask'] 17 address_dict['ip_address'] = ip_address 18 address_dict['netmask'] = net_mask 19 return address_dict 20 21 if __name__ == "__main__": 22 interface_list = get_interface_list() 23 for interface in interface_list: 24 address_dict = get_addresses(interface) 25 print("Interface information for %s: \n" % interface) 26 print("IP address: %s" % address_dict['ip_address']) 27 print("Netmask: %s\n" % address_dict['netmask'])
STRIVE FOR PROGRESS,NOT FOR PERFECTION