8bitcat
8bitcat

Reputation: 2234

C# - SSH Winforms Emulate console

I know this questions ahs been asked a lot but I can't seem to fidn a good way to start.

I have been using sharpssh, but it's made for a console application. Therefore when i try to create a winforms application I cant make it run the way I want.

In my form I have: A textbox which shows the ouput A textbox where the user should writes the command.

Im stuck at the readline(), I need to PAUSE the application until the user hit's enter and then send the command inserted in the textbox. I dont know how to convert the application into a winform application.

So the question is how to go about this? - Should I use processes one that starts when initializing the application, and one that starts when output is recieved and input is required? And use the second process to collect the input listen for the event enter key pressed in the textbox and the start the other process to send the input and wait for output again?

If so any have an example on how to solve it?

Here's the code?

    public Form1()
    {
        InitializeComponent();

        txthost.Text = "XXX";
        txtuser.Text = "XXXXX";
        txtpass.Text = "XXXXX";
        string pattern = "sdf:";
        mPattern = pattern;
        this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(checkforenter);
    }

    public void button1_Click(object sender, EventArgs e)
    {

        try
        {

            mShell = new SshShell(Host, User);
            mShell.Password = Pass;
            //WRITING USER MESSAGE
            txtOutput.AppendText("Connecting...");
            mShell.Connect();
            txtOutput.AppendText("OK");
            //txtOutput.AppendText("Enter a pattern to expect in response [e.g. '#', '$', C:\\\\.*>, etc...]: ");
            //Stop for user input

            mShell.ExpectPattern = mPattern;
            mShell.RemoveTerminalEmulationCharacters = true;
            _writer = new TextBoxStreamWriter(txtOutput);

            Console.SetOut(_writer);

            StringReader reader = new StringReader(txtInput.Text);

            while (mShell.ShellOpened)
            {
                txtOutput.AppendText("\r\n" + "TERMINAL MODE ENGAGED");
                txtOutput.AppendText(mShell.Expect( pattern ));

                txtInput.Text = Console.ReadLine();
                Console.ReadLine();
                Console.WriteLine(Environment.NewLine);
                txtOutput.AppendText(mShell.Expect(pattern));
                MessageBox.Show("innan enter 2");
                Console.WriteLine(Environment.NewLine);
                txtOutput.AppendText(mShell.Expect(("(continue)")));
                MessageBox.Show("efter enter 2");
                Console.WriteLine(Environment.NewLine);
                //Data from termninal --> Append to text
                string output = mShell.Expect(Pattern);
                txtOutput.AppendText(output);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

Upvotes: 1

Views: 3567

Answers (1)

Thraka
Thraka

Reputation: 2164

According to the SharpSSH article on Codeproject (http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET) there are two methods for you to read and write to the SSH connection:

//Writing to the SSH channel
ssh.Write( command );

//Reading from the SSH channel
string response = ssh.ReadResponse();

Your textbox becomes your console so you don't need to use the Console object anymore. You need to detect when the Enter key is pressed. When pressed, do the Write command and send what was last typed into the SSH connection. Then do a ReadResponse which will pause the application and wait for the response. Once it comes back, you can append the resulting string to the textbox.

You will probably be worried about detecting just what the user entered and sending that to the SSH connection. What you can do is everytime you do a ReadResponse, get the character index in the textbox and then use that with where the character is after they press Enter.

Or you can just use two textboxes, one for input and one for output.

Hope this helps!

Upvotes: 5

Related Questions