Configure routers using python program

 import paramiko

def configure_router(ip, username, password, commands): # Establish SSH connection client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(ip, username=username, password=password) # Execute commands on the router for command in commands: stdin, stdout, stderr = client.exec_command(command) output = stdout.read().decode().strip() error = stderr.read().decode().strip() if output: print(f"Command: {command}\nOutput:\n{output}\n") if error: print(f"Command: {command}\nError:\n{error}\n") # Close the SSH connection client.close() if __name__ == "__main__": # Define the router credentials and commands router_ip = "192.168.0.1" router_username = "admin" router_password = "password123" configuration_commands = [ "enable", "configure terminal", "interface GigabitEthernet0/1", "ip address 192.168.1.1 255.255.255.0", "no shutdown", "exit", "exit", "copy running-config startup-config" ] # Configure the router configure_router(router_ip, router_username, router_password, configuration_commands) In this program, we use the `paramiko` library to establish an SSH connection with the router. We provide the router IP, username, password, and a list of commands to be executed on the router.

The `configure_router` function takes these parameters and performs the following steps:
1. Establishes an SSH connection with the router.
2. Iterates over the list of commands and executes each command on the router using `exec_command` method from `paramiko`.
3. Reads the output and error streams from the `stdout` and `stderr` variables.
4. Prints the output and error messages for each command, if any.
5. Closes the SSH connection with the router.

To use this program, you need to install the `paramiko` library. You can do this by running `pip install paramiko` in your command prompt or terminal.

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.