[SOLVED] Execute command in node with API

SKORPIA

New Member
Jul 15, 2022
13
1
1
Hi, i'm trying to do this :

Code:
$params = array("commands" => json_encode(array( "pct exec ". $lxcid." -- bash -c 'echo PermitRootLogin Yes >> /etc/ssh/sshd_config'" ))));



if($pve->post("/nodes/proxmox/execute/", $params){
     echo '{"result": "good", "desc": "cmd 1 ok"}';
 }else{
     echo '{"result": "error", "desc": "error during command ssh"}';
 }


And i have "cmd 1 ok" in my logs, but when i go in the container $lxcid, nothings were append in the target file

I precise, i can't use ssh to type the command, it's for automatic task just after the creation of the container and to automatically give root access to the user with his password
 
Last edited:
I try to add some commands in my array, but nothing happened and API returns true, like the command were correctly executed but were not
 
  1. that API endpoint is not for executing arbitrary commands, but for calling API endpoints in a sort of batch mode (the 'commands' parameter is a list of API endpoints to call and their arguments) - the description/docs could be clearer, a colleague is already working on that ;)
  2. your code doesn't seem to check the server response, just that the POST call worked
 
  • Like
Reactions: sterzy
  1. that API endpoint is not for executing arbitrary commands, but for calling API endpoints in a sort of batch mode (the 'commands' parameter is a list of API endpoints to call and their arguments) - the description/docs could be clearer, a colleague is already working on that ;)
  2. your code doesn't seem to check the server response, just that the POST call worked
Thx for your answer, so how to execute commands in lxc with api ? (As you see I would write in a file)
 
there is no API endpoint for that. you can specify an SSH key at container creation time (in addition, or instead of the root password) to give access. there is also pct exec and/or pct push, but those are CLI only.
 
there is no API endpoint for that. you can specify an SSH key at container creation time (in addition, or instead of the root password) to give access. there is also pct exec and/or pct push, but those are CLI only.
Ok, I see, so how to generate an ssh public key and assign it to the creation of a container ?
Below my php script for lxc creation :

Code:
$sTemplateName = 'local:vztmpl/debian-11-standard_11.3-1_amd64.tar.zst'; // .tar.gz
$aServerSpecs  = array ( );

// Specs
$aServerSpecs [ 'ostemplate' ] = $sTemplateName;
$aServerSpecs [ 'vmid' ]       = $pve->get_next_vmid ( );
$aServerSpecs [ 'hostname' ]   = 'VPS'+$aServerSpecs [ 'vmid' ] ;
$aServerSpecs [ 'password' ]   = $passwd;

// CPU
$aServerSpecs [ 'arch' ]     = 'amd64';
$aServerSpecs [ 'cpulimit' ] = 1;
$aServerSpecs [ 'cpuunits' ] = 1000;

// Memory
$aServerSpecs [ 'memory' ] = '2048';
$aServerSpecs [ 'swap' ]   = '512';

// HDD
$aServerSpecs [ 'rootfs' ]  = 20; // ?
$aServerSpecs [ 'storage' ] = 'local-lvm'; // ?

// Network
$aServerSpecs [ 'net0' ] = 'bridge=vmbr1,gw=192.168.20.1,hwaddr=12:34:56:77:90:AB,ip=192.168.20.'.$aServerSpecs [ 'vmid' ].'/24,name=eth0,type=veth';


// Create
$pve->post ( '/nodes/proxmox/lxc', $aServerSpecs );

And how to connect to lxc with the public key created ?
 
well generation is usually done with ssh-keygen, but you can do it however you want of course. the parameter to set it is 'ssh-public-keys', and afterwards you can just use regular ssh using the key to access the container?
 
well generation is usually done with ssh-keygen, but you can do it however you want of course. the parameter to set it is 'ssh-public-keys', and afterwards you can just use regular ssh using the key to access the container?
Else, it is possible to modificate the lxc image ? To modifiy a file content for all lxc i'm going to deploy
 
sure, you can use a custom template with a different sshd config file..
 

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!