#!/bin/perl -w
use XML::Simple;
my $xml = new XML::Simple;

my $xmlinput = `onevm list -x`;
my $data = $xml->XMLin($xmlinput);

foreach $vm ( @{ $data->{VM} } ) {
    if ( $vm->{STATE} == 3 ){
        if ( $vm->{LCM_STATE} == 3 ){
	    $vm_id = $vm->{ID};
	    $vm_host = $vm->{HISTORY_RECORDS}->{HISTORY}->{HOSTNAME};
	    $vm_gtype = lc($vm->{TEMPLATE}->{GRAPHICS}->{TYPE});
	    $vm_gpasswd = $vm->{TEMPLATE}->{GRAPHICS}->{PASSWD};
	    print "/usr/bin/virsh --connect=qemu+ssh://$vm_host/system qemu-monitor-command --hmp one-$vm_id set_password $vm_gtype $vm_gpasswd\n";
	}
    }
}
