如何将多个命令馈送到命令行.exe使用psex在远程计算机上启动

2024-04-20 00:43:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用psexec连接到远程计算机并执行命令提示符. 一旦打开这个会话,我想运行多个命令,如mkdir、del等。我面临的问题是,我只能运行一个命令,子进程是communicate close the pipe。有什么办法可以完成吗?在

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-

^{pr2}$

看来我的“dir”不起作用了。在

PS:如何调试这种场景?在


Tags: 命令selfsessiondefdirrpcexexxx
1条回答
网友
1楼 · 发布于 2024-04-20 00:43:29

我在当地做过:

>>> import subprocess
>>> s = subprocess.Popen(['cmd.exe'], stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = subprocess.PIPE, )
>>> s.stdin.write('dir\r\n') # letting out '\r\n' does not run the command
>>> s.communicate()

我要问你的问题是:当你对psexec做同样的操作时,它还能工作吗?在

使用字符串connection_string而不是列表可能会有问题。 尝试:

  1. 添加Popen(…,shell=True)

  2. 使用列表。在

    [self.rpc_exec_path, '\\\\' + self.ip,  '-u', self.user,  '-p', self.password, 'cmd.exe']
    

相关问题 更多 >