title: How to run commands by sudo and enter password by ssh .net c#
body: <p>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 <strong>"sudo"</strong> 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.</p>
  
  <p>This is my code :</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><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);
  }
  </code></pre>
  
  <p>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. </p>
  
  <p><a href="https://stackoverflow.com/questions/16560144/ssh-net-freeze-upon-sudo-su-user2">SSH .NET : freeze upon sudo su - user2</a>
  ( he or she  didnt know the password but I know I dont know how to enter it via code)</p>
  
  <p><a href="https://stackoverflow.com/questions/40918265/how-to-su-with-ssh-net">How to su with SSH.Net?</a>
  (I used  create shell method but i couldn't understand what is for and like this question I got  "Last login: ..." output.)</p>