I want to run command and get the result on remote computer which has Linux operating system. I am using ssh .net library to connect by C# code. I can connect and run some commands that it doesn't need to use "sudo" before. but I don't know How to run commands which need to be run by sudo, because after run -for instance- "sudo iptables -L -n" I should enter password and I don't know How to enter password after execute this command.
This is my code :
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);
}
when this function run, I can connect to remote pc successfully, output1 has correct value, but output2 and output3 are empty. actually it is obvious that after sudo command it is needed to enter password. I have read most question about ssh .net. some similar question has retain unanswered.
SSH .NET : freeze upon sudo su - user2 ( he or she didnt know the password but I know I dont know how to enter it via code)
How to su with SSH.Net? (I used create shell method but i couldn't understand what is for and like this question I got "Last login: ..." output.)