1

I want to use a python script to show up all local administrators in our domain.

My Code :

for line in open(anfangsrechner,"r"):

    zeile = line.strip()

    command ='\\\\' +zeile+ ' -i' ' net' ' localgroup' ' Administratoren'

    abfrage = subprocess.Popen(['PsExec.exe ',command,],stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, )
    # print (abfrage)

    while True:
        line = abfrage.communicate()
        if not line:
            break
        print (line)

But I only get this from the psexec command:

PsExec v2.1 - Execute processes remotely Copyright (C) 2001-2013 Mark
Russinovich Sysinternals - www.sysinternals.com


Process finished with exit code 0

I don't get the whole output. Does someone know how I can fix it?

Mike P
  • 742
  • 11
  • 26
access199
  • 13
  • 1
  • 3
  • What do you get if you run PsExec in a command prompt (not from your Python script?) – Mike P Aug 12 '14 at 09:36
  • when i use the hole psexec in an cmd i get this `PsExec v2.1 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich Sysinternals - www.sysinternals.com Aliasname Administratoren Beschreibung Administratoren haben uneingeschränkten Vollzugriff auf den Com puter bzw. die Domäne. Mitglieder ------------------------------------------------------------------------------- Administrator ISEW2K\Domain Admins` so it works in cmd but not in the script :-/ i need to check more then 1000 Maschines - so an script would be nice – access199 Aug 12 '14 at 09:38
  • modify your script to print the command it builds up to the console; copy the printed command and see if it executes. – Mike P Aug 12 '14 at 09:41
  • This is the command the script builds `Psexec.exe \\w1378.ise.fhg.de -i net localgroup Administratoren` When i use this in command line it executes correctly. Executes in the script -> no output shown – access199 Aug 12 '14 at 09:47

1 Answers1

1

You are passing the arguments as a long string, rather than a list.

The quick fix would be using shell=True:

abfrage = subprocess.Popen('PsExec.exe '+command, 
                           stdout=subprocess.PIPE, 
                           shell=True)

The right way to do this would be creating a list of arguments and passing it.

Quoting the documentation:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • Build a list like this [code]`l = ['\\\\', '-i', 'net', 'localgroup', 'Administratoren', '>>', '\\\\\\software\\it\\Admintest\\.txt']`[/code] `abfrage = subprocess.Popen(['PsExec.exe ',l[0],l[1],l[2],l[3],l[4],l[5],l[6],],stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) print (abfrage) abfrage.communicate()` my output is writen in an external file. The Output is empty .... – access199 Aug 12 '14 at 10:33
  • 1. Try removing `stdin=subprocess.PIPE`, since you're not piping any input to the command. 2. You can build the list using `['PsExec.exe']+l` instead of adding items one by one. – Adam Matan Aug 12 '14 at 12:08
  • My new code line : `abfrage = subprocess.Popen(['PsExec.exe ']+l,stdout=subprocess.PIPE, shell=True)` But my ouput .txt is empty again .... – access199 Aug 12 '14 at 12:25
  • delet -i in the command list get a output but not the whole i got this : `Aliasname Administratoren` Thats first of all okay, but it is useless beacuse their are no Account shown :-/ it looks like that python dont get the output from the subprocess thats executes in the remote workstation – access199 Aug 12 '14 at 12:40
  • Try wrapping your program in a shell script and run that script from Python. Make sure the shebang `#!` at the top is correct. – Adam Matan Aug 12 '14 at 13:54
  • Can you get me any faq or how to's for wrapping this? – access199 Aug 13 '14 at 09:29
  • Just wrap the shell command into a shell script. Search for "Bash script howto", and put your command instead of the one in the example – Adam Matan Aug 13 '14 at 10:27
  • start py script in powershell -> Works perfectly! Thank you Adam! you gave me the right tipp :) realy thanks! – access199 Aug 13 '14 at 12:45
  • Sure, happy to help. Apologies for having `bash` in mind - in Windows, it's obviously powershell. – Adam Matan Aug 13 '14 at 14:06