源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python实现获取命令行输出结果的方法

  • 时间:2022-11-27 06:49 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python实现获取命令行输出结果的方法
本文实例讲述了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相关内容可查看本站专题:《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/699.htm]Python常用遍历技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/520.htm]Python入门与进阶经典教程[/url]》 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部