DevNet 101: Network Automation Using Python


How to add  vlans on one switch by python


Add VLANs on ESW1 by python script:

import paramiko
import time

ip_address = "192.168.23.101"
username = "ahmed"
password = "cisco"

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()
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("configure terminal\n")
remote_connection.send("interface loop 0\n")
remote_connection.send("ip address 1.1.1.1 255.255.255.255\n")
remote_connection.send("interface loop 1\n")
remote_connection.send("ip address 2.2.2.2 255.255.255.255\n")
remote_connection.send("end\n")

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 devnet.py 
Successful connection 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
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
ESW1(config)#interface loop 0
ESW1(config-if)#ip address 1.1.1.1 255.255.255.255
ESW1(config-if)#interface loop 1
ESW1(config-if)#ip address 2.2.2.2 255.255.255.255
ESW1(config)#end
ESW1#vlan database
ESW1(vlan)#vlan 2 name Vlan_2
VLAN 2 added:
    Name: Vlan_2
ESW1(vlan)#vlan 3 name Vlan_3
VLAN 3 added:
    Name: Vlan_3
ESW1(vlan)#vlan 4 name Vlan_4
VLAN 4 added:
    Name: Vlan_4
ESW1(vlan)#vlan 5 name Vlan_5
VLAN 5 added:
    Name: Vlan_5
ESW1(vlan)#vlan 6 name Vlan_6
VLAN 6 added:
    Name: Vlan_6
ESW1(vlan)#vlan 7 name Vlan_7
VLAN 7 added:
    Name: Vlan_7
ESW1(vlan)#vlan 8 name Vlan_8
VLAN 8 added:
    Name: Vlan_8
ESW1(vlan)#vlan 9 name Vlan_9
VLAN 9 added:
    Name: Vlan_9
ESW1(vlan)#vlan 10 name Vlan_10
VLAN 10 added:
    Name: Vlan_10
ESW1(vlan)#vlan 11 name Vlan_11
VLAN 11 added:
    Name: Vlan_11
ESW1(vlan)#exit
APPLY completed.
Exiting....
ESW1#

Show VLAN:
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

Show IP Interface:
 ESW1#show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.23.100  YES manual up                    up
FastEthernet0/1            unassigned      YES NVRAM  administratively down down
FastEthernet1/0            unassigned      YES unset  up                    down
FastEthernet1/1            unassigned      YES unset  up                    down
FastEthernet1/2            unassigned      YES unset  up                    down
FastEthernet1/3            unassigned      YES unset  up                    down
FastEthernet1/4            unassigned      YES unset  up                    down
FastEthernet1/5            unassigned      YES unset  up                    down
FastEthernet1/6            unassigned      YES unset  up                    down
FastEthernet1/7            unassigned      YES unset  up                    down
FastEthernet1/8            unassigned      YES unset  up                    down
FastEthernet1/9            unassigned      YES unset  up                    down
FastEthernet1/10           unassigned      YES unset  up                    down
FastEthernet1/11           unassigned      YES unset  up                    down
FastEthernet1/12           unassigned      YES unset  up                    down
FastEthernet1/13           unassigned      YES unset  up                    down
FastEthernet1/14           unassigned      YES unset  up                    down
FastEthernet1/15           unassigned      YES unset  up                    down
Vlan1                      unassigned      YES NVRAM  administratively down down
Loopback0                  1.1.1.1         YES manual up                    up
Loopback1                  2.2.2.2         YES manual up                    up
        

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

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





























Comments

Popular posts from this blog

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

Cisco Nexus: Configuration VXLAN.

How to configure OSPF on Palo Alto Networks Firewall?