0

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.

venkat14
  • 583
  • 3
  • 12
  • 34

2 Answers2

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.

TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
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.

cosmin_popescu
  • 186
  • 4
  • 16
  • I tried the suggested answer from https://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net#437444 , but i do not see a command prompt box opening and the commands being added. what could be the issue – venkat14 Nov 29 '18 at 07:44