public void TestRunCommandStdOutInGuest() { foreach (VMWareVirtualMachine virtualMachine in _test.PoweredVirtualMachines) { Shell guestShell = new Shell(virtualMachine); string guid = Guid.NewGuid().ToString(); StringBuilder commands = new StringBuilder(); commands.AppendLine(string.Format("echo {0}", guid)); Shell.ShellOutput output = guestShell.RunCommandInGuest(commands.ToString()); Assert.AreEqual(guid, output.StdOut.Trim()); Assert.IsTrue(string.IsNullOrEmpty(output.StdErr)); } }
public string GetHostname() { try { LoginTools(); Shell guestShell = new Shell(VM.VM); //TODO: mock? Shell.ShellOutput output = guestShell.RunCommandInGuest("hostname"); return output.StdOut.Replace("\n", "").Replace("\r", ""); } catch (Exception) { return null; } }
public void SetIP(string value) { if (value.Length < 7) throw new InvalidDataException("IP too short"); Shell.ShellOutput output = new Shell.ShellOutput(); LoginTools(true); Shell guestShell = new Shell(VM.VM); // TODO: mock? string cmd = "netsh interface ip set address " + AppConfiguration.GetNetworkInterfaceName() + " static " + value + " 255.255.255.0"; output = guestShell.RunCommandInGuest(cmd); // Depending on OS, should print "Ok.\n\n" or not print any output if success if (output.StdOut.Length < 12) { return; } else if (output.StdOut.Contains("failed")) { throw new InvalidOperationException(cmd + "\n" + output.StdOut); } else { throw new InvalidProgramException(cmd + "\n" + output.StdOut); } }
public void SetHostname(string hostname) { if (hostname.Length < 3) throw new ArgumentException("Hostname too short"); Shell.ShellOutput output = new Shell.ShellOutput(); LoginTools(true); var renameScriptHost = AppConfiguration.GetWebserverTmpPath() + "renamecomp.vbs"; if (!File.Exists(renameScriptHost)) { if (!Directory.Exists(AppConfiguration.GetWebserverTmpPath())) Directory.CreateDirectory(AppConfiguration.GetWebserverTmpPath()); //if (!File.Exists(renameScriptHost)) //File.Create(renameScriptHost); File.WriteAllText(renameScriptHost, @"Set objWMIService = GetObject(""Winmgmts:root\cimv2"") For Each objComputer in _ objWMIService.InstancesOf(""Win32_ComputerSystem"") Name = WScript.Arguments.Item(0) Return = objComputer.rename(Name,NULL,NULL) If Return <> 0 Then WScript.Echo ""rename-fail, error = "" & Err.Number Else WScript.Echo ""rename-succ"" End If Next "); } //note: Host means Webserver, NOT VMware server if (!VM.DirectoryExistsInGuest(@"C:\temp")) VM.CreateDirectoryInGuest(@"C:\temp"); if (!VM.FileExistsInGuest(@"C:\temp\renamecomp.vbs")) VM.CopyFileFromHostToGuest(renameScriptHost, @"C:\temp\renamecomp.vbs"); Shell guestShell = new Shell(VM.VM); //TODO: mock? output = guestShell.RunCommandInGuest(@"cscript c:\temp\renamecomp.vbs " + hostname); //output = guestShell.RunCommandInGuest(@"cscript "+Config.getWebserverVMPath()+@"\renamecomp.vbs " + newName); if (output.StdOut.Contains("rename-succ")) return; else if (output.StdOut.Contains("rename-fail")) throw new InvalidOperationException(output.StdOut); else throw new InvalidOperationException(output.StdOut); }