0

I am in try to retrieve Interface Name using python and so far with below code I am able to get data as string. Since I just want to parse Interface name I am using x.split()[] option and then trying to retrieve Interface name which is working fine but will split each word individually in different string. For example if I have long Interface Name it will get split in “Local”, “Area”, “Network” instead once single string as “Local Area Network”.

import subprocess  
p = subprocess.Popen('netsh interface show interface',stdout=subprocess.PIPE) 
[x, err] = p.communicate()
print x.split()

From the experts I want to know if there is a way to retrieve entire interface name from the result.

I also tried converting string result into data set and then grab interface name, but not successful in converting data into data set.

Squashman
  • 13,649
  • 5
  • 27
  • 36
pylearner
  • 5
  • 1
  • 3
  • have you had any chance to try the solution below? – David Zemens Nov 04 '16 at 21:59
  • @DavidZemens yes and I still wanted to go one more level deep. so it works as intended although if I have like 4 Interfaces and I just want to read first one, what would be best way to do that? I tried looking into regex but couldn't find a way to just keep first line and remove rest from results. I'm learning programming and have tried my best before asking. Example: I have below for connections and just want to know first connection name and remove rest from my result. Ethernet1 Ethernet2 Ethernet3 Ethernet4 – pylearner Nov 04 '16 at 22:45

1 Answers1

0

x is a string with \r\n line separators. Each "line" is not delimited in a standard way (tab, for instance) but rather by multiple spaces. There's probably an elegant way to handle this.

The first line is empty, the second line is headers, third line is just a row of -----, so you need to process from line 4 to the end.

import subprocess  
import re
p = subprocess.Popen('netsh interface show interface',stdout=subprocess.PIPE) 
[x, err] = p.communicate()
items = x.split('\r\n')
for itm in items[3:]:
    ## convert this to a delimited line for ease of processing:
    itm = re.sub('\s\s+', '\t', itm)
    print(itm.split('\t')[-1])

Results:

enter image description here

Or further refined:

import subprocess  
import re
p = subprocess.Popen('netsh interface show interface',stdout=subprocess.PIPE) 
[x, err] = p.communicate()
items = x.split('\r\n')
print('\n'.join(map(lambda itm: re.sub('\s\s+', '\t', itm).split('\t')[-1], items[3:])))

Update from Comments

so it works as intended although if I have like 4 Interfaces and I just want to read first one, what would be best way to do that? I tried looking into regex but couldn't find a way to just keep first line and remove rest from results.

You won't need regex for this. The only thing I used regex for was to convert any instance of multiple space character, to a \t (tab) character, so that we can easily parse the lines using split. Otherwise, as you've observed, it will produce outputs like 'Local', 'Area', 'Network'

So the approach I have above assumed you wanted to print out all of the names, and we determined that these items begin on line 4 of the stdout. That's why we did:

for itm in items[3:]:

That line says "iterate over each thing in items, from the third thing until the very last thing." This is a concept known as slicing which can be done on any sequence object (string, tuple, list, etc.)

I have below for connections and just want to know first connection name and remove rest from my result.

So we no longer need iteration (which is done if we need to process each item). We know we only need one item, and it's the first item, so you can revise as:

import subprocess  
import re
p = subprocess.Popen('netsh interface show interface',stdout=subprocess.PIPE) 
[x, err] = p.communicate()
## This gets the line-delimited list from stdout
items = x.split('\r\n')
## Now, we only need the fourth item in that list
itm = items[3]
## convert this to a delimited line for ease of processing:
itm = re.sub('\s\s+', '\t', itm)
## print to console
print(itm.split('\t')[-1])

Or, more succinctly:

import subprocess  
import re
p = subprocess.Popen('netsh interface show interface',stdout=subprocess.PIPE) 
[x, err] = p.communicate()
## This gets the line-delimited list from stdout
items = x.split('\r\n')
""" 
    Now, we only need the fourth item in that list
    convert this to a delimited line for ease of processing, 
    and finally print to console

    This is a one-line statement that does what 3 lines did above
"""
print(re.sub('\s\s+', '\t', items[3]).split('\t')[-1])
Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • it works as intended although if I have like 4 Interfaces and I just want to read first one, what would be best way to do that? I tried looking into regex but couldn't find a way to just keep first line and remove rest from results. I'm learning programming and have tried my best before asking. Example: I have below for connections and just want to know first connection name and remove rest from my result. Ethernet1 Ethernet2 Ethernet3 Ethernet4 – pylearner Nov 04 '16 at 22:42
  • @pylearner OK, I've added some more to the answer, as well as explanation on your follow-ups, however, I would caution you against extended follow-up Q's in the future (it is frowned upon here for a few reasons...). When an answer solves the question you asked, you *should* mark it as "Accepted" or upvote if it's helpful. If you *meant* to ask some slightly more specific question, or you left out (seemingly un)important details, then first you should *try* to adapt the answer, and if that fails, review the dox in light of that answer. If you still can't get it, ask a *new* question :) – David Zemens Nov 05 '16 at 03:13