title: Running a python script on a remote server with sudo privileges
body: <p>I got to know about python pexcept package to solve my problem of running python script on remote server as root, using the following code:</p>
  <pre><code>import pexpect
  
  # Set the username for ssh connection
  username = 'zod1@&lt;remote-server-ip&gt;'
  
  # Take a valid password from the user
  Password = input(&quot;Enter the login password of %s: &quot; %username)
  
  # Run ssh command using spawn
  child = pexpect.spawn('ssh ' + username)
  
  # Wait for the password
  child.expect('password:')
  
  # Send the password taken from the user
  child.sendline(Password)
  
  # Expected three output
  i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])
  
  # i will be 0 if ssh is unable to connect
  if i == 0:
      print(&quot;Permission denied by host. Unable to login&quot;)
      child.kill(0)
  
  # i will be 1 if ssh is able to connect but terminal is not set
  elif i == 1:
      print('Connected Successfully.\nTerminal type is not set.')
      child.sendline('vt100')
      child.expect('[#\$]')
  
  # i will be 2 if ssh is able to connect and terminal is set
  elif i == 2:
      print('Connected Successfully.')
      prompt = child.after
      print('Shell Command Prompt:', prompt.decode(&quot;utf-8&quot;))
  </code></pre>
  <p>till here everything works fine, but I need to execute the next parts of the script(not included here) as root user, for that i'm using:</p>
  <pre><code>pwd = input(&quot;super user password: &quot;)
  child = pexpect.spawn('sudo su')
  i = child.expect(['[sudo] password for zod1:']) 
  child.sendline(pwd)
  </code></pre>
  <p>which gives:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Users/ravi3.intern/Desktop/new.py&quot;, line 131, in &lt;module&gt;
      i = child.expect(['[sudo] password for zod1:']) 
    File &quot;/opt/homebrew/lib/python3.9/site-packages/pexpect/spawnbase.py&quot;, line 343, in expect
      return self.expect_list(compiled_pattern_list,
    File &quot;/opt/homebrew/lib/python3.9/site-packages/pexpect/spawnbase.py&quot;, line 372, in expect_list
      return exp.expect_loop(timeout)
    File &quot;/opt/homebrew/lib/python3.9/site-packages/pexpect/expect.py&quot;, line 181, in expect_loop
      return self.timeout(e)
    File &quot;/opt/homebrew/lib/python3.9/site-packages/pexpect/expect.py&quot;, line 144, in timeout
      raise exc
  pexpect.exceptions.TIMEOUT: Timeout exceeded.
  &lt;pexpect.pty_spawn.spawn object at 0x100c36f40&gt;
  command: /usr/bin/sudo
  args: ['/usr/bin/sudo', 'su']
  buffer (last 100 chars): b'Password:'
  before (last 100 chars): b'Password:'
  after: &lt;class 'pexpect.exceptions.TIMEOUT'&gt;
  match: None
  match_index: None
  exitstatus: None
  flag_eof: False
  pid: 94531
  child_fd: 6
  closed: False
  timeout: 30
  delimiter: &lt;class 'pexpect.exceptions.EOF'&gt;
  logfile: None
  logfile_read: None
  logfile_send: None
  maxread: 1
  ignorecase: False
  searchwindowsize: None
  delaybeforesend: 0.05
  delayafterclose: 0.1
  delayafterterminate: 0.1
  searcher: searcher_re:
      0: re.compile(b'[sudo] password for zod1:'
  </code></pre>
  <p>which seems to getting stuck on child.expect part. I tested the same thing with 'date' instead of 'sudo su' which seems to be working fine with the above approach. So, can someone please help in debugging the error.. TIA!</p>