Python实现获取命令行输出结果的方法
作者:JoeBlackzqq 时间:2023-04-23 09:34:13
本文实例讲述了Python实现获取命令行输出结果的方法。分享给大家供大家参考,具体如下:
Python获取命令行输出结果,并对结果进行过滤找到自己需要的!
这里以获取本机MAC地址和IP地址为例!
# coding: GB2312
import os, re
# execute command, and return the output
def execCmd(cmd):
r = os.popen(cmd)
text = r.read()
r.close()
return text
# write "data" to file-filename
def writeFile(filename, data):
f = open(filename, "w")
f.write(data)
f.close()
# 获取计算机MAC地址和IP地址
if __name__ == '__main__':
cmd = "ipconfig /all"
result = execCmd(cmd)
pat1 = "Physical Address[\. ]+: ([\w-]+)"
pat2 = "IP Address[\. ]+: ([\.\d]+)"
MAC = re.findall(pat1, result)[0] # 找到MAC
IP = re.findall(pat2, result)[0] # 找到IP
print("MAC=%s, IP=%s" %(MAC, IP))
运行结果:
E:\Program\Python>del.py
MAC=00-1B-77-CD-62-2B, IP=192.168.1.110
E:\Program\Python>
希望本文所述对大家Python程序设计有所帮助。
标签:Python,命令行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python标准日志模块logging的使用方法
2022-10-29 19:08:16
![](https://img.aspxhome.com/file/2023/6/81546_0s.jpg)
在ubuntu16.04中将python3设置为默认的命令写法
2022-06-21 10:12:41
![](https://img.aspxhome.com/file/2023/9/111959_0s.png)
Pandas 连接合并函数merge()详解
2021-10-28 05:13:44
判断Threading.start新线程是否执行完毕的实例
2023-06-18 16:43:25
![](https://img.aspxhome.com/file/2023/3/68383_0s.jpg)
JS代码格式化和语法着色V2
2023-07-02 05:18:27
mysql 重启方法(初学者)
2010-12-03 16:40:00
ASP.NET教程第一讲 ASP.NET简介
2007-08-07 11:50:00
Python如何通过手肘法实现k_means聚类详解
2021-03-13 16:35:34
![](https://img.aspxhome.com/file/2023/4/127724_0s.png)
pytorch获取模型某一层参数名及参数值方式
2022-07-03 06:10:28
go语言代码生成器code generator使用示例介绍
2024-05-21 10:19:29
![](https://img.aspxhome.com/file/2023/0/125070_0s.png)
mysql8.0主从复制搭建与配置方案
2024-01-15 11:26:25
![](https://img.aspxhome.com/file/2023/5/89925_0s.png)
python 获取计算机的网卡信息
2023-08-11 21:12:06
在Pycharm中修改文件默认打开方式的方法
2023-03-12 06:34:23
![](https://img.aspxhome.com/file/2023/0/72130_0s.jpg)
SQL Server数据库对服务器的需求
2010-06-11 13:52:00
Go语言正则表达式示例
2023-04-13 19:41:34
表格梳理解析python内置时间模块看完就懂
2023-10-21 08:10:27
Python中使用asyncio 封装文件读写
2022-11-13 03:18:12
Python异常处理如何才能写得优雅(retrying模块)
2023-07-13 05:50:44
Golang实现http重定向https
2024-04-26 17:27:57
Python3 如何开启自带http服务
2023-07-27 14:33:46
![](https://img.aspxhome.com/file/2023/1/63911_0s.jpg)