How to run as script in Linux Guest

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
Willys
Posts: 1
Joined: 30. Jan 2014, 02:17

How to run as script in Linux Guest

Post by Willys »

Hello,

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");
	}
I'm currently using VBox API 4.3 in Java via the WebServer.
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.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to run as script in Linux Guest

Post by noteirak »

Willys wrote:but that didn't work.
Can you ellaborate? What was the error message? what was the behaviour you got and the one you were expecting?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply