34 lines
827 B
Python
34 lines
827 B
Python
#coding=utf-8
|
|
import psutil
|
|
#获取网卡名称和其ip地址
|
|
|
|
ipv4_index = 2
|
|
ipv6_index = 10
|
|
mac_index = 17
|
|
|
|
def print_ip_address(ip):
|
|
print("ipaddr: %s" %(ip))
|
|
|
|
def get_netcard():
|
|
netcard_info = []
|
|
info = psutil.net_if_addrs()
|
|
|
|
|
|
#print(info)
|
|
#print("-------------------------------")
|
|
#print(info.items())
|
|
#print("===============================")
|
|
|
|
for k,v in info.items():
|
|
#print(k,v)
|
|
#print("#########################")
|
|
for item in v:
|
|
print(item)
|
|
if item[0] == 2 and not item[1]=='127.0.0.1':
|
|
#if item[0] == ipv4_index:
|
|
print_ip_address(item[1])
|
|
netcard_info.append((k,item[1]))
|
|
return netcard_info
|
|
if __name__ == '__main__':
|
|
print("%s" %(get_netcard()))
|