DevNet 102: Network Automation Using Python


                        How to add  vlans on Multi-Switches  by python



Add commands to two switches:

ip name-server 8.8.8.8
ip domain name cloud.me
username ahmed password cisco
enable password cisco
line vty 0  4
login local
transport input all
exit
crypto key generate rsa
ip ssh version 2


Create text file contain IPs of switches:

root@kali:/home# vim myswitches.txt
192.168.23.100
192.168.23.101

Add VLANs on two switches by python script:

import paramiko
import time
import getpass

# Open file with list of switches
f = open("myswitches.txt")

# enter to each switch and configure it:
for line in f:
    ip_address = line.strip()
    username = raw_input("Enter your username of : " + ip_address + ": ")
    password = getpass.getpass()

    ssh_client  = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip_address,username=username,password=password)

    print "Successful connection  " + ip_address

    remote_connection = ssh_client.invoke_shell()

    print "Configure vlans on  " + ip_address

    remote_connection.send('en\n')
    time.sleep(1)
    recv = remote_connection.recv(9999)

    remote_connection.send('cisco\n')
    time.sleep(1)
    recv = remote_connection.recv(9999)
    remote_connection.send("vlan database" + "\n")

    for n in range (2,12):
        print "Create VLAN " + str(n)
        remote_connection.send("vlan " + str(n) + " name Vlan_" + str(n) + "\n")
        time.sleep(0.5)

    remote_connection.send("exit\n")
    time.sleep(15)                          
    output  = remote_connection.recv(65535)
    print output
    ssh_client.close

Run Script:
root@kali:/home# python  multi-devices-vlans.py
Enter your username of : 192.168.23.100: ahmed
Password: 
Successful connection  192.168.23.100
Configure vlans on  192.168.23.100
Create VLAN 2
Create VLAN 3
Create VLAN 4
Create VLAN 5
Create VLAN 6
Create VLAN 7
Create VLAN 8
Create VLAN 9
Create VLAN 10
Create VLAN 11
vlan database
ESW1(vlan)#vlan 2 name Vlan_2
VLAN 2 modified:
    Name: Vlan_2
ESW1(vlan)#vlan 3 name Vlan_3
VLAN 3 modified:
    Name: Vlan_3
ESW1(vlan)#vlan 4 name Vlan_4
VLAN 4 modified:
    Name: Vlan_4
ESW1(vlan)#vlan 5 name Vlan_5
VLAN 5 modified:
    Name: Vlan_5
ESW1(vlan)#vlan 6 name Vlan_6
VLAN 6 modified:
    Name: Vlan_6
ESW1(vlan)#vlan 7 name Vlan_7
VLAN 7 modified:
    Name: Vlan_7
ESW1(vlan)#vlan 8 name Vlan_8
VLAN 8 modified:
    Name: Vlan_8
ESW1(vlan)#vlan 9 name Vlan_9
VLAN 9 modified:
    Name: Vlan_9
ESW1(vlan)#vlan 10 name Vlan_10
VLAN 10 modified:
    Name: Vlan_10
ESW1(vlan)#vlan 11 name Vlan_11
VLAN 11 modified:
    Name: Vlan_11
ESW1(vlan)#exit
APPLY completed.
Exiting....
ESW1#
Enter your username of : 192.168.23.101: ahmed
Password: 
Successful connection  192.168.23.101
Configure vlans on  192.168.23.101
Create VLAN 2
Create VLAN 3
Create VLAN 4
Create VLAN 5
Create VLAN 6
Create VLAN 7
Create VLAN 8
Create VLAN 9
Create VLAN 10
Create VLAN 11
vlan database
ESW2(vlan)#vlan 2 name Vlan_2
VLAN 2 added:
    Name: Vlan_2
ESW2(vlan)#vlan 3 name Vlan_3
VLAN 3 added:
    Name: Vlan_3
ESW2(vlan)#vlan 4 name Vlan_4
VLAN 4 added:
    Name: Vlan_4
ESW2(vlan)#vlan 5 name Vlan_5
VLAN 5 added:
    Name: Vlan_5
ESW2(vlan)#vlan 6 name Vlan_6
VLAN 6 added:
    Name: Vlan_6
ESW2(vlan)#vlan 7 name Vlan_7
VLAN 7 added:
    Name: Vlan_7
ESW2(vlan)#vlan 8 name Vlan_8
VLAN 8 added:
    Name: Vlan_8
ESW2(vlan)#vlan 9 name Vlan_9
VLAN 9 added:
    Name: Vlan_9
ESW2(vlan)#vlan 10 name Vlan_10
VLAN 10 added:
    Name: Vlan_10
ESW2(vlan)#vlan 11 name Vlan_11
VLAN 11 added:
    Name: Vlan_11
ESW2(vlan)#exit
APPLY completed.
Exiting....
ESW2#



ESW1#show vlan-switch

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Fa1/0, Fa1/1, Fa1/2, Fa1/3
                                                Fa1/4, Fa1/5, Fa1/6, Fa1/7
                                                Fa1/8, Fa1/9, Fa1/10, Fa1/11
                                                Fa1/12, Fa1/13, Fa1/14, Fa1/15
2    Vlan_2                           active
3    Vlan_3                           active
4    Vlan_4                           active
5    Vlan_5                           active
6    Vlan_6                           active
7    Vlan_7                           active
8    Vlan_8                           active
9    Vlan_9                           active
10   Vlan_10                          active
11   Vlan_11                          active
1002 fddi-default                     active
1003 token-ring-default               active
1004 fddinet-default                  active
1005 trnet-default                    active
ESW2#show vlan-switch

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Fa1/0, Fa1/1, Fa1/2, Fa1/3
                                                Fa1/4, Fa1/5, Fa1/6, Fa1/7
                                                Fa1/8, Fa1/9, Fa1/10, Fa1/11
                                                Fa1/12, Fa1/13, Fa1/14, Fa1/15
2    Vlan_2                           active
3    Vlan_3                           active
4    Vlan_4                           active
5    Vlan_5                           active
6    Vlan_6                           active
7    Vlan_7                           active
8    Vlan_8                           active
9    Vlan_9                           active
10   Vlan_10                          active
11   Vlan_11                          active
1002 fddi-default                     active
1003 token-ring-default               active
1004 fddinet-default                  active
1005 trnet-default                    active

---------------------------------------------------------------------------------------------------------------

Good Luck https://www.linkedin.com/in/ahmedms/




























Comments

Popular posts from this blog

Kubernetes 104: Create a 2-node k3s cluster with k3sup

DevNet 103: Network Automation Using Python

How to configure OSPF on Palo Alto Networks Firewall?