2

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
Community
  • 1
  • 1
Glowie
  • 2,271
  • 21
  • 60
  • 104
  • 5
    shouldn't there **four** backslashes? You want to have "\\" so you need to escape **both** of them. – lejlot May 15 '14 at 18:48
  • @lejlot --- this solved my problem. Can you click "Answer Question" so I can mark it as the answer – Glowie May 15 '14 at 18:54
  • I suggest using [raw strings](https://docs.python.org/2/reference/lexical_analysis.html#string-literals) in the future e.g. `r"\\\10.10.10.10"`. – Cristian Ciupitu May 15 '14 at 20:05

1 Answers1

1

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

ρss
  • 5,115
  • 8
  • 43
  • 73