VBoxManage doesn't redirect standard input
Posted: 5. Jun 2017, 19:51
About
My host is Windows 10 and my guest is Ubuntu. I want to run a Python script within the guest machine for a host/guest communication via Standard I/O. The standard output of the Python script is shown within the Windows 10 host but the problem is that I am not able to write anything to the standard input of the guest machine. Therefore I built a testing environment which emulates the behaviour described in the VirtualBox manual (highlighting added by myself):
The testing environment consists of the following two programs:
VM Behaviour Emulator
Conclusion
While the testing environment works as expected (it can pass data to VM Behaviour Emulator through standard input) while there is a problem when calling "VBoxManage guestcontrol ... run ...". It simply doesn't recognize the data written through standard input. The same behaviour shows when trying to run "VBoxManage guestcontrol ... run ..." from the host's command line.
My host is Windows 10 and my guest is Ubuntu. I want to run a Python script within the guest machine for a host/guest communication via Standard I/O. The standard output of the Python script is shown within the Windows 10 host but the problem is that I am not able to write anything to the standard input of the guest machine. Therefore I built a testing environment which emulates the behaviour described in the VirtualBox manual (highlighting added by myself):
Testing Environmentrun Executes a guest program - forwarding stdout, stderr and stdin to/from the host until it completes.
- Source: VirtualBox Manual (cannot post links yet)
The testing environment consists of the following two programs:
- VM Control: In the testing environment the VM Behaviour Emulator is called here. VM Control would normally call "VBoxManage guestcontrol ... run ..."
- VM Behaviour Emulator: When this program is called by VM Control a line is getting written to the standard output. If VM Control writes something to the standard input afterwards VM Behaviour Emulator will echo the input to the standard output.
Code: Select all
using System;
using System.Diagnostics;
using System.IO;
namespace VM_Control
{
class Program
{
private static StreamWriter vmComInput = null;
static void Main(string[] args)
{
// Process instance for executable
Process vmCom = new Process();
// Path to executable
vmCom.StartInfo.FileName = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory,
@"..\..\..\VM Behaviour Emulator\bin\Debug\VM Behaviour Emulator.exe"));
// Process configuration
vmCom.StartInfo.UseShellExecute = false;
vmCom.StartInfo.CreateNoWindow = true;
vmCom.StartInfo.RedirectStandardError = true;
vmCom.StartInfo.RedirectStandardInput = true;
vmCom.StartInfo.RedirectStandardOutput = true;
// Setup EventHandler
vmCom.OutputDataReceived += new DataReceivedEventHandler(VMComOutputHandler);
// Start the executable in a separate process
vmCom.Start();
// Enable OutputDataReceived events
vmCom.BeginOutputReadLine();
// Link local StreamWriter instance to processes StandardInput
vmComInput = vmCom.StandardInput;
// Wait until the process finished executing
vmCom.WaitForExit();
}
private static void VMComOutputHandler(object sendingProcess,DataReceivedEventArgs line)
{
if (!String.IsNullOrEmpty(line.Data))
{
// Print received StandardOutput line
Console.WriteLine(line.Data);
// Provide some input through the StandardInput StreamWriter
if (line.Data == "Enter something: ")
vmComInput.WriteLine("... an input string entered by a StreamWriter instance.");
// Another input through the StandardInput StreamWriter would close the application
// at this point
else if (line.Data == "Press enter to quit the application")
Debug.WriteLine("Process finished");
}
}
}
}Code: Select all
using System;
namespace VM_Behaviour_Emulator
{
class Program
{
static void Main(string[] args)
{
string input = "";
// Prompt user for input
Console.WriteLine("Enter something: ");
input = Console.ReadLine();
// Echo user's input
Console.WriteLine("You entered: {0}", input);
// Wait until user quits the application
Console.WriteLine("Press enter to quit the application");
Console.ReadLine();
}
}
}While the testing environment works as expected (it can pass data to VM Behaviour Emulator through standard input) while there is a problem when calling "VBoxManage guestcontrol ... run ...". It simply doesn't recognize the data written through standard input. The same behaviour shows when trying to run "VBoxManage guestcontrol ... run ..." from the host's command line.