質問

Have VMware VM on which i wanted to execute some commands for example shutdown /r or dir /o:d etc... from remote machine. Using module pysphere module for communicating with 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"])

役に立ちましたか?

解決

when you run the command cmd.exe you need to add /c:

cmd.exe /c shutdown /r

As explained here, you need to tell cmd.exe to run the parameter you passed as a string. Your command was just running cmd.exe without actually issuing the command that you passed as a parameter. the /c flag instructs cmd.exe to execute the parameter that is passed.

他のヒント

VM_object.start_process('cmd.exe', args=["shutdown /r"]) in virtual machine this command will be interpreted as

cmd.exe "shutdown /r"

because start_process use subprocess.list2cmdline() method to parse "args" parameter. list2cmdline() adds quotes for any list's element that contain spaces.

So, next, about cmd parameters. Use

cmd /?

to understand what you should use in your code. There is 2 intresting args for me in my tasks:

  • cmd.exe /c ... , to just close cmd window after execution
  • cmd.exe /k ... , to not close console window after execution commands

/k usefull, for example, when your python code intrested in asking last %errorlevel% of batch-file, executed by popen.

So, i think, your code should looks like:

VM_object.start_process('cmd.exe', args=["/c","shutdown", "/r"])
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top