[英]how to execute commands on remote machine using pysphere
Have VMware VM on which i wanted to execute some commands for example shutdown /r or dir /o:d etc... from remote machine. 让我想在远程机器上执行某些命令的VMware VM,例如shutdown / r或dir / o:d等。 Using module pysphere module for communicating with VM. 使用模块pysphere模块与VM通信。
i tried start_process but it is just creating process for cmd.exe my code for creating process is VM_object.start_process('cmd.exe', args=["shutdown /r"]) 我尝试了start_process,但它只是为cmd.exe创建进程,我的代码用于创建进程是VM_object.start_process('cmd.exe',args = [“shutdown / r”])
when you run the command cmd.exe you need to add /c: 当您运行命令cmd.exe时,您需要添加/ c:
cmd.exe /c shutdown /r
As explained here , you need to tell cmd.exe to run the parameter you passed as a string. 正如解释在这里 ,你需要告诉CMD.EXE来运行你作为一个字符串传递的参数。 Your command was just running cmd.exe without actually issuing the command that you passed as a parameter. 您的命令只是运行cmd.exe而没有实际发出您作为参数传递的命令。 the /c flag instructs cmd.exe to execute the parameter that is passed. / c标志指示cmd.exe执行传递的参数。
VM_object.start_process('cmd.exe', args=["shutdown /r"]) in virtual machine this command will be interpreted as 虚拟机中的VM_object.start_process('cmd.exe',args = [“shutdown / r”])此命令将被解释为
cmd.exe "shutdown /r"
because start_process use subprocess.list2cmdline() method to parse "args" parameter. 因为start_process使用subprocess.list2cmdline()方法来解析“args”参数。 list2cmdline() adds quotes for any list's element that contain spaces. list2cmdline()为包含空格的任何列表元素添加引号。
So, next, about cmd parameters. 那么,接下来,关于cmd参数。 Use 采用
cmd /?
to understand what you should use in your code. 了解您应该在代码中使用什么。 There is 2 intresting args for me in my tasks: 在我的任务中有2个有趣的参数:
/k usefull, for example, when your python code intrested in asking last %errorlevel% of batch-file, executed by popen. / k有用,例如,当您的python代码有兴趣询问由popen执行的批处理文件的最后%errorlevel%时。
So, i think, your code should looks like: 所以,我认为,您的代码应如下所示:
VM_object.start_process('cmd.exe', args=["/c","shutdown", "/r"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.