I would like to run a script in my Linux Guest VMs by using IGuestSession.processCreate but that didn't work.
It's for a prototype and I can not open an SSH terminal, everything must be done in my java program.
Here is my code in java :
Code: Select all
protected final static List<String> VM_ENV_CMD = Arrays.asList("BASH=/bin/bash");
private static VirtualBoxManager connectVBox()
{
VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
try {
mgr.connect(VBOX_URL, VBOX_USER, VBOX_PASSWORD);
} catch (VBoxException e) {
e.printStackTrace();
System.out.println("Cannot connect, start webserver first!");
}
return mgr;
}
public static void main (String args[]) throws Exception
{
// Connecting to VBox
VirtualBoxManager mgr = connectVBox();
IVirtualBox vbox = mgr.getVBox();
IMachine vmOne = vbox.findMachine("My VM");
ISession mgrSession;
mgrSession = mgr.openMachineSession(vmOne);
IConsole mgrConsole = mgrSession.getConsole ();
IGuestSession vmSession = mgrConsole.getGuest().createSession(VM_USERNAME, VM_USERPASS, null, null);
vmSession.waitForArray(Arrays.asList(GuestSessionWaitForFlag.Start), (long) 0);
List<ProcessCreateFlag> pFlag = new ArrayList<ProcessCreateFlag>();
pFlag.add(ProcessCreateFlag.WaitForProcessStartOnly);
List<String> pArg = new ArrayList<String>();
pArg.add("/path/to/myscript.sh");
pArg.add("-arg1");
pArg.add("arg2");
vmSession.processCreate("/bin/bash", pArg, VM_ENV_CMD, pFlag,(long) 0);
// Closing the session
vmSession.close();
mgr.closeMachineSession(mgrSession);
// Closing VBox
mgr.disconnect();
System.out.println("OK");
}
My host OS is Win 8 and my guest is a mini Lubuntu based on Linux version 3.2.0-58.
Thank you for your help.
PS : Sorry if I make some english mistake.