0
import subprocess,time

psexecloc ="C:\\Users\\administrator\\Desktop\\sample\\bin\\psexec.exe"
remotecmd="notepad.exe"
username = "XXXr"
password = "XXXXXXX"
remotehostname = "XXXXXXXX"
args = 
[psexecloc, "-u", username, "-p", password, remotehostname,   remotecmd]

output = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
time.sleep(30)
outstr=output.communicate()
stdoutstr = outstr[0]
stderrstr = outstr[1]
print ("Output Tupple: ",outstr)

Just a cmd will popup for a ms, facing issue in this code please help

6
  • I think you cannot execute graphical applications using psexec. Have you tried cmd /c dir for instance? Commented Feb 21, 2017 at 14:07
  • 1
    @Jean-FrançoisFabre You can execute GUI with psexec. And does your remotehostname contains a `\\` (double backslash) as a prefix? it should Commented Feb 21, 2017 at 14:09
  • psexec \\ps-name notepad.exe Commented Feb 21, 2017 at 14:09
  • In place of remotehostname, I am providing system IP. Commented Feb 21, 2017 at 14:12
  • aah good lead. Can you print psexecloc from python? you need raw prefix or 4 antislashes else it fails. Commented Feb 21, 2017 at 14:12

1 Answer 1

1

Be aware that the syntax of PsExec for your needs is: psexec \\hostname -u user -p pass executable and if it's a GUI, add the -i flag.

From san's comment, it takes more time for the GUI executable to load, than a non-gui exe

So the following does work now that I've tested it:
The part that was missing is the backslashes before the hostname

import subprocess,time

psexecloc ="psexec.exe"
remotecmd="notepad.exe"
username = "XXXr"
password = "XXXXXXX"
remotehostname = "\\\\my-pc-name"
args = [psexecloc, "-i", "-u", username, "-p", password, remotehostname,   remotecmd]

output = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
time.sleep(30)
outstr=output.communicate()
stdoutstr = outstr[0]
stderrstr = outstr[1]
print ("Output Tuple: ",outstr)
Sign up to request clarification or add additional context in comments.

9 Comments

Psexec is not invoking the notepad.exe in the target machine, but I could see Psexec process in the task manager..
Unfortunately I don't have any accessible remote to test it again at the moment. Have you checked after running psexec if notepad is running on the remote? (even if not visible). You could also open notepad from a simple batch file. You can mark psexec to copy the file and then execute it. Maybe it will behave different
no, notepad is not running in the target machine but I could see only psexec process running in the target machine.
Any help on this will be appreciated
please try to add an -i flag (it receives a session number)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.