noteirak

VirtualBox API - Run Process in guest and read stdout

Feb 24th, 2015
797
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  3.  *                     Version 2, December 2004
  4.  *
  5.  * Everyone is permitted to copy and distribute verbatim or modified
  6.  *  copies of this license document, and changing it is allowed as long
  7.  * as the name is changed.
  8.  *
  9.  *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  10.  *   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  11.  *
  12.  *  0. You just DO WHAT THE FUCK YOU WANT TO.
  13.  */
  14.  
  15. import java.util.Arrays;
  16. import org.virtualbox_4_3.GuestSessionStatus;
  17. import org.virtualbox_4_3.IEvent;
  18. import org.virtualbox_4_3.IEventListener;
  19. import org.virtualbox_4_3.IEventSource;
  20. import org.virtualbox_4_3.IGuestProcess;
  21. import org.virtualbox_4_3.IGuestSession;
  22. import org.virtualbox_4_3.IMachine;
  23. import org.virtualbox_4_3.ISession;
  24. import org.virtualbox_4_3.LockType;
  25. import org.virtualbox_4_3.ProcessCreateFlag;
  26. import org.virtualbox_4_3.ProcessWaitForFlag;
  27. import org.virtualbox_4_3.ProcessWaitResult;
  28. import org.virtualbox_4_3.VBoxEventType;
  29. import org.virtualbox_4_3.VirtualBoxManager;
  30.  
  31. public class GuestSessionProcess {
  32.  
  33.    private static String errorIfNull(String systemProperty) {
  34.       String value = System.getProperty(systemProperty);
  35.       if (value != null) {
  36.          return value;
  37.       } else {
  38.          throw new RuntimeException(systemProperty + " is not set");
  39.       }
  40.    }
  41.    
  42.    public static void main(String[] args) {
  43.       String vmId = errorIfNull("vbox.vm");
  44.       String guestUser = errorIfNull("vbox.guest.user");
  45.       String guestPass = errorIfNull("vbox.guest.pass");
  46.       String exec = errorIfNull("vbox.guest.exec");
  47.      
  48.       System.out.println("Starting");
  49.       VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
  50.       try {
  51.          if (System.getProperty("vbox.ws") != null) {
  52.             String host = System.getProperty("vbox.ws.host", "http://localhost:18083");
  53.             String user = System.getProperty("vbox.ws.user", "");
  54.             String pass = System.getProperty("vbox.ws.pass", "");
  55.             vboxManager.connect(host, user, pass);
  56.          }
  57.          System.out.println("Connected");
  58.          try {
  59.             ISession session = vboxManager.getSessionObject();
  60.             IMachine vm = vboxManager.getVBox().findMachine(vmId);
  61.             vm.lockMachine(session, LockType.Shared);
  62.             try {
  63.                System.out.println("Machine locked");
  64.                IGuestSession guestSess = session.getConsole().getGuest().createSession(guestUser, guestPass, null, null);
  65.                try {
  66.                   System.out.println("Session created");
  67.  
  68.                   guestSess.waitFor(1L, 30 * 1000L);
  69.                   if (!guestSess.getStatus().equals(GuestSessionStatus.Started)) {
  70.                      throw new RuntimeException("Guest session did not start after 30 sec");
  71.                   }
  72.                   IGuestProcess process = guestSess.processCreate(exec, null, null, Arrays.asList(ProcessCreateFlag.WaitForStdOut), 0L);
  73.  
  74.                   IEventSource es = process.getEventSource();
  75.                   IEventListener el = es.createListener();
  76.                   es.registerListener(el, Arrays.asList(VBoxEventType.Any), false);
  77.  
  78.                   try {
  79.                      System.out.println("Guest process created");
  80.                      ProcessWaitResult pwr = process.waitFor((long) ProcessWaitForFlag.Start.value(), 30 * 1000L);
  81.                      System.out.println("Process wait result: " + pwr);
  82.                      boolean keepLooping = true;
  83.                      do {
  84.                         IEvent ev = es.getEvent(el, 200);
  85.                         if (ev != null) {
  86.                            es.eventProcessed(el, ev);
  87.                         }
  88.                         /* This is how you should normally do it, but waiting for Stdout is not currently implemented - see http://hyperbox.altherian.org/kb/guessProcessHandling.txt
  89.                         ProcessWaitResult wr = process.waitForArray(Arrays.asList(ProcessWaitForFlag.StdOut, ProcessWaitForFlag.Terminate), 200L);
  90.                         System.out.println("Process wait result: " + wr);
  91.                          */
  92.                         byte[] stdOut = process.read(1L, 64L, 0L);
  93.                         System.out.print(new String(stdOut));
  94.                         keepLooping = !process.getStatus().toString().contains("Terminated");
  95.                      } while (keepLooping);
  96.                      System.out.println("Process exit code: " + process.getExitCode());
  97.                   } finally {
  98.                      es.unregisterListener(el);
  99.                      if (!process.getStatus().toString().contains("Terminated")) {
  100.                         process.terminate();
  101.                      }
  102.                   }
  103.                } finally {
  104.                   System.out.println("Session close");
  105.                   guestSess.close();
  106.                }
  107.             } catch (Throwable t) {
  108.                t.printStackTrace();
  109.             } finally {
  110.                System.out.println("Machine unlock");
  111.                session.unlockMachine();
  112.             }
  113.          } finally {
  114.             if (System.getProperty("vbox.ws") != null) {
  115.                vboxManager.disconnect();
  116.             }
  117.             System.out.println("Disconnected");
  118.          }
  119.       } finally {
  120.          vboxManager.cleanup();
  121.          System.out.println("Closing");
  122.       }
  123.    }
  124.    
  125. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×