Invoke pstools in Python script - python

I am trying to invoke pstools (specifically, psloggedon.exe) in my Python 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 pst, "is output"
This is the output
<subprocess.Popen object at 0x0000000002B18D68> is output
I would like the output to be
DOMAIN\user
Thank You

If you want to print the output then you have to use print out, "is output" instead of print pst, "is output" because out will contain the output.

Related

How to RegEx value from terminal output

I am trying to get the time value from a terminal output.
import os
import re
cmd = os.popen('time Open /Applications/TextEdit.app').read()
time = re.search("real [0-9]{1}", cmd)
print(time)
However, it can not find it.
Output
real 0m0.042s
user 0m0.015s
sys 0m0.013s
None
I can not even get the 0. How can I get the 0.042 as my time variable?
The problem is that time is by default printing on stderr and the call to os.popen is saving only the stdout in cmd. Therefore, I would suggest doing the following:
from subprocess import PIPE, Popen
p = Popen("time Open /Applications/TextEdit.app", shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
time = re.search("^real\s+(\S+)$", stderr, re.M).group(1)
Thanks to # Wiktor Stribiżew for the regex
Try like this:
time = re.search("real \d+m\d+.\d+s", cmd)

Problem printing popen stdout from subprocess

i need put the output command to a variable.
I was trying this:
import os
import subprocess
output = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
print (output.stdout)
output.terminate()
but i get
'open file '<fdopen>', mode 'rb' at 0xb76db5a0>'
what is the problem ? It's okay ?
i use python 2.6.6.
output.stdout is a file object. You can use the read method of the file object to get the content of the output:
print(output.stdout.read())
or you can use the Popen.communicate method instead:
stdout, stderr = output.communicate()
print(stdout)

Python script not executing sysinternals command

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.

How to execute a UNIX command in Python script

#!/usr/bin/python
import os
import shutil
import commands
import time
import copy
name = 'test'
echo name
I have a simple python scripts like the above. When I attempt to execute it I get a syntax error when trying to output the name variable.
You cannot use UNIX commands in your Python script as if they were Python code, echo name is causing a syntax error because echo is not a built-in statement or function in Python. Instead, use print name.
To run UNIX commands you will need to create a subprocess that runs the command. The simplest way to do this is using os.system(), but the subprocess module is preferable.
you can also use subprocess module.
import subprocess
proc = subprocess.Popen(['echo', name],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
(out, err) = proc.communicate()
print out
Read: http://www.doughellmann.com/PyMOTW/subprocess/

Subprocess.communicate prints newline to standard output

I have a script that calls ffprobe, parses its output and outputs it to the console.
Here's a stripped-down version of it without the parsing code and command-line options:
"""Invoke ffprobe to query a video file and parse the output"""
def ffprobe(fname):
import subprocess as sub
import re
p = sub.Popen(['ffprobe', fname], stderr=sub.PIPE)
stdout, stderr = p.communicate()
def main():
ffprobe("foo.mp4")
#print options.formatstr % locals()
if __name__ == '__main__':
main()
You can see that the only print statement in my code is commented out, so the program shouldn't really output anything. However, this is what I get:
mpenkov#misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
mpenkov#misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
mpenkov#misha-desktop:~/co/youtube$ python ffprobe.py foo.mp4
A newline is mysteriously output by each invocation. Where is it coming from, and how can I deal with it?
There appears to be a similar SO question, except it's not using the communicate call (http://stackoverflow.com/questions/7985334/python-subprocess-proc-stderr-read-introduce-extra-lines).
I cannot reproduce the problem, so maybe it depends on the file you're passing to ffprobe.
Anyway, from what I see, stdout isn't being captured, so maybe the problem is just that ffprobe is printing a new line character to stdout.
To confirm this, please replace:
p = sub.Popen(['ffprobe', fname], stderr=sub.PIPE)
stdin, stderr = p.communicate()
with:
p = sub.Popen(['ffprobe', fname], stdout=sub.PIPE, stderr=sub.PIPE)
stdout, stderr = p.communicate()
In the new version, stdout is captured and the output from p.communicate is correctly named since it returns stdout not stdin.

Categories