Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- namespace VirtualBoxManager
- {
- class VirtualBoxManager
- {
- private String VMName;
- private String virtualBoxManage;
- public VirtualBoxManager(String VMName, String VirtualBoxManage)
- {
- this.VMName = VMName;
- this.virtualBoxManage = VirtualBoxManage;
- }
- private String launchVBoxManager(String args, String attendedOutput)
- {
- Process VBoxManage = new Process();
- VBoxManage.StartInfo.CreateNoWindow = true;
- VBoxManage.StartInfo.FileName = this.virtualBoxManage + "VBoxManage.exe";
- VBoxManage.StartInfo.WorkingDirectory = this.virtualBoxManage;
- VBoxManage.StartInfo.Arguments = args;
- VBoxManage.StartInfo.UseShellExecute = false;
- //We redirect output only if the user supply any attended output
- if (attendedOutput != "")
- {
- VBoxManage.StartInfo.RedirectStandardOutput = true;
- }
- VBoxManage.Start();
- //If we are waiting for an output, we wait until the end of the output
- if (attendedOutput != "")
- {
- String line = "";
- while (!VBoxManage.StandardOutput.EndOfStream)
- {
- System.Windows.Forms.Application.DoEvents();
- line = VBoxManage.StandardOutput.ReadToEnd();
- if (line.Trim().StartsWith(attendedOutput))
- {
- return line.Trim();
- }
- }
- }
- return "-1";
- }
- private String launchVBoxManager(String args)
- {
- return this.launchVBoxManager(args, "");
- }
- public void startVM()
- {
- launchVBoxManager(" startvm \"" + this.VMName + "\" --type headless");
- }
- public void stopVM()
- {
- launchVBoxManager(" controlvm \"" + this.VMName + "\" poweroff");
- }
- public String ipVM()
- {
- //Checking IP
- String ip = " guestproperty get \"" + this.VMName + "\" \"/VirtualBox/GuestInfo/Net/0/V4/IP\"";
- String tmpIP = launchVBoxManager(ip, "Value");
- if (tmpIP != "-1")
- {
- return tmpIP.Substring(7);
- }
- else
- {
- return "0.0.0.0";
- }
- }
- public Boolean statusVM(String login, String password)
- {
- String checkStatus = " guestcontrol \"" + this.VMName + "\" execute --image \"/bin/uname\" --username \"" + login + "\" --password \"" + password + "\" --wait-stdout";
- String checkStatusResult = "Linux";
- //Return the boolean result
- return (launchVBoxManager(checkStatus, checkStatusResult) == checkStatusResult);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement