Run console from user web via PHP API

gald

New Member
Jan 29, 2013
5
0
1
Hello to everybody,

I have started to use the PHP Api in order to give access to our customers to their VPS with KVM. We have seen that stopping and starting the virtual machine is really easy, and we will create 2 buttons that will stop or start the machine. We have tried that and is working properly.

The problem comes when we try to link our customer panel with the VPS console.

For that we have tried this way:

$nodename: Is the name of the node that we connect to
$vmid: Is an integer refering to the ID service or virtual machine
$ip: Is the IP of the server when Proxmox is, and the one used to login.

$pve2 = new PVE2_API($hostname, $username, $realm, $password);

if ($pve2->constructor_success()) {
if ($pve2->login()) {
echo "Login!"

$parameters = array("node" => $nodename,"vmid" => $vmid);
$response = $pve2->post("/nodes/$nodename/qemu/$vmid/vncproxy",$parameters);
var_dump($response);
echo "<br>";

echo "<a href='https://$ip:$port/?console=kvm&vmid=$vmid&vmname=&node=$nodename'>" . _("Open console") . "</a>";
}
}

We get Login! and then a false in var_dump($response). We would like that when opening the link, the console will appear to the user and he/she just could connect to his VPS, not see the rest of VPS in the server, etc...

Now when the click is made the Proxmox Console appears, but it asks again the login, password and the realm in order to login. In the top left we can read also Error No ticket($vmid) when $vmid is the ID of the virtual machine.

We have ensured that the machine is running.

I will much appreciate any help you can offer me.

Thanks in advance,

Gald
 
Hello to Dietmar and everybody,

Sorry, yes, I set the print debug and then I realized that I was sending some extra information that was not needed.
Now, I have fixed that changing the call to vncproxy:

$pve2->set_debug(true);
$parametros = array();
$devuelve = $pve2->post("/nodes/$nodename/qemu/$node/vncproxy",$parametros);

echo "<a href='https://$ip:$port/?console=kvm&vmid=$vmid&vmname=&node=$nodename'>" . _("Open console") . "</a>";

This way I get a OK message, you can see all the information below (I have deleted some private information), but when I click on the link I'm asked again to insert the login, the password and the realm. I would like to be an automatic login, to just give the user the access to the console and embed it on our customer panel.

Thank you very much again.

----------------------------------------------

FULL RESPONSE:

HTTP/1.1 200 OK
Date: Wed, 30 Jan 2013 07:46:11 GMT
Server: Apache
Pragma: no-cache
Cache-control: no-cache
Content-Length: 2062
Vary: Accept-Encoding
Connection: close
Content-Type: application/json;charset=UTF-8
Expires: Wed, 30 Jan 2013 07:46:11 GMT

{"data":{"cert":"-----BEGIN CERTIFICATE-----\n

00=\n-----END CERTIFICATE-----\n","user":"user1@pam","upid":"UPID::","port":5900,"ticket":"/=="}}

END FULL RESPONSE.

Headers:

HTTP/1.1 200 OK
Date: Wed, 30 Jan 2013 07:46:11 GMT
Server: Apache
Pragma: no-cache
Cache-control: no-cache
Content-Length: 2062
Vary: Accept-Encoding
Connection: close
Content-Type: application/json;charset=UTF-8
Expires: Wed, 30 Jan 2013 07:46:11 GMT

End Headers.

Data:

{"data":{"cert":"-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n","user":"user1@pam","upid":"UPID:h71a-:vncproxy:401:user1@pam:","port":5900,"ticket":"PVEVNC:5108CFC3::Jfp/=="}}

End Headers.

RESPONSE ARRAY:

Array
(
[data] => Array
(
[cert] => -----BEGIN CERTIFICATE-----
0=
-----END CERTIFICATE-----

[user] => user1@pam
[upid] => UPI .....
[port] => 5900
[ticket] => PVEVN99hxRzjOg==
)

)

END RESPONSE ARRAY.
 
Hello to Dietmar and everybody,

Sorry, yes, I set the print debug and then I realized that I was sending some extra information that was not needed.
Now, I have fixed that changing the call to vncproxy:

$pve2->set_debug(true);
$parameters = array();
$return = $pve2->post("/nodes/$nodename/qemu/$node/vncproxy",$parameters);

echo "<a href='https://$ip:$port/?console=kvm&vmid=$vmid&vmname=&node=$nodename'>" . _("Open console") . "</a>";

This way I get a OK message, you can see all the information below (I have deleted some private information), but when I click on the link I'm asked again to insert the login, the password and the realm. I would like to be an automatic login, to just give the user the access to the console and embed it on our customer panel.

Thank you very much again.

----------------------------------------------

FULL RESPONSE:

HTTP/1.1 200 OK
Date: Wed, 30 Jan 2013 07:46:11 GMT
Server: Apache
Pragma: no-cache
Cache-control: no-cache
Content-Length: 2062
Vary: Accept-Encoding
Connection: close
Content-Type: application/json;charset=UTF-8
Expires: Wed, 30 Jan 2013 07:46:11 GMT

{"data":{"cert":"-----BEGIN CERTIFICATE-----\n

00=\n-----END CERTIFICATE-----\n","user":"user1@pam","upid":"UPID::","port":5900,"ticket":"/=="}}

END FULL RESPONSE.

Headers:

HTTP/1.1 200 OK
Date: Wed, 30 Jan 2013 07:46:11 GMT
Server: Apache
Pragma: no-cache
Cache-control: no-cache
Content-Length: 2062
Vary: Accept-Encoding
Connection: close
Content-Type: application/json;charset=UTF-8
Expires: Wed, 30 Jan 2013 07:46:11 GMT

End Headers.

Data:

{"data":{"cert":"-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n","user":"user1@pam","upid":"UPID:h71a-:vncproxy:401:user1@pam:","port":5900,"ticket":"PVEVNC:5108CFC3::Jfp/=="}}

End Headers.

RESPONSE ARRAY:

Array
(
[data] => Array
(
[cert] => -----BEGIN CERTIFICATE-----
0=
-----END CERTIFICATE-----

[user] => user1@pam
[upid] => UPI .....
[port] => 5900
[ticket] => PVEVN99hxRzjOg==
)

)

END RESPONSE ARRAY.
 
Hello Dietmar and everybody,

Thank you very much for your help! We succeed! Yes, finally we have pasted an example of the applet params we found, adapt to us and now is working properly.

The information to show the console using PHP is the following one, hope it would be interesting for the people, and thanks Dietmar for your help!

if ($pve2->constructor_success()) {
// $pve2->set_debug(true);

if ($pve2->login()) {
$parameters = array(); //"vmid" => $node, "node" => $nodename,
$response = $pve2->post("/nodes/$nodename/qemu/$node/vncproxy",$parameters);
?>
<script>
PVE_vnc_console_event = function(appletid, action, err) {
}
</script>

<applet id='pveKVMConsole-1018-vncapp' border='false' code='com.tigervnc.vncviewer.VncViewer' archive='https://<?=$hostname?>/vncterm/VncViewer.jar' width='100%' height='100%' style='width: 640px; height: 506px; '>
<param name='id' value='pveKVMConsole-1018-vncapp'>
<param name='PVECert' value='<?php echo str_replace("\n", '|', $response["cert"]); ?>'>
<param name='HOST' value='<?php echo $hostname; ?>'>
<param name='PORT' value='<?php echo $response["port"]; ?>'>
<param name='USERNAME' value='<?php echo $response["user"]; ?>'>
<param name='password' value='<?php echo $response["ticket"]; ?>'>
<param name='Show Controls' value='Yes'>
<param name='Offer Relogin' value='Yes'>
</applet>

//In case the user wants to open in another window:
echo "<a href='https://" . $hostname . ":8006/?console=kvm&vmid=$node&vmname=&node=$nodename' target='_blank'>" . _("Open console") . "</a>";
 

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!