1

i would like to fetch only the lines from windows net use cmd wich are relevant for me.

if UNC path is to long net use does a line break

my code:

output = subprocess.Popen('net use', stdout=subprocess.PIPE).communicate() valid_lines = [ line.strip() for line in output[0].split('\r\n')] valid_lines = valid_lines[6:-3] print "output", valid_lines

Sample net use: enter image description here

output ['Getrennt \\192.168.1.111\bze\export', 'Microsoft Windows Network', 'OK \\master\bze\export Microsoft Windows Network']

i would like to have the output in from first one line, like on 'OK.....'

thx

manga
  • 105
  • 1
  • 8
  • You've described your situation fairly well. I understand that you want to write a program that merges the "Getrennt" line with the following line. Nevertheless, your post is missing a key ingredient: the question. Do you have a specific question? – Robᵩ Aug 29 '14 at 19:52
  • 1
    Consider querying the mapped network connections directly through code (`WNetOpenEnum`/`WNetEnumResource`), instead of dealing with parsing the text output of the `net` command. – nobody Aug 29 '14 at 19:53

2 Answers2

0

As Andrew Medico points out, there are APIs for this, and if possible, it's much better to call a function that's designed to return information to a program than to try to parse input which has been formatted for a human (because the latter could be overly complicated—as you're seeing here—or even ambiguous).

But this is doable. You just need to work out what the rule is for line continuation.

It looks like the rule is pretty simple: if the first column is empty, the line is a continuation for the previous line.

If so:

prev_line = ''
for line in valid_lines:
    if line[0]:
        if prev_line:
            print prev_line
        prev_line = ''
    else:
        prev_line += line

You can make this more concise, and more readable to many, with itertools.groupby, at the risk of making it incomprehensible to novices, so I went with the explicit loop.

You could also cheat by abusing a parser for a protocol that has a compatible continuation-line format; e.g., the rfc822 module. But ultimately that's probably doing more work rather than less.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 3
    @abarnett, first column empty doesn't imply a line continuation. I ran this command on my PC and the status column is empty for several entries. One thing I noticed, at least in my case, is the remote column and network column are always populated. – user3885927 Aug 29 '14 at 20:08
  • @user3885927: Helpful information, thanks. But whatever the rule is, once you figure it out, the code is easy to write. If the OP wants someone else to reverse engineer the grammar for the output of `net use`, that's not really an SO question, much less a Python programming question… – abarnert Aug 29 '14 at 20:16
0

This is a good question and I found a better and much reliable way to do this provided your OS supports WMIC command. I beleive from windows XP onwards this is supported but you can check for your specific system. Using WMIC you can format your output as csv and you get fully reliable info. Sample code is below:

import subprocess
output = subprocess.Popen('wmic netuse list /format:csv', stdout=subprocess.PIPE).communicate()
valid_lines = [ line.strip() for line in output[0].split('\r\n')]
#If you don't want the header use 2: instead of 1:
myData=[line.split(',') for line in valid_lines][1:]
user3885927
  • 3,363
  • 2
  • 22
  • 42