title: Avoiding SSH Password prompt using Python's Subprocess library
body: <p>I'm building a program which sends commands to multiple servers with as less as possible user intervention.
  For this program, I use subprocess to send commands through SSH.</p>
  
  <p>The interacted servers are many and dynamics.
  The program will be used mainly by my team members in my company.
  Most of the servers have the right file at /root/.ssh/authorized_keys and the program works great with them.</p>
  
  <p>But some servers weren't installed well and we can't connect them using SSH without entering root password.
  The SSH password prompts hangs at the moment it needs to <code>ssh.stdout.readlines()</code> of the "problematic server".</p>
  
  <p>How can I make sure the program will ignore, skip and keep going with the code each time it faces a server that prompts for root password because of keys issue and won't hang on the password prompt?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#!/usr/bin/python
  from socket import socket
  import subprocess, os
  def run_command():
  ipList = input_ip_addresses() ### A function that receive from the user an infinite loop 
                                ### of ip addresses and then returns the ip addresses 
                                ### as a list.
  print '\n Type Linux commands. Type "end" to finish entering commands.'
  cmds = []
  while True:
      userInput = raw_input("&gt; ")
      if userInput.lower() == "end":
          break
      cmds.append(userInput)
  print '\nThanks! Working...\n'
  for ipAddr in ipList:
      badConnection = 0
      s = socket()
      s.settimeout(2)
      for currentPort in ("22", "22222"):
          print "INFO: Trying to connect to: {} with port {}".format(ipAddr, currentPort)
          try:
              portSocket = int(currentPort)
              s.connect((ipAddr, portSocket))
              s.close()
          except:
              badConnection += 1
              print "ERROR: Could not connect to {} with port {}".format(ipAddr, currentPort)
              if badConnection == 1:
                  continue
          if badConnection == 2:
              break
          for cmd in cmds:
              ssh = subprocess.Popen(['ssh', ipAddr, "-p", currentPort], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=0)
              finalCMD = "{} \n".format(cmd)
              ssh.stdin.write(finalCMD)
              ssh.stdin.close()
              sshOutput = ssh.stdout.readlines()
              sshError = ssh.stderr.readlines()
              if len(sshError) &gt; 0:
                  for msg in sshError:
                      print msg
              if len(sshOutput) &gt; 0:
                  for msg in sshOutput:
                      print msg
              if len(sshOutput) == 0 and len(sshError) == 0:
                  msg = 'Command executed successfully'
                  print msg
          break
      if badConnection == 2:
          continue
  </code></pre>
  
  <p>Additionally some servers work with port 22 and some with port 22222, so that is why the there is a "CurrentPort" condition.</p>