0

I am trying to make a program that connects to an SSH server and then logs me in. I have successfully created tracert or ping commands, but SSH connection doesn't work for me at all. I am using SSH.NET. I get no output to my text box. I already followed some advice from StackOverflow (Execute long time command in SSH.NET and display the results continuously in TextBox), but it did not work for me.

Here's the code:

private void button5_Click(object sender, EventArgs e)
{
    string host = "someserverip";
    string username = textBox6.Text;
    string password = textBox8.Text;
    string ipaddress = textBox12.Text;

    using (var radius = new SshClient(host, port, username, password))
    {
        radius.Connect();
        var cmd = radius.CreateCommand("check-host" + ipaddress);
        var result = cmd.BeginExecute();

        using (var reader = new StreamReader(
              cmd.OutputStream, Encoding.UTF8, true, 1024, true))
        {
            while (!result.IsCompleted || !reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (line != null)
                {
                    textBox10.Invoke(
                        (Action)(() =>
                            textBox10.AppendText(line + Environment.NewLine)));
                }
            }
        }
        cmd.EndExecute(result);
    }
    textBox10.ScrollBars = ScrollBars.Vertical;
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0

Something probably goes wrong with your command and as you are not reading the error output, you won't know.

  • Try reading the ExtendedOutputStream to see any error output the command might produce.

  • To ease debugging you can (temporarily) merge both streams into one using 2>&1 shell syntax.


Though in your code, you miss a space between command name and its arguments. It should rather be

"check-host " + ipaddress
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992