I am trying to write a program that will allow me to interact with a remote database that is on the local server within an Ubuntu Virtual Machine. Below are the commands I can run in the windows terminal to connect to the database, and these are essentially the same commands I want to execute in the program in order to communicate with the database.
>ssh user@192.168.0.60
user@192.168.0.60's password: *********
>sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 101
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2019, 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 am able to establish the ssh connection easily, I can execute a command like ls
and see the contents of the current directory. But when I try to execute a command like sudo mysql
the program seems to enter a loop - I am guessing because it is prompted for the password but I don't know how to provide the password when it is prompted.
I'll add the connection code below. I really just need to be able to run select statements with the connection to retrieve data from the MySQL database.
using System;
using Renci.SshNet;
namespace DBconnector
{
class Program
{
private static string username = "user";
private static string password = "password";
private static string host = "192.168.0.60";
static void Main(string[] args)
{
SshClient client = new SshClient(host, 22, username, password);
client.Connect();
if (!client.IsConnected)
{
Console.WriteLine("Connection Error: Failed");
System.Environment.Exit(1);
}
SshCommand dir = client.RunCommand("...some command..."); // This is where I want to run 'sudo mysql'
// and here I need to be able to change DB and select from tables
Console.WriteLine(dir.Result);
}
}
}
Thank you!