Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command output parsing in Python

I am trying to write a Python script on Windows 7, which reads the output of command ipconfig /displaydns and try to get some values from the output.

The output of ipconfig /displaydns"is something like this,

Windows IP Configuration

9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.

Record Name . . . . . : 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.
Record Type . . . . . : 1
Time To Live  . . . . : 294
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 127.0.0.16

I am taking this output and saving it in a variable as below,

output = subprocess.check_output("ipconfig /displaydns", shell=True)

When I print "output" I get the following

b'\r\nWindows IP Configuration\r\n\r\n   9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.\r\n    ----------------------------------------\r\n    Record Name . . . . . : 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.\r\n    Record Type . . . . . : 1\r\n    Time To Live  . . . . : 289\r\n    Data Length . . . . . : 4\r\n    Section . . . . . . . : Answer\r\n    A (Host) Record . . . : 127.0 .0.16\r\n\r\n\r\n'

From this output I am interested in the values for A (Host) Record and Record Name which are 127.0.0.16 and 9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts. respectively.

How would I do it in Python?

like image 846
user2253876 Avatar asked Apr 23 '13 17:04

user2253876


People also ask

What is the command for output in Python?

In Python 3. x, you can output without a newline by passing end="" to the print function or by using the method write: import sys print("Hello", end="") sys. stdout.

What is parsing in Python?

In this article, parsing is defined as the processing of a piece of python program and converting these codes into machine language. In general, we can say parse is a command for dividing the given program code into a small piece of code for analyzing the correct syntax.

Is Python good for parsing?

Parsing text using string methodsPython is incredible when it comes to dealing with strings. It is worth internalising all the common string operations. We can use these methods to extract data from a string as you can see in the simple example below.


1 Answers

import subprocess
output = subprocess.check_output("ipconfig /displaydns", shell=True)
result = {}
for row in output.split('\n'):
    if ': ' in row:
        key, value = row.split(': ')
        result[key.strip(' .')] = value.strip()

print(result)
print(result['A (Host) Record'])

gives:

{'A (Host) Record': '127.0 .0.16', 'Data Length': '4', 'Section': 'Answer', 'Record Name': '9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.', 'Time To Live': '289', 'Record Type': '1'}
127.0 .0.16

Another solution would be to: (when i thought of this in my head, i thought it would be more compact.. it wasn't but anyway, it's a different way of calling the external command where you get control of the errors and output (you can differntiate the two))

import subprocess
cmdpipe = subprocess.Popen("ipconfig /displaydns", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result = {}
for row in cmdpipe.stdout.readline():
    if ': ' in row:
        key, value = row.split(': ')
        result[key.strip(' .')] = value.strip()

# You need to close the file handles, as they will stay open "forever" otherwise.
cmdpipe.stdout.close()
cmdpipe.stderr.close()

print(result)
print(result['A (Host) Record'])

I'll also add that using shell=True might be dangerous especially when combined with user-input. It does add some "hidden magic" to make some things easier or more user-natural. But in most cases you will want to do subprocess.Popen(["ipconfig", "/displaydns"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) instead.

like image 133
Torxed Avatar answered Sep 21 '22 07:09

Torxed