且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python脚本不执行sysinternals命令

更新时间:2023-12-04 23:51:52

正如 lejlot 的评论所建议的,你必须使用\\",因为\"是 python 中的转义字符.

This is a follow-up from Invoke pstools in Python script

When I open a command prompt and execute

D:\pstools\psloggedon.exe -l -x \\10.10.10.10

I get

DOMAIN\user

But when I execute the script

import sys, subprocess, socket, string
import wmi, win32api, win32con



pst = subprocess.Popen(
        ["D:\pstools\psloggedon.exe", "-l", "-x", "\\10.10.10.10"],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )

out, error = pst.communicate()


print out, "is output"

I get

Error opening HKEY_USERS for \10.10.10.10
is output

How do I get the subprocess to read the IP address as \10.10.10.10 instead of \10.10.10.10

By the way, I tried to add third backslash

pst = subprocess.Popen(
        ["D:\pstools\psloggedon.exe", "-l", "-x", "\\\10.10.10.10"],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )

And the output is

Error opening HKEY_USERS for .0.139.40
is output

As suggested by lejlot's comment you have to use "\\" because "\" is an escape character in python.