Parsing the ifconfig command output with a Python array

Source: Internet
Author: User

Sometimes we need to parse the formatted output of the command, parsing the formatting features that usually depend on the command itself, but these features have commonalities: Circular list structures at various levels


For example, the ifconfig command first loops through all of the network interfaces and then, within the network interface, lists the predefined fields in the loop


So for this command parsing, the first thing to do is to organize the output of the list, which is a good idea with a Python (Multidimensional) array.

#encoding =utf-8import subprocesstmp_file = open ('/tmp/g.log ', ' W ') subprocess.call ([' ifconfig '], stdout=tmp_file) tmp _file = open ('/tmp/g.log ', ' r ') ifaces = []ifaces.append ([]) #向数组的尾端添加一个数组! ifaces_cnt = 0line_num = 0for Line_raw in tmp_file: line    = Line_raw.rstrip () #空行的长度为1, strip after length is 0    if Len (line) ; 0:        ifaces[ifaces_cnt].append (line) #向数组的尾端添加一个字符串!    else:        ifaces_cnt = ifaces_cnt +1        ifaces.append ([]) print ' There is%d section '%len (ifaces Ifaces:    print ' ======== ' for L in face    :        print L

To run the output effect:

There is 3 section========eth1      Link encap: Ethernet  Hardware address 00:1c:25:dd:44:8b          inet address: 192.168.0.105  Broadcast : 192.168.0.255  Mask: 255.255.255.0          inet6 Address: fe80::21c:25ff:fedd:448b/64 scope:link up          broadcast RUNNING Multicast  mtu:1500  hop count: 1          receive packet: 569916 Error: 0 Discard: 0 Overload: 0 Number of frames: 0          Send packet: 389975 error: 0 Discard: 0 Overload: 0 Carrier: 0          collisions: 0 Send Queue Length: +          Bytes Received: 651139742 (651.1 mb)  Send bytes: 37622845 (37.6 MB)          Interrupt: 17========lo        link encap: Local loopback          inet Address: 127.0.0.1  mask: 255.0.0.0          Inet6 Address::: 1/128 scope:host up          LOOPBACK  RUNNING mtu:65536  Hops: 1          receive packet: 17094 Error: 0 Discard: 0 Overload: 0 Number of frames: 0          Send packet: 17094 error: 0 Discard: 0 Overload: 0 Carrier: 0          collisions: 0 Send Queue Length: 0          Receive bytes: 1578445 (1.5 mb)  Send bytes: 1578445 (1.5 MB) ========

The extra 1 sections are boundary effects, please ignore

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Parsing the ifconfig command output with a Python array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.