protected execFileSet prepareForExecution(string toExecute, string args, string tempDir) { // We do this by creating two batch files on the target. // The first contains the command we're executing, and the second simply calls the first with redirection to the files // we want our output in. This simplifies escaping on the commandline via psexec. using (temporaryFile payloadBatch = new temporaryFile(".bat")) using (temporaryFile launcherTemp = new temporaryFile(".bat")) { string launcherRemotePath = tempDir + Guid.NewGuid() + "_launcher.bat"; string payloadRemotePath = tempDir + Guid.NewGuid() + "_payload.bat"; // Assemble the 'launcher' file, which calls the payload. We give std/out/err randomly-generated filenames here. string stdOutFilename = string.Format(tempDir + "\\hyp_stdout_" + Guid.NewGuid()); string stdErrFilename = string.Format(tempDir + "\\hyp_stderr_" + Guid.NewGuid()); string returnCodeFilename = string.Format(tempDir + "\\hyp_retcode_" + Guid.NewGuid()); string launchFileContents = String.Format( "@call \"{0}\" 1> {2} 2> {3} \r\n" + "@echo %ERRORLEVEL% > {1}", payloadRemotePath, returnCodeFilename, stdOutFilename, stdErrFilename); launcherTemp.WriteAllText(launchFileContents); // And the payload batch. payloadBatch.WriteAllText(string.Format("@{0} {1}", toExecute, args)); // Then, copy them to the guest. withRetryUntilSuccess(() => copyToGuest(launcherRemotePath, launcherTemp.filename), new cancellableDateTime(TimeSpan.FromMinutes(10))); withRetryUntilSuccess(() => copyToGuest(payloadRemotePath, payloadBatch.filename), new cancellableDateTime(TimeSpan.FromMinutes(10))); // Now return info about what files we're going to use, so the caller can.. use them. return(new execFileSet(stdOutFilename, stdErrFilename, returnCodeFilename, launcherRemotePath)); } }
public static temporaryFile wrapExistingFile(string tempFilename) { temporaryFile toRet = new temporaryFile(false); toRet.filename = tempFilename; return(toRet); }
public void copyToGuestFromBuffer(string dstpath, byte[] srcContents) { using (temporaryFile tmpFile = new temporaryFile()) { using (FileStream tmpFileStream = new FileStream(tmpFile.filename, FileMode.OpenOrCreate)) { tmpFileStream.Write(srcContents, 0, srcContents.Length); tmpFileStream.Seek(0, SeekOrigin.Begin); // ?! } copyToGuest(dstpath, tmpFile.filename); } }