ps工具使用python

时间:2018-04-11 12:37:49

标签: python python-2.7 subprocess

我正在尝试在远程服务器上运行批处理脚本。所以我正在编写这样的代码。

import subprocess
subprocess.call("psExec \\\\11.456.787.8 -u {} -p {} C:\\basicfolder\\".format('user','pwd')+'run_1523448627.15.bat',shell=True)

错误是

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

Couldn't access 11.456.787.8:
Access is denied.
5

但是我收到了拒绝访问的错误。当我手动尝试psexec命令时,它正在连接并执行。但在python中它抛出错误

1 个答案:

答案 0 :(得分:0)

我怀疑用户名或密码中未显示的内容未被命令行正确转义。您应该使用实际参数重写命令:

import subprocess
username = "foo"
password = "whatever"
subprocess.call(["psExec","/accepteula",r"\\11.456.787.8","-u",username,"-p",password,"whateverfile.bat'])

这样:

  • 将命令分成args
  • 删除shell=True
  • 添加/accepteula不再有新的个人资料弹出窗口(奖金)