1

I have a C# web application in which I want to execute Unix commands one by one. I want the command prompt window to appear and then after each command execution , move to the next command and then exit the window. I have tried the below, but the command prompt window does not appear and the commands are not getting executed after each wait

   public void connecterOne()
      {
      try
        {
            using (Process p = new Process())
            {
                // set start info
                p.StartInfo = new ProcessStartInfo("cmd.exe")
                {
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    WorkingDirectory = @"c:\"
                };
                // event handlers for output & error
                p.OutputDataReceived += p_OutputDataReceived;
                p.ErrorDataReceived += p_ErrorDataReceived;

                // start process
                p.Start();
                // send command to its input
                p.StandardInput.Write("ftp server" + p.StandardInput.NewLine);
               p.StandardInput.Write("username" + p.StandardInput.NewLine);
       p.StandardInput.Write("pwd" + p.StandardInput.NewLine);
                string output = p.StandardOutput.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        Process p = sender as Process;
        if (p == null)
            return;
        Console.WriteLine(e.Data);
    }

    static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Process p = sender as Process;
        if (p == null)
            return;
        Console.WriteLine(e.Data);
    }

How to execute commands one by one and have the command prompt window open which shows the commands being executed. I am not getting any errors, but there is no output seen.

4
  • You want to execute those commands on the server, right? Commented Nov 29, 2018 at 6:30
  • 1
    I think something wrong.. cmd.exe on unix? that sound awkward.. you mean window server or what?? Commented Nov 29, 2018 at 6:31
  • i want the execute the commands from cmd Commented Nov 29, 2018 at 6:37
  • From a comment below: "but i do not see a command prompt box opening and the commands being added" - why would you? where do you expect to see this? On the ASP.NET server machine? Commented Nov 29, 2018 at 8:30

2 Answers 2

1

This is a security issue A web application has no access to user pc command prompt , A web application deployed on IIS on web server not on user pc.

If you run a process it will execute on that server but you need to allow server to execute these commands.But I think it is bad practice.

Sign up to request clarification or add additional context in comments.

Comments

0

To execute a command on a remote Linux based machine, you need to connect first with ssh. For C#, you can use the SSH.NET

Also, this article might help you.

1 Comment

I tried the suggested answer from stackoverflow.com/questions/437419/… , but i do not see a command prompt box opening and the commands being added. what could be the issue

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.