在Python中捕获PSexec输出

在Python中捕获PSexec输出,python,python-3.x,subprocess,psexec,Python,Python 3.x,Subprocess,Psexec,我一直致力于通过Python3.4.2使用PsExec在数百个远程系统上执行命令。我一直在以可分析的方式收集输出时遇到问题。我使用subprocess.Popen和os.system尝试了几种变体,但这两种变体似乎都不能完全满足我的需要 如上所述,我需要在几百个Windows系统上运行一个命令。我需要运行并抛出参数反对的可执行文件可能存在于几个可预测的位置之一。我决定从SO post中汲取灵感,基本上使用嵌套的if/else语句通过PsExec运行命令,收集输出PsExec/Windows输出,

我一直致力于通过Python3.4.2使用PsExec在数百个远程系统上执行命令。我一直在以可分析的方式收集输出时遇到问题。我使用
subprocess.Popen
os.system
尝试了几种变体,但这两种变体似乎都不能完全满足我的需要

如上所述,我需要在几百个Windows系统上运行一个命令。我需要运行并抛出参数反对的可执行文件可能存在于几个可预测的位置之一。我决定从SO post中汲取灵感,基本上使用嵌套的
if/else
语句通过PsExec运行命令,收集输出PsExec/Windows输出,并解析输出以确定成功或失败(
如果“系统找不到指定的路径”。在输出中:
)。如果命令失败,那么我将运行下一个命令变体。我知道这是不好的做法,我当然愿意接受其他选择

这是我的老鼠窝代码块:

def f_remove_ma():
    host = f_get_queue()
    g_psexec_location = "PsExec.exe"

    # Legacy 32-bit:
    ma_install_location = "<install location 1>"
    ma_arguments = "/arguments"
    output = subprocess.Popen([g_psexec_location, "\\\\" + host, "-accepteula", ma_install_location, ma_arguments], stdout=subprocess.PIPE).communicate()[0]

    if 'could not start' in output:
        print("Install not found in location 1")
        return
        # Standard 64-bit:
        ma_install_location = "<install location 2>"
        ma_arguments = "/arguments"
        output = subprocess.Popen([g_psexec_location, "\\\\" + host, "-accepteula", ma_install_location, ma_arguments], stdout=subprocess.PIPE).communicate()[0]

        if "The system cannot find the path specified." in output:
            # Standard 32-bit:
            ma_install_location = "<install location 3>"
            ma_arguments = "/arguments"
            output = subprocess.Popen([g_psexec_location, "\\\\" + host, "-accepteula", ma_install_location, ma_arguments], stdout=subprocess.PIPE).communicate()[0]

            if "The system cannot find the path specified." in output:
                # New 32/64-bit:
                ma_install_location = "<install location 4>"
                ma_arguments = "/arguments"
                output = subprocess.Popen([g_psexec_location, "\\\\" + host, "-accepteula", ma_install_location, ma_arguments], stdout=subprocess.PIPE).communicate()[0]

                if "The system cannot find the path specified." in output:
                    # New 32/64-bit:
                    ma_install_location = "<install location 5>"
                    ma_arguments = "/arguments"
                    output = subprocess.Popen([g_psexec_location, "\\\\" + host, "-accepteula", ma_install_location, ma_arguments], stdout=subprocess.PIPE).communicate()[0]

                    if "The system cannot find the path specified." in output:
                        print("No known versions of software found.")
                    else:
                        print(output)
                else:
                    print(output)
            else:
                print(output)
        else:
            print(output)
    else:
        print(output)
有一次,我让它实际输出一些东西,但由于输出是字节码,所以不能作为字符串进行分析。当时,我试着做
.decode(output)
,但没有任何帮助

以下是我在这个主题上找到的一些互联网研究,这些研究到目前为止都很有帮助:

。。。最后,它没有完全用Python实现,而且对我来说似乎也不起作用

**编辑:为“主机”添加馈线队列功能


所有这些,我有几个问题:

  • 为什么PsExec在初始执行后挂起
  • 如何在Python中捕获所有内容(包括PsExec输出)
  • 有没有更好的方法可以执行多个嵌套的If/Else语句

  • 提前谢谢

    听起来你需要一份工作。。第一个电话挂断了吗?如果您只想捕获output@PadraicCunningham-是的,它在第一次呼叫时会挂起。您是否收到任何输出?你也试过stderr看看有没有什么收获?为什么要嵌套所有的ifs?我也会将所有的
    放在一个列表中,并对其进行迭代,而不是多个ifs
    PsExec v2.11 - Execute processes remotely
    Copyright (C) 2001-2014 Mark Russinovich
    Sysinternals - www.sysinternals.com
    
    def f_create_queue(ip_list):
        global g_queue
        g_queue
        for ip in ip_list:
            g_queue.put(ip)
        print("\nQueue has {} items ready for processing\n".format(g_queue.qsize()))
    
    
    def f_get_queue():
        global g_queue
        return g_queue.get()