简体   繁体   English

如何解析getmac命令的output?

[英]How do I parse the output of getmac command?

After doing some research I found out that the best way to get the Ethe.net MAC address under windows is the "getmac" command.在做了一些研究之后,我发现获取 windows 下的 Ethe.net MAC 地址的最佳方法是“getmac”命令。 (getmac module of python does not produce the same..): Now I want to use this command from within a python code to get the MAC address. (python 的 getmac 模块不产生相同的..):现在我想从 python 代码中使用这个命令来获取 MAC 地址。 I figured out that my code should start something like this:我发现我的代码应该像这样开始:

import os
if sys.platform == 'win32':
    os.system("getmac")
    do something here to get the first mac address that appears in the results

here is an example output这是一个例子 output

Physical Address    Transport Name                                            
=================== ==========================================================  
1C-69-7A-3A-E3-40   Media disconnected                                        
54-8D-5A-CE-21-1A   \Device\Tcpip_{82B01094-C274-418F-AB0A-BC4F3660D6B4}      

I finally want to get 1C-69-7A-3A-E3-40 preferably without the dashes.我终于想要 1C-69-7A-3A-E3-40 最好没有破折号。 Thanks in advance.提前致谢。

Two things.两件事情。 First of all, I recommend you find ways of getting the mac address more elegantly.首先,我建议您找到更优雅地获取 mac 地址的方法。 This question's answer seems to use the uuid module , which is perhaps a good cross-platform solution. This question's answer seems to use the uuid module ,这也许是一个很好的跨平台解决方案。

Having said that , if you want to proceed with parsing the output of a system call, I recommend the use of Python's subprocess module .话虽如此,如果你想继续解析系统调用的 output,我推荐使用 Python 的subprocess 模块 For example:例如:

import subprocess

output_of_command = subprocess.check_output("getmac")

This will run getmac and the output of that command will go into a variable.这将运行getmac ,该命令的 output 将 go 转换为一个变量。 From there, you can parse the string.从那里,您可以解析字符串。

Here's how you might extract the mac address from that string:以下是从该字符串中提取 mac 地址的方法:

# I'm setting this directly to provide a clear example of the parsing, separate
# from the first part of this answer.

my_string = """Physical Address Transport Name
=================== ==========================================================
1C-69-7A-3A-E3-40 Media disconnected
54-8D-5A-CE-21-1A \Device\Tcpip_{82B01094-C274-418F-AB0A-BC4F3660D6B4}"""

my_mac_address = my_string.rsplit('=', 1)[-1].split(None, 1)[0]

The first split is a right split .第一次分裂是右分裂 It's breaking up the string by the '=' character, once, starting from the end of the string.它从字符串的末尾开始一次按“=”字符拆分字符串。 Then, I'm splitting the output of that by whitespace, limiting to one split, and taking the first string value.然后,我将其中的 output 按空格拆分,限制为一次拆分,并获取第一个字符串值。

Again, however, I would discourage this approach to getting a mac address.但是,我再次反对这种获取 mac 地址的方法。 Parsing the human-readable output of command line scripts is seldom advisable because the output can unexpectedly be different than what your script is expecting.很少建议解析人类可读的命令行脚本 output,因为 output 可能会意外地与您的脚本所期望的不同。 You can assuredly get the mac address in a more robust way.您肯定可以以更可靠的方式获取 mac 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM