3

Whenever I try to execute shell commands, I get an error saying

/bin/hostname: /bin/hostname: cannot execute binary file
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: Index was outside the bounds of the array.

I currently have

public MachineData FindHostname()
{
        Thread.Sleep(3000);
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "/bin/bash";
        psi.Arguments = "/bin/hostname && /bin/uname";
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;

        Process proc = new Process
        {
            StartInfo = psi
        };

        proc.Start();
        ...
}

Does anyone know of a workaround?

7
  • 1
    If you try it from command line you'll see it doesn't work and has nothing to do with ASP.NET. You need to give bash the -c flag to run commands Commented Aug 15, 2016 at 20:24
  • obvious mistake. thanks, i'll try that out. Commented Aug 15, 2016 at 20:59
  • 1
    You can also use the UseShellExecute = true setting. It can be used for exactly this kind of scenario; wanting to run a command through the shell... :) Commented Jan 21, 2017 at 12:48
  • I'm sorry but how will it work. I didn't get it. Any elaborated answer. In my case I'm providing -c but it still gives an error Commented Aug 16, 2017 at 10:13
  • Hi, Haseeb. What error are you getting? Commented Aug 18, 2017 at 4:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.