Command Line for virt-viewer

mechanicus

Renowned Member
Mar 30, 2009
54
0
71
Germany
Hello,
to start a spice session from the web interface by clicking the "SPICE"-Buttom on the right upper corner runs well. Great work!
But what's the command line, if I want to start a spice connection from a desktop linux machine to a VM in my Proxmox-node by virt-viewer?
 
Hi,

the pve-spice server uses short-lived (like 30 secs) tickets, so you can only launch them through the web gui link, sadly.
That's why they are configured to auto-delete them selves after use.

I guess it is a security feature, but I also would like to be able to permanently use spice without having to log into web gui.
A separate spice server would be needed, perhaps, and that, I guess, is really difficult...

Let's hope in some more info from proxmox devs...

Marco
 
Вариант для операционной системы Windows. Работает под ActivePerl. Вариант с curl под Windows, увы, не работает.

Code:
#!/usr/bin/perl 

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Protocol::https;
use HTTP::Cookies;
use Cwd 'abs_path';

my $USERNAME='mylogin@pam';
my $PASSWORD='mypsw';
my $VMID=100;
my $NODE='svc3';
my $PROXY='10.0.0.222';
my $FPATH = "spiceproxy";  
my $PPATH = 'C:\Program Files\VirtViewer\bin';

my $DATA = api_req($PROXY, '/api2/json/access/ticket', 'username='.$USERNAME.'&password='.$PASSWORD);
my ($TICKET) = $DATA =~ /\"ticket\"\:\"(.*?)\"/;
my ($CSRF) = $DATA =~ /\"CSRFPreventionToken\"\:\"(.*?)\"/;
$DATA = api_req($PROXY, '/api2/spiceconfig/nodes/'.$NODE.'/qemu/'.$VMID.'/spiceproxy', 'proxy='.$PROXY, ['CSRFPreventionToken',$CSRF], [['PVEAuthCookie',$TICKET]]);
open(my $fh, '>', $FPATH);
print $fh $DATA;
close $fh;
my $abs_path = abs_path($FPATH);
chdir($PPATH) or die("Can't change to dir $PPATH: $!\n");;
exec("remote-viewer.exe", $abs_path);

sub api_req
{
  my ($prx, $url, $data, $head, $cook) = @_;
  my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 },);
  # Create a request
  my $req = HTTP::Request->new(POST => 'https://'.$prx.':8006'.$url);
  $req->content_type('application/x-www-form-urlencoded');
  $req->content($data);
  $req->header($head->[0], $head->[1]) if(defined($head));
  print $req->headers->as_string."\n";
  if(defined($cook))
  {
    my $cookie_jar = HTTP::Cookies->new();
    foreach my $c (@$cook)
    {
      $cookie_jar->set_cookie(0,$c->[0], $c->[1],'/',$prx,8006,0,0,86400,0);
    }
    $ua->cookie_jar($cookie_jar);
  }
  my $res = $ua->request($req);
  
  # Check the outcome of the response
  if ($res->is_success) 
  {
      return $res->decoded_content;
  }
  else 
  {
      die $res->status_line, "\n";
  }
}
 
Option for the Windows operating system. Works under ActivePerl. The variant with curl under Windows, alas, does not work.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Protocol::https;
use HTTP::Cookies;
use Cwd 'abs_path';

my $USERNAME='mylogin@pam';
my $PASSWORD='mypsw';
my $VMID=100;
my $NODE='svc3';
my $PROXY='10.0.0.222';
my $FPATH = "spiceproxy";
my $PPATH = 'C:\Program Files\VirtViewer\bin';

my $DATA = api_req($PROXY, '/api2/json/access/ticket', 'username='.$USERNAME.'&password='.$PASSWORD);
my ($TICKET) = $DATA =~ /\"ticket\"\:\"(.*?)\"/;
my ($CSRF) = $DATA =~ /\"CSRFPreventionToken\"\:\"(.*?)\"/;
$DATA = api_req($PROXY, '/api2/spiceconfig/nodes/'.$NODE.'/qemu/'.$VMID.'/spiceproxy', 'proxy='.$PROXY, ['CSRFPreventionToken',$CSRF], [['PVEAuthCookie',$TICKET]]);
open(my $fh, '>', $FPATH);
print $fh $DATA;
close $fh;
my $abs_path = abs_path($FPATH);
chdir($PPATH) or die("Can't change to dir $PPATH: $!\n");;
exec("remote-viewer.exe", $abs_path);

sub api_req
{
  my ($prx, $url, $data, $head, $cook) = @_;
  my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 },);
  # Create a request
  my $req = HTTP::Request->new(POST => 'https://'.$prx.':8006'.$url);
  $req->content_type('application/x-www-form-urlencoded');
  $req->content($data);
  $req->header($head->[0], $head->[1]) if(defined($head));
  print $req->headers->as_string."\n";
  if(defined($cook))
  {
    my $cookie_jar = HTTP::Cookies->new();
    foreach my $c (@$cook)
    {
      $cookie_jar->set_cookie(0,$c->[0], $c->[1],'/',$prx,8006,0,0,86400,0);
    }
    $ua->cookie_jar($cookie_jar);
  }
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success)
  {
      return $res->decoded_content;
  }
  else
  {
      die $res->status_line, "\n";
  }
}

I know this post is really old, but would be nice if this was still doable. I tried on version 6.1 and get
500 Can't connect to [server IP]:8006

Does anyone know how to update this code to work with 6.1?

UPDATE: Ok, I figured it out. I wasn't running ActiveState Perl. I downloaded ActiveState Perl and now it works :) Thank you for this code!

One other question though, is there a way to make it "Prompt" the user for the password instead of storing it in plain text?
 
Last edited:
Code:
my $USERNAME='mylogin@pam';
my $PASSWORD='mypsw';
my $VMID=100;
my $NODE='svc3';
my $PROXY='10.0.0.222';
my $FPATH = "spiceproxy"; 
my $PPATH = 'C:\Program Files\VirtViewer\bin';

[Russian , via Google Translate]
[Русский - через Google Translate]
Спасибо за размещение кода; ссылка выше была сломана (или, возможно, доступна только для коммерческих пользователей?).
Я отключил логин UserName / Password по соображениям безопасности и использую только аутентификацию пары ключей.
Я не знаю программирования на Perl, поэтому не могу отредактировать код, чтобы я мог его использовать.
Есть ли простое изменение кода, которое позволило бы мне использовать код с аутентификацией пары ключей к хосту?

[English]
Thank you for posting the code; the link above was broken (or maybe only available to commerical users?).
I have disabled UserName/Password login for security reasons and only use key-pair authentication.
I do not know Perl programming so cannot edit the code to let me use it.
Is there an easy code change that would let me use the code with key-pair authentication to the host?