0

Within my C# WPF app I am trying to access an SSH server, and script some commands. It works for simple commands such as ls, hostname and so on, but when I tried opening mysql, the program just hangs and I have to End Task. For some reason mysql isn't counting that as a command that returns a result. My code is:

using (var client = new SshClient(hostname, username, serverPassword))
{
    try
    {
        client.Connect();

        Console.WriteLine("Command>" + sshCommand);
        var run = client.CreateCommand(sshCommand);
        run.Execute();
        Console.WriteLine("Return Value = {0}", run.Result);
        client.Disconnect();
        returnedResults = run.Result;
    }
    catch
    {
        Console.WriteLine("Fail");
    }
}

When I log in to the server using putty and send the mysql command, I get this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 113147
Server version: 5.7.21-0ubuntu0.16.04.1-log (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

I would like to script each step in changing the Mysql password, but rather than just blasting commands at the server, have a check to make sure that each command got the expected result before sending the next command.

1 Answers1

0

It may be because mysql is not a "one-and-done" command but it goes into a mysql session and expects input from user, so it may not be returning.

Arthur Hylton
  • 131
  • 1
  • 7
  • What can I do about it? I have tried looking around for an interactive terminal package, but haven't found one. I tried using ShellStream (https://stackoverflow.com/questions/30883237/how-to-run-commands-on-ssh-server-in-c), which didn't work. – Alan Halls Jun 29 '18 at 17:58
  • 1
    ShellStream actually ended up working, but I had to add a delay of 500ms to get the results. – Alan Halls Jun 29 '18 at 18:19