在Python中捕获PSexec输出

时间:2015-03-19 20:39:17

标签: python python-3.x subprocess psexec

我一直致力于通过Python 3.4.2使用PsExec在数百个远程系统上执行命令。我一直在以可解析的方式收集输出时遇到问题。我使用subprocess.Popenos.system尝试了几种变体,但似乎都没有完全符合我的需要。

如上所述,我需要在几百个Windows系统上运行命令。我需要运行并抛出参数的可执行文件可能存在于几个可预测的位置之一。我决定从this SO帖子中获取灵感,并基本上使用嵌套的if/else语句通过PsExec运行命令,收集输出PsExec / Windows输出,并解析输出以确定成功或失败({{1} })。如果命令失败,那么我将运行下一个命令变体。我知道这是不好的做法,我当然愿意接受替代方案。

这是我的老鼠的巢代码块:

if "The system cannot find the path specified." in output:

这种方法起初看起来很有希望,但是在PsExec执行后它正在为我提供支持。以下是输出。

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)

有一次我得到它实际输出的东西,但由于输出是字节码,它不能解析为字符串。当时,我尝试做PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich Sysinternals - www.sysinternals.com 这对我没有任何帮助。

以下是我在迄今为止帮助的主题上发现的一些互联网研究:

Python Popen hanging with psexec - undesired results

Calling psexec from a python script doesn't show the whole output

Why does Popen.communicate() return b'hi\n' instead of 'hi'?

...最后this one在Python中没有完全实现,但似乎对我不起作用。

**编辑:为“主持人”添加支线队列功能

.decode(output)

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

  1. 为什么PsExec在初次执行后会挂起?
  2. 如何在Python中捕获所有内容(包括PsExec输出)?
  3. 我有更好的方法可以执行多个嵌套的If / Else语句吗?
  4. 提前致谢!

0 个答案:

没有答案