有个需求需要将ifcofig输出解析出来,这里将写的整理出来。方便后续使用。

eth0      Link encap:Ethernet  HWaddr 00:50:53:b2:23:e6
inet addr:10.190.179.83 Bcast:10.180.255.255 Mask:255.255.0.0
inet6 addr: fe80::250:56ff:feb2:33e5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:28559410 errors:0 dropped:48189 overruns:0 frame:0
TX packets:2392292 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5488032566 (5.4 GB) TX bytes:3079384640 (3.0 GB)

示例代码

import commands

def parse_ifconfig_data(data):
"""
parse ifconfig data into ["eth0 ****" ,"eth1 ***", "lo ***"]
:param data: ifconfig output data
:return: a list like ["eth0 ****" ,"eth1 ***", "lo ***"]
"""
parsed_data = []
new_line = ''
data = [i for i in data.split('\n') if i] for line in data:
if not line.startswith(' '):
parsed_data.append(new_line)
new_line = line + '\n'
else:
new_line += line + '\n'
parsed_data.append(new_line)
return parsed_data[1:] def parse_one_interface(one_data):
result = {}
line_list = data.split('\n')
result["device_name"] = line_list[0].split()[0]
result["HWaddr"] = line_list[0].split()[-1]
result["inet_addr"] = line_list[1].split()[1].split(':')[1]
result["Mask"] = line_list[1].split(':')[-1]
result["MTU"] = line_list[3].split()[-2].split(':')[-1]
return result (ret, if_info) = commands.getstatusoutput("sudo ifconfig -a")
info = parse_ifconfig_data(if_info)
data = info[0]
print parse_one_interface(data)

示例输出

{'HWaddr': '00:50:53:b2:23:e6', 'MTU': '1500', 'Mask': '255.255.0.0', 'inet_addr': '10.190.179.83', 'device_name': 'eth0'}

python解析ifconfig 输出成字典的更多相关文章

  1. python将字符串转换成字典的几种方法

    当我们遇到类似于{‘a’:1, 'b':2, 'c':3}这种字符串时,想要把它转换成字典进行处理,可以使用以下几种方法: 1. Python自带的eval函数(不安全) dictstr = '{&q ...

  2. python 将字符串转换成字典dict的各种方式总结

    1)利用eval可以将字典格式的字符串与字典户转 >>>mstr = '{"name":"yct","age":10}' ...

  3. python 将excel转换成字典,并且将字典写到txt文件里

    # -*- coding: utf-8 -*- #python2.7 import sys reload(sys) sys.setdefaultencoding('utf-8') from pyexc ...

  4. python 将字符串转换成字典dict

    JSON到字典转化:>>>dictinfo = json.loads(json_str) 输出dict类型 字典到JSON转化:>>>jsoninfo = json ...

  5. python读取excel数据转换成字典

    以上面的excel格式,输出字典类型: import xlrddef read_excel_data(): filename = 'E:\学历列表.xls' data = xlrd.open_work ...

  6. python将str转换成字典

    典型的应用场景:Json数据的解析 >>> user "{'name' : 'jim', 'sex' : 'male', 'age': 18}" >> ...

  7. Python将字符串转换成字典

    1. ast包 import ast user_info = '{"name" : "南湖", "gender" : "male& ...

  8. Python+Selenium进行UI自动化测试项目中,常用的小技巧1:读取excel表,转化成字典(dict)输出

    从今天开始我将会把在项目中遇到的问题,以及常用的一些技巧来分享出来,以此来促进自己的学习和提升自己:更加方便我以后的查阅. 现在要说的是:用Python来读取excel表的数据,返回字典(dict), ...

  9. 第4.4节 Python解析与推导:列表解析、字典解析、集合解析

    一.    引言 经过前几个章节的介绍,终于把与列表解析的前置内容介绍完了,本节老猿将列表解析.字典解析.集合解析进行统一的介绍. 前面章节老猿好几次说到了要介绍列表解析,但老猿认为涉及知识层面比较多 ...

随机推荐

  1. python线程threading.Timer源码解读

    threading.Timer的作用 官方给的定义是: """Call a function after a specified number of seconds: t ...

  2. Elasticsearch系列---Elasticsearch的基本概念及工作原理

    基本概念 Elasticsearch有几个核心的概念,花几分钟时间了解一下,有助于后面章节的学习. NRT Near Realtime,近实时,有两个层面的含义,一是从写入一条数据到这条数据可以被搜索 ...

  3. nginx recv() failed (104: Connection reset by peer) while reading response header from upstream解决方法

    首先说下 先看 按照ab 每秒请求的结果 看看 都有每秒能请求几个 如果并发量超出你请求的个数 会这样 所以一般图片和代码服务器最好分开 还有看看io瓶ding 和有没有延迟的PHP代码执行 0 先修 ...

  4. day7-集合

    一.定义变量是为了吹处理状态的变化,定义变量名是为了获取变量值.字符串.数字.列表.元组.字典都是为了更好的描述变量的状态1.可变不可变:变量名不变时,里面内容是否可以变化# 可变:列表.字典.修改变 ...

  5. docker搭建本地registry

    第一步:拉取registry镜像 [root@localhost iso]# docker image pull registry Using default tag: latest latest: ...

  6. JavaScrip 基础

    JavaScript 基础 前段的三剑客之一JS,来来来,看看它是什么鬼!到底如何让网页动起来的呢,今天就搞他一下. 一.JavaScript的简单介绍 javascript是一门动态弱类型的解释型编 ...

  7. pat 1077 Kuchiguse(20 分) (字典树)

    1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  8. pat 1046 Shortest Distance(20 分) (线段树)

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  9. 领扣(LeetCode)数字转换为十六进制数 个人题解

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果要转化的数 ...

  10. centos 更换用户密码

    腾讯云报告了我的服务器被暴力破解了.... 因此需要更换更复杂的password, 命令为:passwd  用户名,例如下我要更换root的password [root@VM_0_4_centos ~ ...