title: Execute a Command on a Remote Server That Requires Sudo with password input - Paramiko
body: <p>I am trying to execute a sudo command on a remote machine using python-paramiko, (Python 2.7.9 on linux2)
  Below is the code. when I execute the code it's giving different output each time, whereas its working fine when I run the same code in python>>> cmdline</p>
  
  <pre><code>import paramiko
  import sys
  import time
  def send_string_and_wait(command, wait_time, should_print):
     shell.send(command)
     time.sleep(wait_time)
     receive_buffer = shell.recv(1024)
     if should_print:
       return  receive_buffer
  
  dbname='test'
  cl='testdb'
  host='testhost'
  owner='uname'
  passwd='p'
  
  
  client = paramiko.SSHClient()
  client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  client.connect(str(host), username=str(owner),  password=str(passwd), port=22)
  shell = client.invoke_shell()
  send_string_and_wait("sudo su - oracle\n", 1, True)
  send_string_and_wait(str(passwd) + "\n", 1, True)
  a=send_string_and_wait("sh Validation_Final.sh" + str(' ') +  str(dbname) + str(' ') + str(cl) + "\n", 0, True)
  print a
  client.close() 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/R2omj.png" rel="nofollow noreferrer">sample output</a></p>
  
  <p>Any advise and suggestions will be greatly appreciated, Thank you!</p>