title: Improving exception handling of Netmiko script to SSH into Cicso devices
body: <p>I'm trying to improve the exception handling of a netmiko script that successfully runs commands on Cisco devices listed in a text file and saves a list of the ones that expecience a connection timeout to a .txt file. When I SSH into Switch-A manually, I get the &quot;connection closed&quot; error. A manual attempt to Switch- B results in the connection refused error.</p>
  <p>Below is the error messag when trying to connect to Switch-A:</p>
  <pre><code>Connecting to device&quot; Switch-A
  
  Exception: Error reading SSH protocol banner
  
  Traceback (most recent call last):
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\paramiko-2.4.2-py3.6.egg\paramiko\transport.py&quot;, line 2138, in _check_banner
  
  buf = self.packetizer.readline(timeout)
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\paramiko-2.4.2-py3.6.egg\paramiko\packet.py&quot;, line 367, in readline
  
  buf += self._read_timeout(timeout)
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\paramiko-2.4.2-py3.6.egg\paramiko\packet.py&quot;, line 563, in _read_timeout
  
  raise EOFError()
  
  EOFError
  </code></pre>
  <hr />
  <p>The error message for SWITCH-B</p>
  <pre><code>During handling of the above exception, another exception occurred:
  
  
  
  Traceback (most recent call last):
  
  File &quot;Use me #2.py&quot;, line 39, in &lt;module&gt;
  
  net_connect = ConnectHandler(**ios_device)
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\netmiko-2.3.3-py3.6.egg\netmiko\ssh_dispatcher.py&quot;, line 228, in ConnectHandler
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\netmiko-2.3.3-py3.6.egg\netmiko\base_connection.py&quot;, line 312, in __init__
  
  File &quot;C:\CorpApps\Python36\lib\site-packages\netmiko-2.3.3-py3.6.egg\netmiko\base_connection.py&quot;, line 858, in establish_connection
  
  KeyboardInterrupt
  
  
  
  During handling of the above exception, another exception occurred:
  
  
  
  Traceback (most recent call last):
  
  File &quot;Use me #2.py&quot;, line 50, in &lt;module&gt;
  
  except (SSHException):
  
  TypeError: catching classes that do not inherit from BaseException is not allowed
  </code></pre>
  <pre><code>from netmiko import ConnectHandler
  from netmiko.ssh_exception import  NetMikoTimeoutException
  from paramiko.ssh_exception import SSHException 
  from netmiko.ssh_exception import  AuthenticationException
  from getpass import getpass
  from pprint import pprint
  
  with open('commandsv2.txt') as f:
      commands_list = f.read().splitlines()
  
  with open('routersv3.txt') as f:
      router_list = f.read().splitlines()
  
  username=input('Enter your username:')
  password=getpass()
  print (password)
  
  
  for routers in router_list:
      print ('Connecting to device&quot; ' + routers)
      ip_address_of_device = routers
      ios_device = {
      'device_type': 'cisco_ios',
      'ip': ip_address_of_device,
      'username': username,
      'password': password
      }
      
      Timeouts=open(&quot;Connection time outs.txt&quot;, &quot;a&quot;)
      Authfailure=open(&quot;Auth failures.txt&quot;, &quot;a&quot;)
      SSHException=(&quot;SSH Failure.txt&quot;, 'a')
      EOFError=(&quot;EOFerrors.txt&quot;,'a')
      UnknownError=(&quot;UnknownError.txt&quot;,'a')
      
      try:
          net_connect = ConnectHandler(**ios_device)  
          output=net_connect.send_config_set(commands_list)
          print(output)
      except (AuthenticationException):
          print ('Authentication Failure: ' + ip_address_of_device)
          Authfailure.write('\n' + ip_address_of_device)
          continue 
      except (NetMikoTimeoutException):
          print ('\n' + 'Timeout to device: ' + ip_address_of_device)
          Timeouts.write('\n' + ip_address_of_device)
          continue
      except (SSHException):
          print ('SSH might not be enabled: ' + ip_address_of_device)
          SSHException.write('\n' + ip_address_of_device)
          continue 
      except (EOFError):
          print ('\n' + 'End of attempting device: '  ip_address_of_device)
          EOFError.write('\n' + ip_address_of_device)
          continue
      except unknown_error:
          print ('Some other error: ' + str(unknown_error))
          continue
  </code></pre>