VLANs Creating using Paramiko

Paramiko is a Python library used for automating network infrastructure. It allows you to connect to a network element using SSH and run commands. Network automation offers many benefits including, saving time configuring the devices, eliminating human errors, fast troubleshooting..etc.

In this project, we will demonstrate how we create VLANs for several different customers in a switch using Paramiko. We will be using GNS3 environment for simulating the network

Intorduction

Project Setup:

In the below picture, we have a network topology in the GNS3 environment. The cloud-shaped node represents my physical PC device. It connects to the virtual GNS3 environment using a loopback network adapter. Python is installed on my local machine and Pycharm is used as the Python editor.

"SW1" is a layer 3 switch and we connect to it using the management VLAN 1 for configuration with an IP of 10.1.2.10. SSH was pre-configured for "SW1" with username "Ahmed" and password "1234" along with an enable password of "9999" for read privilege mode.

We want to create 20 VLANS for 20 customers. Since VLAN 1 is reserved for management, we shall create VLANS from 2 to 21 with their VLAN interfaces working as IP gateways. Below, we have a Python dictionary containing customer details. the keys of the dictionary contain the customers' names and and the values of the dictionary contain their VLAN interfaces as shown below.

Project Details:

First of all, we need to import "Paramiko" library and "Time" library. We imported "Time" because we need to give the script enough time to be executed without interruption as will be explained later.

As shown below, we have a Python dictionary named "SW1" that contains the IP of the switch that we want to connect and configure, port 22 which is for SSH, and my SSH credentials. A built-in function called "paramiko.SSHClient()"will be used to connect to our GNS3 node. This function uses a sub-function called "connect". It takes "SW1" dictionary as an argument. We unpacked the "SW1" inside "connect". "paramiko.SSHClient()" is assigned to a "ssh_client" variable.

Now to the crucial part. The "invoke_shell" is a function that allows a network engineer to run commands on the device after establishing an SSH connection using a "send" function. For instance, we send the "enable" command on the switch as shown below. The "\n" symbol in Python means entering a new line. so we have to have it at the end of each command for the commands to be executed in the switches.

After we already established an SSH connection, we have to start creating VLANs for our customers. Since we are making 20 VLANS, we will use a "for" loop with a built-in function called "range". "range" function loops from 1 to the "len(customers)" (i.e. length of the customer dictionary) which is 20. However, since the "range" function does not include the last index, we added 1. We also need to enable the trunk on the port "Gig0/0" to allow inter-vlans. we have the variable "vlan" that used for the loop.

Next, we have to name each VLAN with its corresponding customer by looping through the keys of the dictionary. As you notice, in the first line, we have "Vlan+1" because we want to start with Vlan 2 since Vlan on is already reserved. The second line is the command for naming the VLANs. the loop starts with 1 but we want to start with index 0 in the dictionary so we subtract by one. Otherwise, it will start with costumer#2.

Then, we have to configure the VLAN interfaces by looping through the values of the customers' dictionary. We also will add a description for each VLAN. The "sleep" function is used here to hold each loop for about 2 seconds so the scripts will have enough time to be fully and safely executed in the switch without any interruption.

Finally, Let us run the command. As shown below, the Switch is showing SSH session logs as the script is being executed.

Now let us check the configuration and make sure everything is good. As shown below. The VLAN names, VLAN interfaces, and VLAN descriptions are configured as intended.

Please find below the full code:

import paramiko #importing Libraries
import time

customers= {
"customer1":"10.1.10.1",
"customer2": "10.1.11.1",
"customer3": "10.1.12.1",
"customer4": "10.1.13.1",
"customer5": "10.1.14.1",
"customer6": "10.1.15.1",
"customer7": "10.1.16.1",
"customer8": "10.1.17.1",
"customer9": "10.1.18.1",
"customer10": "10.1.19.1",
"customer11": "10.1.20.1",
"customer12": "10.1.21.1",
"customer13": "10.1.22.1",
"customer14": "10.1.23.1",
"customer15": "10.1.24.1",
"customer16": "10.1.25.1",
"customer17": "10.1.26.1",
"customer18": "10.1.27.1",
"custSomer19": "10.1.28.1",
"customer20": "10.1.29.1"} #Customers details in a dictionary. Names with their gateway IP's

SW1={"hostname":"10.1.2.10","port":'22',"username":'Ahmed',"password":"1234"} # switch#1 with my ccredentials
ssh_client = paramiko.SSHClient() # Using SSHClient which is one of the functions in the paramiko library.
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(**SW1,look_for_keys=False,allow_agent=False) # Here, we are connecting to the devices along with my creditials

shell= ssh_client.invoke_shell()
shell.send("enable \n")
for vlan in range(1,len(customers)+1): # we are creating vlans that starts from 2 until last customer and plus one since the range function doesn't include the last index
shell.send("enable \n") #accessing to read-only privlage level
shell.send("9999\n")
shell.send("conf t\n") #accessing to configeration level
shell.send( "int g0/0\n")
shell.send( "switchport trunk encapsulation dot1q\n")
shell.send("switchport mode trunk\n")
shell.send(f'vlan {vlan+1}\n') # looping and creating vlans from 2 to the last customer
shell.send(f'name {list(customers.keys())[vlan-1]} \n') # Naming each vlan while looping through the keys of the customer dictionary
shell.send(f'exit\n')
shell.send(f'interface vlan {vlan+1}\n')
shell.send(f' ip add {list(customers.values())[vlan-1]} 255.255.255.0\n')# Assigning ip gateways for each vlan
shell.send(f'no shut\n')
shell.send(f' description gateway for {list(customers.keys())[vlan-1]}\n')# labeling each vlan with its respective cutomer via looping through the values of the customer dictionary (i.e VLAN interfaces)
time.sleep(2) # using the "sleep" function from the "Time" library to give enough time for the commands to be executed without interruptions.
shell.send(f' end \n')

print("Done")