virtualcontrol Code
Status: Beta
Brought to you by:
hoefer
#! /usr/bin/perl
# This is virtualcontrol, a administration software for KVM virtual machines
# Copyright (C) 2008-2014 Stefan Hoefer <stefan@hoefer.ch>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use Socket;
use IO::Socket::INET;
my $vncpid;
my $sshpid;
$::SIG{INT} = $::SIG{TERM} = sub {
if (defined($vncpid)) {
kill 'TERM', $vncpid;
}
if (defined($sshpid)) {
kill 'TERM', $sshpid;
}
};
my $vtname = $ARGV[0];
if (!defined($vtname) || length($vtname) == 0) {
print "You need to specify a VT host to connect to:\n";
die ' vcview <vtname>\n';
}
my $found = 0;
my $vncport = 0;
my $vthost;
my $password = "";
my $exestring = "/usr/sbin/vcontrol list $vtname |";
if (open my $fd, $exestring) {
my $running = 0;
while (my $line = <$fd>) {
chomp $line;
if ($line =~ "^${vtname}:") {
$found = 1;
}
elsif ($line =~ /vncport: (\d+)/) {
if ($found) {
$vncport = $1;
}
}
elsif ($line =~ /status: running/ || $line =~ /status: migrating/) {
$running = 1;
}
elsif ($line =~ /vncpassword: (.+)/) {
$password = $1;
chomp $password;
}
}
close $fd;
if ($found) {
if (!$running) {
print "Found, but not running\n";
# STH: don't stop searching, just skip this one.
$found = 0;
}
}
else {
print "Skipping.\n";
}
}
if ($found) {
my $mainpid = $$;
$vncpid = fork();
if (!$vncpid) {
sleep(1);
if (length($password) > 0) {
my $outfile = "/tmp/.$::ENV{USER}-vnc";
if (-e $outfile) {
unlink $outfile;
}
if (-e $outfile) {
die "Security alert: Could not remove $outfile!\n";
}
#if (open my $pfd, "| vncpasswd -f > $outfile") {
if (open my $pfd, "| vcpw > $outfile") {
print $pfd $password;
close $pfd;
}
chmod 0600, $outfile;
#exec("vncviewer :$vncport -passwd $outfile -name $ARGV[0]-$mainpid");
#exec("vncviewer :$vncport -passwd $outfile");
exec("ssvncviewer /volumes/data/guests/$vtname/run/vnc.sock -passwd $outfile");
}
else {
#exec("vncviewer :$vncport -name $ARGV[0]-$mainpid");
exec("vncviewer :$vncport");
}
exit(0);
}
waitpid $vncpid, 0;
exit(0);
}