C# .net Core run bash commands

Viewed 2494

I am trying to emulate terminal functionality(i.e QTerminal, LxTerminal) in my C#.net Core 2.0 application I am writing. (the application will eventually be responding to messages received over the internet)

The flow of the app would be:

  1. Receive "StartShell" message via TCP.
  2. .net Core app starts a shell process.
  3. Some time later receive a "ShellInput" message via TCP.
  4. .net Core app writes that command to the process's StandardInput
  5. process output data, which the .net Core app sends out via TCP.
  6. Steps 3-5 repeat until a "StopShell" message is received via TCP, at which point the .net Core app stops the shell process.

the ShellInputs would be anything you would normally type into a linux terminal, e.g.

mkdir

sudo mkdir -p folder

With that sudo command, the password prompt should come back, at which point the user would send a ShellInput message with their password.

On linux I am getting the shell path via

Environment.GetVariable("SHELL");

and then trying to use System.Diagnostics.Process to run that shell.

The problem I'm having is that I cannot seam to keep a shell open and have it correctly accept normal terminal inputs.

Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = Environment.GetEnvironmentVariable("SHELL");
process.StartInfo.Arguments = "-s"

process.ErrorDataReceived += Process_ErrorDataReceived;
process.OutputDataReceived += Process_OutputDataReceived;
process.Exited += Process_Exited;
process.EnableRaisingEvents = true;
if (process.Start())
{
    process.BeginErrorReadLine();
    process.BeginOutputReadLine();
    process.StandardInput.WriteLine("sudo mkdir /home/test");
}

The above code "works" in that it executes, but in my Process_ErrorDataReceived handler I get the message

sudo: no tty present and no askpass program specified

And googling that error just returns a whole bunch of people asking for ssh, which doesn't help.

0 Answers
Related