Je suis nouveau ici et j'apprends juste python. J'ai besoin d'aide pour obtenir la bonne adresse mac de ma carte réseau dans Windows en utilisant python. J'ai essayé de chercher, et j'ai trouvé ça:
Python - Get mac address
Obtenir l'adresse MAC
Analyse de sortie de commande en Python
Analyse de la sortie 'ipconfig /all' de Windows
Si je lance "ipconfig /all" depuis l'invite de commande, j'obtiens ceci:
Windows-IP-Konfiguration
Hostname............: DESKTOP-CIRBA63
Primäres DNS-Suffix.......:
Knotentyp............: Hybrid
IP-Routing aktiviert......: Nein
WINS-Proxy aktiviert......: Nein
Ethernet-Adapter Ethernet:
Verbindungsspezifisches DNS-Suffix:
Beschreibung...........: Realtek PCIe FE Family Controller
Physische Adresse........: 32-A5-2C-0B-14-D9
DHCP aktiviert..........: Nein
Autokonfiguration aktiviert...: Ja
IPv4-Adresse..........: 192.168.142.35(Bevorzugt)
Subnetzmaske..........: 255.255.255.0
Standardgateway.........: 192.168.142.1
DNS-Server...........: 8.8.8.8
8.8.4.4
NetBIOS über TCP/IP.......: Deaktiviert
Ethernet-Adapter Ethernet 2:
Medienstatus...........: Medium getrennt
Verbindungsspezifisches DNS-Suffix:
Beschreibung...........: Norton Security Data Escort Adapter
Physische Adresse........: 00-CE-35-1B-77-5A
DHCP aktiviert..........: Ja
Autokonfiguration aktiviert...: Ja
Tunneladapter isatap.{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}:
Medienstatus...........: Medium getrennt
Verbindungsspezifisches DNS-Suffix:
Beschreibung...........: Microsoft ISATAP Adapter
Physische Adresse........: 00-00-00-00-00-00-00-A0
DHCP aktiviert..........: Nein
Autokonfiguration aktiviert...: Ja
J'ai besoin d'obtenir l'adresse mac de ma carte réseau Realtek ( 32-A5-2C-0B-14-D9 ), pas celle créée par Norton ou le tunneling Windows. Python m'a donné un autre résultat d'adresse mac si j'utilise:
"uuid.getnode() or "getmac"
je pense que le meilleur moyen est d'obtenir la sortie de
"ipconfig /all"
, en regardant "Realtek" sur "Beschreibung", puis d'obtenir les informations "Physische Adresse" pour obtenir ma véritable adresse mac. Comment faire cela en python sous windows? Toute aide est appréciée. Merci d'avance.
Solution du problème
Vous pouvez récupérer les informations de l'interface Windows à l'aide de wmic au format XML, puis convertir le xml en dict. À partir du dict résultant, vous pouvez rassembler toutes les informations nécessaires :
def get_interfaces_with_mac_addresses(interface_name_substring=''):
import subprocess
import xml.etree.ElementTree
cmd = 'wmic.exe nic'
if interface_name_substring:
cmd += ' where "name like \'%%%s%%\'" ' % interface_name_substring
cmd += ' get /format:rawxml'
DETACHED_PROCESS = 8
xml_text = subprocess.check_output(cmd, creationflags=DETACHED_PROCESS)
# convert xml text to xml structure
xml_root = xml.etree.ElementTree.fromstring(xml_text)
xml_types = dict(
datetime=str,
boolean=lambda x: x[0].upper() == 'T',
uint16=int,
uint32=int,
uint64=int,
string=str,
)
def xml_to_dict(xml_node):
""" Convert the xml returned from wmic to a dict """
dict_ = {}
for child in xml_node:
name = child.attrib['NAME']
xml_type = xml_types[child.attrib['TYPE']]
if child.tag == 'PROPERTY':
if len(child):
for value in child:
dict_[name] = xml_type(value.text)
elif child.tag == 'PROPERTY.ARRAY':
if len(child):
assert False, "This case is not dealt with"
else:
assert False, "This case is not dealt with"
return dict_
# convert the xml into a list of dict for each interface
interfaces = [xml_to_dict(x)
for x in xml_root.findall("./RESULTS/CIM/INSTANCE")]
# get only the interfaces which have a mac address
interfaces_with_mac = [
intf for intf in interfaces if intf.get('MACAddress')]
return interfaces_with_mac
Cette fonction renverra une liste de dicts, les informations souhaitées peuvent être renvoyées à partir des dicts résultants :
for intf in get_interfaces_with_mac_addresses('Realtek'):
print intf['Name'], intf['MACAddress']
Aucun commentaire:
Enregistrer un commentaire