我想运行命令并在具有Linux操作系统的远程计算机上获得结果。我使用ssh .net库通过C#代码进行连接。我可以连接并运行一些命令,它不需要使用“sudo”。但我不知道如何运行需要由sudo运行的命令,因为运行后 - 例如 - “sudo iptables -L -n”我应该输入密码,我不知道如何在执行此命令后输入密码命令。如何通过sudo运行命令并通过ssh .net输入密码c#
这是我的代码:
private void connectingToLinuxHost(string ip, string username, string password, int port)
{
SshCommandLineRunner ssh = new SshCommandLineRunner(ip, username, password, port);
ssh.Connect();
string output1 = ssh.ExecuteCommand("ls");
ssh.ExecuteCommand("sudo iptables -L -n");
Thread.Sleep(1000);
string output2 = ssh.ExecuteCommand(password);
ssh.ExecuteCommand("sudo iptables -L -n \n");
Thread.Sleep(1000);
string output3 = ssh.ExecuteCommand(password);
}
时,此功能来看,我可以连接到远程PC成功,输出1有正确的值,但输出2和输出3是空的。实际上很明显,sudo命令后需要输入密码。 我读过关于ssh.net的大多数问题。一些类似的问题仍未得到答复。
SSH .NET : freeze upon sudo su - user2 (他或她不知道密码,但我知道,我不知道如何通过代码输入)
How to su with SSH.Net? (我用贝壳制作方法,但我不明白什么是和像这个问题我得到了“上次登录:...”输出。)
什么是'SshCommandLineRunner'?它是SSH.NET库的一部分吗?正常的方法是'CreateShellStream'来获取流并使用流的Expect和Write方法。 –