营养咨询
python判断是否连接wifi_python操作linux连接wifi,查看wifi连捕鼠弓
aod接状态⽅法
# 查询所有物理⽹卡def_query_net_card_info(if_name): net_card_info={"mac": "","type": "eth","ip": "","netmask": "",
}try:
池州学院学报
net_info=netifaces.ifaddresses(if_name)exceptValueError:
<("No such interface: %s" %if_name)returnnet_card_info#如果为⽆线⽹卡,将修改⽹卡
类型为wlan
if if_name d_excute("iw dev | awk '$1==\"Interface\" {print $2}'"):
net_card_info["type"] = "wlan"net_card_info["mac"] = net_info[netifaces.AF_LINK][0]["addr"].upper()
ipv4_info=(netifaces.AF_INET)ifipv4_info:
net_card_info["ip"] = ipv4_info[0]["addr"]
net_card_info["netmask"] = ipv4_info[0]["netmask"]returnnet_card_info
@staticmethoddefquery_net_cards():"""{
"enp2s0": // 接⼝名称
{
"mac": "98:FA:9B:99:E5:6A", // MAC地址
"type": "eth", // 接⼝类型,eth-有线⽹卡, wlan-⽆线⽹卡
"ip": "192.168.2.90", // 接⼝当前IP
"netmask": "255.255.255.0", // 接⼝掩码
"dns":"192.168.2.1", // dns服务器ip
"gateway": "192.168.2.1" // ⽹管地址
},
"wlan0":
{
"mac": "98:FA:9B:99:E5:6A", // MAC地址
"type": "wlan", // 接⼝类型,eth-有线⽹卡, wlan-⽆线⽹卡
抗弯强度
"ip": "", // 接⼝当前IP
"netmask": "", // 接⼝掩码
"dns":"192.168.2.1", // dns服务器ip
滇西1994
"gateway": "192.168.2.1" // ⽹管地址
}
}"""net_cards=dict()#获取所有物理⽹卡名称 command = "ls -d /sys/class/net/*/device | cut -d/ -f5"d_excute(command)#获取dns服务器ip
dns_path = '/f'dns_list=[]ists(dns_path):
dns_list= d_excute("cat %s|grep ^nameserver|awk '{print $2}'" % dns_path, b_print=False)#获取⽹管地址gateways = netifaces.gateways().get('default').get(netifaces.AF_INET)for interface ininterfaces:
net_card= Network._query_net_card_info(if_name=interface)
net_card["dns"] = dns_list if dns_list else ""net_card["gateway"] = gateways[0] if gateways else
""net_cards[interface]=net_cardreturn net_cards