Qemu Agent Exec for API2 is broken.

sonuyos

Active Member
Jun 18, 2020
45
0
26
29
If i run the following command in SSH, it works fine

Code:
root@usa01:~# qm guest exec 125 -- cmd.exe /c slmgr.vbs -rearm && shutdown -r
timeout reached, returning pid
{
   "pid" : 2968
}
Reboot scheduled for Thu 2024-08-29 10:35:12 EDT, use 'shutdown -c' to cancel.

However same command via API2 throws me an error - Agent error: Guest agent command failed, error was 'Failed to execute child process (No such file or directory)'

Code:
POST https://HOSTNAME:8006/api2/json/nodes/usa01/qemu/125/agent/exec
Array
(
    [command] => cmd.exe /c slmgr.vbs -rearm && shutdown -r
)

Duration: 552150 microseconds; 0.55215 seconds

Code:
HTTP 500 HTTP/1.1 500 Agent error: Guest agent command failed, error was 'Failed to execute child process (No such file or directory)'
Cache-Control: max-age=0
Connection: close
Date: Thu, 29 Aug 2024 14:31:38 GMT
Pragma: no-cache
Server: pve-api-daemon/3.0
Content-Length: 13
Content-Type: application/json;charset=UTF-8
Expires: Thu, 29 Aug 2024 14:31:38 GMT

{"data":null}

I am confused how one works via SSH and doesn't via API2.

I suspect API2 is broken for it.
 
IIRC the commands need to be escaped correctly via the API. Does this post help you? https://forum.proxmox.com/threads/proxmox-api-agent-exec-escape-chars.84833/#post-372675
Code:
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://REDACTED:8006/api2/json/nodes/usa01/qemu/125/agent/exec',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
  "command": "cmd.exe /c slmgr.vbs -rearm"
}',
  CURLOPT_HTTPHEADER => array(
    'CSRFPreventionToken: REDACTED',
    'Cookie: PVEAuthCookie=PVE:root@pam:REDACTED',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


Even curl is giving me the same error -

500 Agent error: Guest agent command failed, error was 'Failed to execute child process (Invalid argument)'​

 
The command in the linked thread is for a Linux guest. Therefore, you don't need to copy&paste it as is, as that won't work on Windows ;)

Instead of a regular space between the different positions in your command, you need to use the \u0000. And if you run it via curl, you want to use the urlencoded variant of the NUL character: %00
So in the end, you want the command string to be something like this if you pass it as data --command parameter into the CURL call.
Code:
cmd.exe%00/c%00slmgr.vbs%00-rearm%00&&%00shutdown%00-r

Of use the unicode form \u0000 and run the string through an urlencode function.
 
The command in the linked thread is for a Linux guest. Therefore, you don't need to copy&paste it as is, as that won't work on Windows ;)

Instead of a regular space between the different positions in your command, you need to use the \u0000. And if you run it via curl, you want to use the urlencoded variant of the NUL character: %00
So in the end, you want the command string to be something like this if you pass it as data --command parameter into the CURL call.
Code:
cmd.exe%00/c%00slmgr.vbs%00-rearm%00&&%00shutdown%00-r

Of use the unicode form \u0000 and run the string through an urlencode function.
Still nada.

Getting -

500 Agent error: Guest agent command failed, error was 'Failed to execute child process (No such file or directory)'​



However if I put only cmd.exe, it seems to work. but if I put /c it gives error.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!