[Renci.SshNet]SSH a linux by and send "su - root",Execute() function does not return
i am a newer.Any help will be greatly appreciated. how to use Renci.SshNet provide function to send an interactive command to linux: for example: an ossuser(not root user) login linux. And then I use these codes below to switch root user,but cmd.Execute() does not return. I need a way to switch root because my os forbid directly logging in by root. var cmd = client.CreateCommand("su - root"); cmd.Execute(); cmd = client.CreateCommand("Changeme_123"); cmd.Execute();
Does anybody know,please give me some help.Any help will be greatly appreciated.
@xrayle You have to use ShellStream together with Expect. Sample code for inspiration:
private static void WriteStream(string cmd, ShellStream stream)
{
stream.WriteLine(cmd + "; echo this-is-the-end");
while (stream.Length == 0)
Thread.Sleep(500);
}
private static string ReadStream(ShellStream stream)
{
StringBuilder result = new StringBuilder();
string line;
while ((line = stream.ReadLine()) != "this-is-the-end")
result.AppendLine(line);
return result.ToString();
}
private static void SwithToRoot(string password, ShellStream stream)
{
// Get logged in and get user prompt
string prompt = stream.Expect(new Regex(@"[$>]"));
//Console.WriteLine(prompt);
// Send command and expect password or user prompt
stream.WriteLine("su - root");
prompt = stream.Expect(new Regex(@"([$#>:])"));
//Console.WriteLine(prompt);
// Check to send password
if (prompt.Contains(":"))
{
// Send password
stream.WriteLine(password);
prompt = stream.Expect(new Regex(@"[$#>]"));
//Console.WriteLine(prompt);
}
}
using (var client = new SshClient(server, username, password))
{
client.Connect();
List<string> commands = new List<string>();
commands.Add("pwd");
commands.Add("cat /etc/sudoers");
ShellStream shellStream = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);
// Switch to root
SwithToRoot("rootpassword", shellStream);
// Execute commands under root account
foreach (string command in commands) {
WriteStream(command, shellStream);
string answer = ReadStream(shellStream);
int index = answer.IndexOf(System.Environment.NewLine);
answer = answer.Substring(index + System.Environment.NewLine.Length);
Console.WriteLine("Command output: " + answer.Trim());
}
client.Disconnect();
}
I tried this but it didn't work for returning the MySQL prompt, the application just hangs.
@kasbst How can the user be switched in the SFTP session?
In version 2024.0.0 we've added CreateInputStream() on SshCommand. I think it should work
var inputByteArray = Encoding.UTF8.GetBytes("Changeme_123");
using (var command = _sshClient.CreateCommand("su - root"))
{
var asyncResult = command.BeginExecute();
using (var inputStream = command.CreateInputStream())
{
inputStream.Write(inputByteArray, 0, inputByteArray.Length);
}
command.EndExecute(asyncResult);
}
Or you can use ShellStream as in command https://github.com/sshnet/SSH.NET/issues/136#issuecomment-297710063
I tried this but it didn't work for returning the MySQL prompt, the application just hangs.
I'm using Ubuntu and the code needed some adjustment (I'm using sudo instead of su)
private static void WriteStream(string cmd, ShellStream stream)
{
stream.WriteLine(cmd + "; echo this-is-the-end");
while (stream.Length == 0)
Thread.Sleep(500);
}
private static string ReadStream(ShellStream stream)
{
StringBuilder result = new StringBuilder();
string line;
while (!(line = stream.ReadLine()).EndsWith("\rthis-is-the-end"))
result.AppendLine(line);
return result.ToString();
}
private static void SwithToRoot(string password, ShellStream stream)
{
// Get logged in and get user prompt
string prompt = stream.Expect(new Regex(@"[$>]"));
//Console.WriteLine(prompt);
// Send command and expect password or user prompt
stream.WriteLine("sudo ls > null");
prompt = stream.Expect(new Regex(@"([$#:])"));
//Console.WriteLine(prompt);
// Check to send password
if (prompt.Contains(":"))
{
// Send password
stream.WriteLine(password);
prompt = stream.Expect(new Regex(@"[$#>]"));
//Console.WriteLine(prompt);
}
}
private static string ExecuteSudoCommand(string command, ShellStream stream)
{
WriteStream(command, stream);
return ReadStream(stream);
}
In 2024.0.0 version ShellStream should work on linux correctly.