python获取命令行输出结果
2012-03-17 11:59
549 查看
python获取命令行输出结果,并对结果进行过滤找到自己需要的!
这里以获取本机MAC地址和IP地址为例!
运行结果:
这里以获取本机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获取命令行输出结果
- Python实现获取命令行输出结果的方法
- Python脚本获取Windows窗口标题,输出到命令行
- 用Python获取命令行输出
- [原创]Python 运行 shell 获取输出结果
- C# 编写命令行交互工具——实时输出_获取执行结果
- Python 获取命令行输出
- python如何获取命令行输出?
- 用Python获取命令行输出 将屏幕输出的log保存文件中
- Python获取命令实时输出-原样彩色输出并返回输出结果
- LINUX C获取命令行输出结果
- Python执行命令行命令并获取输出
- 【转】C#执行命令行程序/命令并获取输出结果
- C语言与python获取命令行输出
- C# 编写命令行交互工具——实时输出_获取执行结果
- Python读取UTF-8编码文件并使用命令行执行时输出结果的问题
- Python脚本获取Windows窗口标题,输出到命令行
- 直接从命令行获取MySQL查询语句结果
- 利用Python分析GP服务运行结果的输出路径 & 实现服务输出路径的本地化 分类: Python ArcGIS for desktop ArcGIS for server 2015-08-06 19:49 3人阅读 评论(0) 收藏