从python脚本调用psexec不会显示整个输出 [英] Calling psexec from a python script doesn't show the whole output

查看:455
本文介绍了从python脚本调用psexec不会显示整个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的代码:

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命令获得它:

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?

推荐答案

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

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

快速解决方案将使用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.

引用文档:

args是所有调用所必需的,应为字符串或序列 程序参数.通常提供一个参数序列 首选,因为它允许模块处理所有必需的 转义和引用参数(例如,允许文件中有空格) 名称).如果传递单个字符串,则任何一个外壳程序都必须为True(请参见 下方),否则字符串必须简单地命名要执行的程序 而不指定任何参数.

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.

这篇关于从python脚本调用psexec不会显示整个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆