[英]Python subprocess.call() apparently not working with psexec
I am having an issue executing remote processes with subprocess.call()
and psexec. 我在使用
subprocess.call()
和psexec执行远程进程时遇到问题。 I am using the following syntax for executing the process remotely with subprocess.call()
: 我正在使用以下语法通过
subprocess.call()
远程执行subprocess.call()
:
def execute(hosts):
''' Using psexec, execute the script on the list of hosts '''
successes = []
wd = r'c:\\'
file = r'c:\\script.exe'
for host in hosts:
res = subprocess.call(shlex.split(r'psexec \\%s -e -s -d -w %s %s ' % (host,wd,file)), stdin=None, stdout=None, stderr=None)
if res == 0:
successes.append(host)
else:
logging.warning("Error executing script on host %s with error code %d" % (host, res))
print shlex.split(r'psexec \\%s -e -s -d -w %s %s ' % (hosts[0],wd,file))
return successes
As you can see, as part of my troubleshooting, I am printing the shlex.split()
output to ensure that it is what I want. 如您所见,作为故障排除的一部分,我正在打印
shlex.split()
输出以确保它是我想要的。 This print statement gives: 该打印声明提供:
['psexec', '\\HOSTNAME', '-e', '-s', '-d', '-w', 'c:\\', 'c:\\script.exe']
Which is what I would expect. 这是我所期望的。 Unfortunately, when I run it, I get an error saying:
不幸的是,当我运行它时,我收到一条错误消息:
PsExec could not start \GN-WRK-02:
The system cannot find the file specified.
Directly after this, I run the psexec command with the exact syntax that the program should be running it with (judging by the shlex.split()
output) and it works completely fine. 在此之后,我立即以程序应使用的确切语法运行psexec命令(由
shlex.split()
输出判断),并且可以正常工作。 My syntax is: 我的语法是:
psexec \\HOSTNAME -e -s -d -w c:\\ c:\\script.exe
Any ideas why this wouldn't be working? 有什么想法为什么这行不通吗? If it matters the execute function is being called through multiprocessing's
map()
function on two or 3 host lists. 如果很重要,则可以通过两个或三个主机列表上的multiprocessing的
map()
函数来调用execute函数。
Any help would be great! 任何帮助将是巨大的! Thanks!
谢谢!
Your \\\\
double slash in front of the hostname is just one slash; 主机名前面的
\\\\
双斜杠只是一个斜杠; it is doubled to escape the slash. 它加倍以逃避斜线。
You can see this in the shlex.split()
output: 您可以在
shlex.split()
输出中看到以下内容:
['psexec', '\\HOSTNAME', '-e, '-s', '-d', '-w', 'c:\\', 'c:\\script.exe']
note that \\\\
before the hostname is just the two backslashes, just like in the c:\\\\
filename value. 请注意,主机名之前的
\\\\
只是两个反斜杠, 就像在c:\\\\
文件名值中一样。 . 。 If you print just that value you see that the backslash at the sart is just one character:
如果仅打印该值,则可以看到该sart处的反斜杠只是一个字符:
>>> print '\\HOSTNAME'
\HOSTNAME
>>> '\\HOSTNAME'[0]
'\\'
>>> '\\HOSTNAME'[1]
'H'
That's because shlex.split()
is a POSIX tool, not a Windows tool, and it interprets the \\\\
in the raw string as an escape too; 这是因为
shlex.split()
是POSIX工具,而不是Windows工具,它也将原始字符串中的\\\\
解释为转义符。 if you are using that tool, double the slashes again: 如果您使用的是该工具,请再次将斜杠加倍:
shlex.split(r'psexec \\\\%s -e -s -d -w %s %s ' % (host,wd,file))
An alternative may be to disable POSIX mode, but I am not entirely certain how that would interplay with Windows: 一种替代方法是禁用 POSIX模式,但是我不确定这将如何与Windows相互作用:
shlex.split(r'psexec \\%s -e -s -d -w %s %s ' % (host,wd,file), posix=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.