Home > Mobile >  How to extract a single word from an os.system command?
How to extract a single word from an os.system command?

Time:01-04

I'm writing a python program where I do some os.system(cmd). I would need to extract one single word from the terminal output. The output contains a seire of informations. Inside this information, I only need the parameter address, as a simple string. How could I do? This is an example of the ouput:

  --------------------------------
  General            |  dbus path: /org/freedesktop/ModemManager1/Bearer/1
                     |       type: default
  --------------------------------
  Status             |  connected: yes
                     |  suspended: no
                     |  interface: wwp0s20f0u3i2
                     | ip timeout: 20
  --------------------------------
  Properties         |        apn: wap.tim.it
                     |    roaming: allowed
  --------------------------------
  IPv4 configuration |     method: static
                     |    address: 10.200.210.208
                     |     prefix: 27
                     |    gateway: 10.200.210.209
                     |        dns: 217.200.201.65, 217.200.201.64
                     |        mtu: 1500
  --------------------------------
  Statistics         |   duration: 1290

I've done:

  proc=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, )
  output=proc.communicate()[0]
  print(output)

but obviously, it returns the entire output. Also including a grep inside the command, isn't a good solution. The output is bad formatted. I would need:

10.200.210.208

CodePudding user response:

You might regular expression for that task following way

import re
output = '''  --------------------------------
  General            |  dbus path: /org/freedesktop/ModemManager1/Bearer/1
                     |       type: default
  --------------------------------
  Status             |  connected: yes
                     |  suspended: no
                     |  interface: wwp0s20f0u3i2
                     | ip timeout: 20
  --------------------------------
  Properties         |        apn: wap.tim.it
                     |    roaming: allowed
  --------------------------------
  IPv4 configuration |     method: static
                     |    address: 10.200.210.208
                     |     prefix: 27
                     |    gateway: 10.200.210.209
                     |        dns: 217.200.201.65, 217.200.201.64
                     |        mtu: 1500
  --------------------------------
  Statistics         |   duration: 1290'''
address = re.search("address: ([0-9] [.][0-9] [.][0-9] [.][0-9] )",output).group(1)
print(address) # 10.200.210.208

Explanation: I use pattern with single capturing group, encased in ( ) which I then access using .group(1)

Disclaimer: I assume address line is always present and address is in form of 4 base-10 numbers sheared by . character.

Note: for brevity I set output to multiline string, rather than calling command.

CodePudding user response:

Python has elagent & simple text processing methods which can used to extract the data like so using string.find

s = output
start = 'address'
end = 'prefix'
mask =  (s[s.find(start) len(start):s.rfind(end)])
print(mask)

outputs #

: 10.200.210.208

Later if you want you clean output like so

mask = mask.replace(":",'').replace('|','')
print(mask)

which outputs #

10.200.210.208
  • Related