title: Python Script to configure (via SSH) Cisco Switches
body: <p>I have a lot of cisco switches and routers in my environment. I have three sets of credentials (only one of them works with a particular device. I then have the IPs listed in a notepad (sub.txt). And the configuration in another notepad (config.txt)</p>
  
  <p>The aim is to push the configurations (line by line) from config.txt to the list of list of IP via SSH. If the command is accepted by the devices, a log should be put into success.txt and if the command is not accepted for some reason, a log should be appended to fail.txt</p>
  
  <p>But this does not work. Can you please help me fix?</p>
  
  <pre><code>import paramiko
  import time
  import sys
  import logging
  import socket
  import pexpect
  import traceback
  from pexpect.popen_spawn import PopenSpawn
  
  
  
  remote_conn_pre = paramiko.SSHClient()
  remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  
  ips = [i.strip() for i in open("sub.txt")] 
  user_local = "user1"
  pass_local = "pass1"
  user_aspac = "user2"
  pass_aspac = "pass2"
  user_batcca = "user3"
  pass_batcca = "pass3"
  
  g = open('config.txt', 'r+')
  str = g.read()
  g.close
  
  success = open('success.txt', 'a')
  fail = open('failed.txt', 'a')
  
  paramiko.util.log_to_file("paramiko.log")
  
  for ip in ips:
      try:
          remote_conn_pre.connect(ip, username=user_local, password=pass_local, timeout=4, look_for_keys=False, allow_agent=False)
          #print ("SSH connection established to %s" + ip)
          remote_conn = remote_conn_pre.invoke_shell()
          print (ip + ' === local credential')
          #remote_conn.send("show switch\n")
          remote_conn.send((str))
          time.sleep(2) 
          output = remote_conn.recv(5000)
          print (output)
  
      except paramiko.AuthenticationException:
          try:
              remote_conn_pre.connect(ip, username=user_aspac, password=pass_aspac, timeout=4, look_for_keys=False, allow_agent=False)
              remote_conn1 = remote_conn_pre.invoke_shell()
              print ip + ' === Global Credentials'
              #output = remote_conn.recv(500)
              #remote_conn.send("show switch")
              #remote_conn.send("\n")
              remote_conn1.send((str))
              time.sleep(2) 
              output1 = remote_conn1.recv(5000)
              print (output1)
          except paramiko.AuthenticationException:
              try:
                  #remote_conn_pre.connect(ip, username=user_batcca, password=pass_batcca, timeout=4, look_for_keys=False, allow_agent=False)
                  #remote_conn2 = remote_conn_pre.invoke_shell()
                  child = pexpect.popen_spawn.PopenSpawn('ssh ' + user_batcca + '@' + ip)
                  child.expect ('[pP]assword:')
                  child.sendline(pass_batcca)
                  print ip + ' === BATCCA Credential'
                  #output2 = remote_conn2.recv(5000)
                  for line in open('Config.txt').xreadlines():
                      child.sendline(line)
                      i = child.expect (['#', '^'])
                      if i==0:
                          success.write(ip + '\t' + line +'\n')
                      elif i==1:
                          fail.write(ip + '\t' + line +'\n')
                      time.sleep(5)
                      output2 = child.recv(5000)
                      print (output2)
  
              except paramiko.AuthenticationException:
                  print ip + ' === Bad credentials'
                  remote_conn3 = remote_conn_pre.invoke_shell()
                  output3 = remote_conn3.recv(5000)
                  print (output3)
      except paramiko.SSHException:
          print ip + ' === Issues with ssh service'
      except socket.error:
          print ip + ' === Device unreachable'
  </code></pre>