简体   繁体   English

如何使用pysphere在远程机器上执行命令

[英]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个有趣的参数:

  • cmd.exe /c ... , to just close cmd window after execution cmd.exe / c ...,执行后只关闭cmd窗口
  • cmd.exe /k ... , to not close console window after execution commands cmd.exe / k ...,执行命令后不关闭控制台窗口

/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.

相关问题 如何连接到远程 Windows 机器以使用 python 执行命令? - How to connect to a remote Windows machine to execute commands using python? 使用python脚本在远程计算机上执行bash命令 - Execute bash commands on remote machine using python script 如何通过使用本地计算机上的命令执行远程Python以在远程计算机上打开网站 - how to execute remote python to open a web on remote machine by using a command on a local machine 如何使用python在远程Windows计算机中执行.exe文件 - How to execute .exe file in a remote windows machine using python 如何使用 psexec 在远程机器上执行 python 脚本? - how to execute python script on remote machine using psexec? Python:并行执行pysphere命令 - Python: Parallel execution pysphere commands 使用Python和paramiko以root身份执行远程命令 - Execute remote commands as root using Python and paramiko 使用paramiko在远程机器上执行git命令 - execute git command in a remote machine using paramiko 如何使用paramiko执行远程命令 - how to use paramiko to execute remote commands 如何使用python3中的无密码身份验证通过子进程方法在REMOTE机器中运行命令? - How to run commands in REMOTE machine with the subprocess approach using passwordless authentication in python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM