59 lines
1.9 KiB
Plaintext
59 lines
1.9 KiB
Plaintext
|
using System;
|
||
|
using System.IO;
|
||
|
using System.Text;
|
||
|
using System.Diagnostics;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
namespace AniNIX.TheRaven {
|
||
|
|
||
|
public static class RavenExecute {
|
||
|
|
||
|
/// <summary>
|
||
|
/// This method allows TheRaven to execute a command on the OS.
|
||
|
/// </summary>
|
||
|
/// <param name=command>The command string to run as the string argument to "bash -c 'command'"</param>
|
||
|
/// <param name=input>The effective replacement for the command's stdin</param
|
||
|
/// <return>The stdout of the command</return>
|
||
|
/// </summary>
|
||
|
public static String Command(String command, String input) {
|
||
|
//Sanitize inputs.
|
||
|
if (command.Contains("\'")) {
|
||
|
throw new Exception("Command strings cannot include \'.");
|
||
|
}
|
||
|
|
||
|
//Create process.
|
||
|
Process proc = new Process();
|
||
|
proc.StartInfo.CreateNoWindow = true;
|
||
|
proc.StartInfo.FileName = "/bin/bash";
|
||
|
proc.StartInfo.Arguments = String.Format("-c \'{0}\'",command);
|
||
|
proc.StartInfo.UseShellExecute=false;
|
||
|
|
||
|
//Redirect input
|
||
|
proc.StartInfo.RedirectStandardOutput=true;
|
||
|
proc.StartInfo.RedirectStandardInput=true;
|
||
|
|
||
|
//Start process
|
||
|
proc.Start();
|
||
|
|
||
|
//Add input and read output.
|
||
|
proc.StandardInput.Write(input);
|
||
|
proc.StandardInput.Close();
|
||
|
proc.WaitForExit();
|
||
|
if (proc.ExitCode != 0) {
|
||
|
throw new Exception(String.Format("Failed to exit command with return code {0}",proc.ExitCode));
|
||
|
}
|
||
|
String stdoutString = proc.StandardOutput.ReadToEnd();
|
||
|
|
||
|
//Close up and return
|
||
|
proc.Close();
|
||
|
return stdoutString;
|
||
|
}
|
||
|
|
||
|
//Add polymorphism to allow no stdin
|
||
|
public static String Command(String command) {
|
||
|
return Command(command,null);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|