在Windows中使用python获取ipconfig结果 [英] Get ipconfig result with python in windows

查看:163
本文介绍了在Windows中使用python获取ipconfig结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,只是在学习python.我需要帮助以使用python在Windows中获取我的网卡的正确MAC地址.我尝试搜索,发现了这些:

I am new here and just learning python. I need help to get the right mac-address of my network card in windows using python. I tried to search, and found these :

  1. Python-获取mac地址

获取MAC地址

Python中的命令输出解析

解析Windows的"ipconfig/all"输出

如果我在命令提示符下运行"ipconfig/all",则会得到以下提示:

If I run "ipconfig /all" from command prompt, I get this :

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

我需要获取Realtek网卡的mac地址( 32-A5-2C-0B-14-D9 ),而不是Norton或Windows隧道创建的MAC地址. 如果我正在使用Python,我给了另一个Mac地址结果: "uuid.getnode() or "getmac" 我认为最好的方法是获取 "ipconfig /all", 在"Beschreibung"中查看"Realtek",然后获取"Physische Adresse"信息,以获取我的真实Mac地址. 如何在Windows上的python中执行此操作?任何帮助表示赞赏.预先感谢.

I need to get the mac address of my Realtek network card (32-A5-2C-0B-14-D9), not the one created by Norton or windows tunneling. Python gave me another result of mac address if i am using : "uuid.getnode() or "getmac" I think the best way is to get the output of "ipconfig /all", looking at "Realtek" at "Beschreibung" and then get the "Physische Adresse" information to get my real mac address. How to do this in python on windows ? Any help is appreciated. Thanks in advance.

推荐答案

以下python3脚本基于Stephen Rauch脚本(感谢wmic实用程序指针,它非常方便)

the python3 script below is based on the Stephen Rauch one (thanks for the wmic utility pointer it's really handy)

它仅从计算机中检索 IP 活动接口, 处理具有多个值的字段(一个NIC上的多个ips/掩码或网关),创建 IPv4Iinterface或ip/mask中的v6 python对象,并输出每个nic包含一个字典的列表.

it retrieves only the IP and active interfaces from the computer, handles fields with multiple values (several ips/masks or gateways on one nic), creates IPv4Iinterface or v6 python objects from ip/mask, and ouputs a list with one dict per nic.

#python3
from subprocess import check_output
from xml.etree.ElementTree import fromstring
from ipaddress import IPv4Interface, IPv6Interface

def getNics() :

    cmd = 'wmic.exe nicconfig where "IPEnabled  = True" get ipaddress,MACAddress,IPSubnet,DNSHostName,Caption,DefaultIPGateway /format:rawxml'
    xml_text = check_output(cmd, creationflags=8)
    xml_root = fromstring(xml_text)

    nics = []
    keyslookup = {
        'DNSHostName' : 'hostname',
        'IPAddress' : 'ip',
        'IPSubnet' : '_mask',
        'Caption' : 'hardware',
        'MACAddress' : 'mac',
        'DefaultIPGateway' : 'gateway',
    }

    for nic in xml_root.findall("./RESULTS/CIM/INSTANCE") :
        # parse and store nic info
        n = {
            'hostname':'',
            'ip':[],
            '_mask':[],
            'hardware':'',
            'mac':'',
            'gateway':[],
        }
        for prop in nic :
            name = keyslookup[prop.attrib['NAME']]
            if prop.tag == 'PROPERTY':
                if len(prop):
                    for v in prop:
                        n[name] = v.text
            elif prop.tag == 'PROPERTY.ARRAY':
                for v in prop.findall("./VALUE.ARRAY/VALUE") :
                    n[name].append(v.text)
        nics.append(n)

        # creates python ipaddress objects from ips and masks
        for i in range(len(n['ip'])) :
            arg = '%s/%s'%(n['ip'][i],n['_mask'][i])
            if ':' in n['ip'][i] : n['ip'][i] = IPv6Interface(arg)
            else : n['ip'][i] = IPv4Interface(arg)
        del n['_mask']

    return nics

if __name__ == '__main__':
    nics = getNics()
    for nic in nics :
        for k,v in nic.items() :
            print('%s : %s'%(k,v))
        print()

导入它或在cmd提示符下使用它:

import it or use it from a cmd prompt :

python.exe getnics.py

将输出类似:

hardware : [00000000] Intel(R) Centrino(R) Wireless-N 2230 Driver
gateway : ['192.168.0.254']
ip : [IPv4Interface('192.168.0.40/24'), IPv6Interface('fe80::7403:9e12:f7db:60c/64')]
mac : xx:xx:xx:xx:xx:xx
hostname : mixer

hardware : [00000002] Killer E2200 Gigabit Ethernet Controller
gateway : ['192.168.0.254']
ip : [IPv4Interface('192.168.0.28/24')]
mac : xx:xx:xx:xx:xx:xx
hostname : mixer

在Windows10上进行了测试. 我对mac地址字段有一些疑问,例如对于VM或欺骗案例,似乎wmic只返回一个字符串,而不返回数组.

tested with windows10. I have some doubts about the mac adress field, with VM or spoofing cases for example, it seems wmic returns one string only, and not an array.

这篇关于在Windows中使用python获取ipconfig结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆