Configure "No Autostate" on Vlan404

Summary:

Note

Credentials were modified to protect the privacy of the organization

Special attention should be paid to the type of Routers firmware that this script should configure - since most field routers were IOS, this script worked fine but some modification of delays and configuration prompts string differ in other device/ servers

Logic Diagram:

Python Script:

from netmiko import ConnectHandler
import paramiko

def is_not_all_whitespace(text):
  """Checks if a text string is not all whitespaces.

  Args:
    text: The text string to check.

  Returns:
    True if the text string is not all whitespaces, False otherwise.
  """

  # Check if the text string is empty.
  if not text:
    return False

  # Check if the text string contains any non-whitespace characters.
  for character in text:
    if character not in " \t\n\r\f":
      return True

  # The text string is all whitespaces.
  return False 

with open("SITES.txt", "r") as f:

  # Iterate over the lines of the file.
  for line in f:

    router = {
    'device_type': 'cisco_ios',
    'host':   str(line),
    'username': 'cisco',
    'password': 'cisco123',
    }

    try:
        router_connected = ConnectHandler(**router)
        
        outputVlan6 = router_connected.send_command('show ip interface brief | i Vlan6')

        prompt = router_connected.find_prompt()
		
		# How to get Hostname for debugging if needed 
        hostname = prompt.split()[-1]
        
        router_connected.config_mode()

        if networkUtil.is_not_all_whitespace(outputVlan6):
            router_connected.send_config_set([
                "interface vlan6", 
                "no autostate",
                ])
            
            router_connected.exit_config_mode()

	        result_dictionary[hostname] = [False, str(line).strip('\n'), router_connected]

			router_connected.save_config()
			
        else:
	        continue 
	    
    except paramiko.ssh_exception.AuthenticationException as e:
        # Handle the exception.
        print('Error connecting to device ' + str(line).strip('\n') + ': {}'.format(e))

    finally:
        # Close the connection to the device.
        router_connected.disconnect()
        continue