I'm trying to connect to remote machine using psexec and execute the cmd.exe. Once I've this session open I want to run multiple commands such as mkdir, del, etc. I'm facing the problem that i can only run one command with subprocess as communicate close the pipe. Is there is any way to accomplish?
from subprocess import Popen, PIPE, STDOUT
class WsRPC():
def __init__(self):
self.rpc_exec_path = r'C:\SysinternalsSuite\psexec.exe'
self.user = 'administrator'
self.ip = '172.xxx.xxx.xxx'
self.password = 'XxXxXxXx'
self.session = ''
def wsConnect(self):
pass
def runCommand(self):
try:
self.session = Popen([self.rpc_exec_path, '\\\\' + self.ip, '-u',
self.user, '-p', self.password, 'cmd.exe'],
stdin = PIPE,stdout = PIPE,stderr = PIPE,
shell = True)
command = 'cmd.exe /c dir'
self.session.stdin.write('dir/r/n')
strout, strerr = self.session.communicate()
print strout
print strerr
except Exception,e:
print str(e)
obj = WsRPC()
obj.runCommand()
I'm getting the following o/p when i run this code -
C:\SysinternalsSuite\psexec.exe \\172.xxx.xxx.xxx -u administrator
-p XxXxXxXx cmd.exe
Microsoft Windows [Version 5.2.3790]
PsExec v2.0 - Execute processes remotely
Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com
Connecting to 172.xxx.xxx.xxx...
Starting PSEXESVC service on 172.xxx.xxx.xxx...
Connecting with PsExec service on 172.xxx.xxx.xxx...
Starting cmd.exe on 172.xxx.xxx.xxx...
cmd.exe exited on 172.xxx.xxx.xxx with error code 0.
So it seems that my "dir" is not working.
PS: How to debug this kind of scenario too?