All,

First, thanks for reading! I'm very new to python, and I'm trying to do something that seemingly feels impossible.

I'm using Python 3.x to write a script. This script, so far, only runs Windows "net use" command and gets back drive mapping info. The problem is that I need to be able to separate the information, but it's reading it as one list item.

Here's the code:

import subprocess
import os
import sys
import re

dc = []
workstations = []


if sys.platform != "win32":
	print ("=== This script only runs on Windows OS systems ===")
	sys.exit(1)
	
scanMsg = '''

Enumerating domain for controllers.....Please wait.....


'''

servers = subprocess.Popen(["net","use"], stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]

dc.append(servers)

print (len(dc))

I'm just checking the length to see if the output I'm getting is split, but there's no way that I can see to split this output.

Here's what I should get:

New connections will be remembered.


Status Local Remote Network

-------------------------------------------------------------------------------
Disconnected Z: \\xxxx\c$ Microsoft Windows Network
The command completed successfully.

But here's what I get... (All I did to get the following is print(dc) instead of len(dc):

['New connections will be remembered.\n\n\nStatus Local Remote Network\n\n-----------------------
--------------------------------------------------------\nDisconnected Z: \\\\xxxx\\c$ Microsoft Window
s Network\nThe command completed successfully.\n\n']

Any ideas? I'm seeing that net use is dumping all of its output into the list, but I can't split it by anything since it only has 1 element (or at least I don't think I can).

So, my end goal is to run "net use", parse the drive information from that, and then perform more actions based off of drive information. I'll need to get the UNC path and drive letter out. Is that possible?

Please let me know if you need more info!

Thanks!

do this.

print("\n".join(x for x in dc))

The dc is a list. You need to run iter it.

Thanks for the response. I'm a little lost I think.

The code (shortened for brevity):

servers = subprocess.Popen(["net","use"], stdout=subprocess.PIPE, universal_newlines=False).communicate()[0]

dc.append(servers)

print ("\n".join(x for x in dc))

In the print line, I get an error unless I change the type dc is. The error that I get is:

Traceback (most recent call last):
File "C:\pythonscripts\nbtenum.py", line 34, in <module>
print ("\n".join(x for x in dc))
TypeError: sequence item 0: expected str instance, bytes found

If I change the print line to:

print ("\n".join(x for x in str(dc)))

I get the list, but it's 1 character per line and scrolls down the screen:

c
o
m
m
a
n
d

c
o
m
p
l
e
t
e
d

s
u
c
c
e
s
s
f
u
l

Thanks

take away the "\n".join

It should be ok.

for x in str(dc):
  print x

All,

Any ideas? I'm seeing that net use is dumping all of its output into the list, but I can't split it by anything since it only has 1 element (or at least I don't think I can).

add a split() method as follows:

servers = subprocess.Popen(["net","use"], stdout=subprocess.PIPE, universal_newlines=True).communicate()[0].split("\n")

servers will reference a list after that, no need to append() to dc.

ckoy,

That fixed my problem! :) Thank you! The next thing I need to find out is how to find the index of a string....I'm going to start a new post for that one.

Thanks again!

The next thing I need to find out is how to find the index of a string....I'm going to start a new post for that one.

Don't do that. Do some reading. Here's the list of methods applicable to strings:
http://docs.python.org/release/2.5.4/lib/string-methods.html
that's from Library Reference of official Python docs. Every standard Python install has it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.