VMware {code} Community
ssmann
Contributor
Contributor

perl vix api looping

I set up a loop to continuously take a snaphot, run a program in guest, copy a file and then revert to snapshot and repeat the process by running the program in guest (with new arguments). however, my program hangs on the ReleaseHandle/HostDisconnect functions (end of first iteration). I tried to exclude them from my while loop but that didnt work.

se VMware::Vix::Simple;

use VMware::Vix::API::Constants;

open(fileHandle, "malwarefiles.txt");

open(fileHandle2, "directories.txt");

my $flag = 0;

while($progName = ;

$directory = "\"".$directory."\"";

$progName = "\"".$progName."\"";

$line = 'C:
malwareAutorun.py '.$directory.' '.$progName;

print $line;

my $endChar = index($progName, '.');

my $outFileName = substr($progName, 1, $endChar-1);;

my $destFileName = 'C:
Documents and Settings
Sean
Desktop
workLogs
'.$outFileName.'.'.'CSV';

print "\n$destFileName\n";

($err, $hostHandle) = HostConnect(1,

VIX_SERVICEPROVIDER_VMWARE_SERVER,

undef,

0,

undef,

undef,

0,

VIX_INVALID_HANDLE);

die "HostConnect() fialed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

$err = RegisterVM($hostHandle, "c:
Program Files
Qemu
WindowsXP.vmx");

die "RegisterVM() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "Done registering vm\n";

($err, $vmHandle) = VMOpen($hostHandle, "c:
Program Files
Qemu
WindowsXP.vmx");

die "VMOpen() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "Done vmOpening\n";

$err = VMPowerOn($vmHandle, VIX_VMPOWEROP_LAUNCH_GUI, VIX_INVALID_HANDLE);

die "VMPowerOn() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "Done powering on\n";

$err = VMWaitForToolsInGuest($vmHandle, 300);

die "VMWaitForToolsInGuest() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

$err = VMLoginInGuest($vmHandle, "Sean", "qwerty", 0);

die "VMLoginInGuest() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "done logging in guest\n";

($err, $snapshotHandle) = VMCreateSnapshot($vmHandle,

undef,

undef,

VIX_SNAPSHOT_INCLUDE_MEMORY,

VIX_INVALID_HANDLE);

die "CreateSnapshot() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "done creating snapshot\n";

$err = VMRunProgramInGuest($vmHandle,

"C:
Python25
python.exe",

#'C:
malwareAutorun.py '."\"$directory\" "."\"$progName\"",

#"C:
malwareAutorun.py \"$directory\" \"$progName\"",

$line,

0,

VIX_INVALID_HANDLE);

die "VMRunProgramInGuest() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "Done running prog in guest\n";

$err = VMCopyFileFromGuestToHost($vmHandle,

"C:
logFile.CSV",

$destFileName,

0,

VIX_INVALID_HANDLE);

die "VMCopyFileFromGuestToHost() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "done copying file\n";

$err = VMRevertToSnapshot($vmHandle,

$snapshotHandle,

0,

VIX_INVALID_HANDLE);

die "VMRevertToSnapshot() failed, $err ", GetErrorText($err), "\n" if $err != VIX_OK;

print "done reverting to snapshot\n";

ReleaseHandle($vmHandle);

HostDisconnect($hostHandle);

}

die "Done\n" if 1;

0 Kudos
3 Replies
lemke
VMware Employee
VMware Employee

HostConnect() is very expensive, and we don't recommend you put it inside the loop like this (pull the Disconnect out as well). Its also pretty fragile in VMware server.

What verson of VMware server are you using?

0 Kudos
ssmann
Contributor
Contributor

vmware server vers 1.0.3. I edited the script by your suggestions and the script stalled at the 2nd iteration, after printing "done creating snapshot", just as before

0 Kudos
ssmann
Contributor
Contributor

im using vix 1.0 if that helps. i want to create a snapshot, run a program in the guest, copy a file, revert to snapshot and then repeat numerous times. is there a way to do this (since the way I posted above didnt seem to work)

0 Kudos