我正在尝试使用 psexec 连接到远程机器并执行 cmd.exe。一旦我打开了这个会话,我想运行多个命令,例如 mkdir、del 等。我面临的问题是我只能运行一个带有子进程的命令作为通信关闭管道。有什么方法可以实现吗?
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()
当我运行此代码时,我得到以下 o/p -
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.
所以看来我的“目录”不起作用。
PS:如何调试这种场景呢?