Monday, October 15, 2012

perl script to close all running virtualbox machine

dara@tun-K42JB:/home/tun# cat myutil/vpoweroff.pl 




#!/usr/bin/perl
#open FIL,"|/usr/bin/vboxmanage list runningvms";
@svms = `/usr/bin/vboxmanage list runningvms`;
#print @svms.length;
if (@svms.length == 0){
    print "no running vms\n";
    exit 1;
}
#while (<FIL>){
@vms = split(/\s+\s+/,$svms);
#print @svms.length;
while (<@svms>){
#    if ($_ =~ /^"*"$/){
    $vm[$n] = $_;
#    }
#    print $_."\n";
    $n++

}
#print $vm[0];
#    print $_;
   
#}

#print FIL;
#while ($i < @vms.length){
while ($i < $n){
#    $vm[$i] ~ s/$vm[$i]/\"$vm[$i]\"/g;
    print $vm[$i]."\n";
    system("vboxmanage controlvm \"$vm[$i]\" poweroff");
    $i = $i + 2;
}

No comments:

Post a Comment