0

Using .NET Core 2.0 on a Linux Ubuntu I want to start a bash shell using the Process class, redirect StandardInput and StandardOutput and want to enter commands and receive the results.

This works great for commands like cd and dir.

When I want to execute a command with sudo, the bash hangs, however, and waits for the input of a password. I guess the same would happen for commands that require a confirmation by Yes or No.

Is there any way to enter the password or a confirmation like Yes or No from my application?

NicolasR
  • 2,222
  • 3
  • 23
  • 38

1 Answers1

2

Option 1 is, drop the 'netcore' tag from your search and look at pure bash-based answers like Is there a way to input automatically when running a shell?

Option 2, is there, or can you create, a username/password you can use that doesn't require sudo? If so then the right Process.Start() overload might do the trick? I hoped that there might be something on ProcessInfo class to request elevated privileges. On Windows platforms, using Verb="runas" does it but will invoke UAC.

Options 3: Forget sudo and look at other Linux options such as creating an executable with setuid set on it. Look at e.g. https://serverfault.com/questions/262809/correct-way-to-give-elevated-privileges-to-mono-application for some thoughts.

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
  • Very helpful options. Following option 1 I found the interesting (but controversial) command "echo TopSecretPassword | sudo -S whatever". I'm not yet sure what will work best. Needs more testing... – NicolasR Mar 23 '18 at 00:35