Advertisement
Guest User

Untitled

a guest
May 20th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import org.virtualbox_5_2.*;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.Stream;
  8.  
  9. /**
  10.  * Documentation: https://download.virtualbox.org/virtualbox/5.2.8/SDKRef.pdf
  11.  */
  12. public class Host {
  13.  
  14.     private static final String VIRTUAL_BOX_SERVER_URL = "http://localhost:18083";
  15.  
  16.     private static final String USER = "bohdana";
  17.     private static final String PASSWORD = "140993dana";
  18.  
  19.     private static final String MACHINE_TO_RUN = "Windows 8.1 (64 - bit)";
  20.     private static final String SNAPSHOT_TO_START = "automation_start";
  21.     private static final String GUEST_USER = "DanaKuzub";
  22.     private static final String GUEST_PASSWORD = "140993dana";
  23.  
  24.     private static final String BUILD_URL = "https://aegis-win.s3.amazonaws.com/setup-aegis-2.4.4-267-x86_64-offline.exe";
  25.  
  26.     public static void main(String[] args) throws IOException, InterruptedException {
  27.         final Process process = new ProcessBuilder("C:\\Program Files\\Oracle\\VirtualBox\\VBoxWebSrv.exe").start();
  28.         final VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
  29.         mgr.connect(VIRTUAL_BOX_SERVER_URL, USER, PASSWORD);
  30.  
  31.         final IVirtualBox virtualBox = mgr.getVBox();
  32.         final ISession session = mgr.getSessionObject();
  33.  
  34.         List<IMachine> machines = virtualBox.getMachines();
  35.         for (IMachine machine : machines) {
  36.             System.out.println(String.format("Name: %s; OS: %s; State: %s", machine.getName(), machine.getOSTypeId(), machine.getState()));
  37.         }
  38.  
  39.         final IMachine machine = virtualBox.findMachine(MACHINE_TO_RUN);
  40.  
  41.         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  42.             if (machine.getState() == MachineState.Running) {
  43.                 IProgress prog = session.getConsole().powerDown();
  44.                 prog.waitForCompletion(10000);
  45.                 session.unlockMachine();
  46.             } else {
  47.                 System.out.println(MACHINE_TO_RUN + " is already powered off");
  48.             }
  49.             mgr.cleanup();
  50.             process.destroy();
  51.         }));
  52.  
  53.         ISnapshot snapshot = machine.findSnapshot(SNAPSHOT_TO_START);
  54.         machine.lockMachine(session, LockType.Shared);
  55.         machine.restoreSnapshot(snapshot).waitForCompletion(-1);
  56.         IProgress vmProcess = session.getMachine().launchVMProcess(session, "gui", null);
  57.         vmProcess.waitForCompletion(-1);
  58.  
  59.         IGuestSession guestSession = session.getConsole().getGuest().createSession(GUEST_USER, GUEST_PASSWORD, "", "");
  60.         guestSession.waitFor(1L, 60 * 1000L);
  61.         System.out.println("Guest session is created");
  62.  
  63.         guestSession.directoryCreate("C:\\Users\\DanaKuzub\\Desktop\\automation_resources\\", 0L, new ArrayList<>());
  64.         guestSession.fileCopyToGuest("C:\\Users\\Bohdana\\Downloads\\test_for_automation.txt", "C:\\Users\\DanaKuzub\\Desktop\\automation_resources\\", new ArrayList<FileCopyFlag>());
  65.  
  66.         IGuestProcess guestProcess = guestSession.processCreate(
  67.                 "C:\\Windows\\system32\\cmd.exe",
  68.                 new ArrayList<>(),
  69.                 new ArrayList<>(),
  70.                 Stream.of(ProcessCreateFlag.WaitForProcessStartOnly).collect(Collectors.toList()),
  71.                 0L
  72.         );
  73.         guestProcess.waitFor(1L, 60 * 1000L);
  74.         System.out.println("CMD pid: " + guestProcess.getPID());
  75.  
  76.         Thread.sleep(60 * 1000);
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement