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

2024-10-01 11:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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命令得到:

^{pr2}$

我没有得到全部输出。有人知道我该怎么修吗?在


Tags: 代码in脚本for管理员lineopencommand
1条回答
网友
1楼 · 发布于 2024-10-01 11:29:56

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

快速修复将使用shell=True

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

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

引用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.

相关问题 更多 >