从python脚本调用psexec不会显示整个输出

时间:2014-08-12 09:34:12

标签: python output psexec

我想使用python脚本显示我们域中的所有本地管理员。

我的代码:

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)

但我只能从psexec命令得到这个:

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


Process finished with exit code 0

我没有得到整个输出。有人知道如何解决它吗?

1 个答案:

答案 0 :(得分:1)

您将参数作为长字符串传递,而不是列表。

快速修复将使用shell=True

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

正确的方法是创建一个参数列表并传递它。

引用the documentation

  

所有调用都需要args,并且应该是字符串或序列   程序参数。通常提供一系列论证   首选,因为它允许模块处理任何所需的   转义和引用参数(例如,允许文件中的空格   名)。如果传递单个字符串,则任何一个shell都必须为True(请参阅   或者字符串必须简单地命名要执行的程序   没有指定任何参数。