Real Network Security & Firewall Configuration Guides

Layman Switch Automation Script



    
Switch used for Automation

from netmiko import ConnectHandler
user = input("Enter the Username: ")
passwd = input("Enter the pass: ")
cisco_switch = {
    'device_type': 'cisco_ios',
    'host':   input("Enter the ip addr: "),
    'username': user,
    'password': passwd,
    'port': 22,          # optional, defaults to 22
    'secret': 'secret',     # optional, defaults to ''
}
net_connect = ConnectHandler(**cisco_switch)
print("***||Layman Switch Automation Script||***")
print("What do you you want to do:\n 1-Show VLANs\n 2-Create VLANs\n 3-Delete VLANs\n 4-Rename VLANs\n 5-Assign IP Address to VLAN interface \n 6-show ip address assigned interface\n 7-Assign default-route\n 8-Assign Static route\n 9-Test Ping\n")
while True:
    option=int(input("Put your Choice here: "))
    #For Showing the VLANs on the Console
    if option==1:
        output=net_connect.send_command('sh vlan br')
        print(output)
    #For Creating New VLAN on the switch
    elif option==2:
        vlan_id=int(input("Enter VLAN id: "))
        commands=[f"vlan {vlan_id}"]
        output=net_connect.send_config_set(commands)
        print("***Operation performed Successfully to see VLANs Press 1***")
    #For Deleting any VLAN form the Switch
    elif option==3:
        vlan_id=int(input("Enter VLAN id: "))
        commands=[f"no vlan {vlan_id}"]
        net_connect.send_config_set(commands)
        print("***Operation performed Successfully to see VLANs Press 1***")
    #For Assigning the name or renaming any VLAN
    elif option==4:
        vlan_id=int(input("Enter VLAN id: "))
        vlan_name=input("Enter VLAN name: ")
        commands=[f'vlan {vlan_id}',f'name {vlan_name}']
        net_connect.send_config_set(commands)
        print("***Operation performed Successfully to see renamed VLANs Press 1***")
    #For Assigning the IP Address to the VLAN virtual Interface
    elif option==5:
        vlan_id=int(input("Enter interface VLAN id: "))
        IP_address=input("Enter ip address: ")
        netmask=input("Enter the vlan subnet mask: ")
        commands=[f'int vlan {vlan_id}',f'ip address {IP_address} {netmask}','no sh']
        net_connect.send_config_set(commands)
        print("***Operation performed Successfully to see interfaces Press 6***")
    #For Seeing all the IP assigned interface
    elif option==6:
        output=net_connect.send_command('sh ip int br | exclude unassigned')
        print(output)
    #For assigning the default gateway to the Switch
    elif option==7:
        default_route=input("Enter the default-route IP: ")
        commands=[f'ip default-gateway {default_route}']
        net_connect.send_config_set(commands)
        print("***Operation performed Successfully***")
    #For assigning a static route on the switch
    elif option==8:
        destination_network=input("Enter the destination Network: ")
        des_mask=input("Enter the destination Mask: ")
        next_hop=input("Enter the next hop IP: ")
        commands=[f'ip route {destination_network} {des_mask} {next_hop}']
        net_connect.send_config_set(commands)
        print("***Operation performed Successfully***")
    #For doing echo Ping request to any IP
    elif option==9:
        ping_ip=input("Enter the IP Address to ping: ")
        commands=[f'do ping {ping_ip}']
        op=net_connect.send_config_set(commands)
        print(f"***Operation performed Successfully***\n {op}")
    #For exiting out of the program
    elif option==0:
        exit(0)
    else:
        print("Invalid input")
Output for the Above Python Script


To see all the available VLANs on the switch I putted 1

To Create a New VLAN on the switch I entered 2 (VLAN-25 created)

Here you can see VLAN 25 created

Here I deleted the created VLAN by putting 3 and VLAN id 25

Here I renamed the VLAN 26 form "My-VLAN" to "my_vlan"

Before renaming

After renaming

Assigning the IP address to VLAN virtual interface i.e.; VLAN-id: 26

Here you can see the IP address assigned to VLAN 26 i.e.; 192.168.1.254

Assigning the default-gateway

output of the above code

Assigning the static route

Output of the above code

Here is the output of the Ping


The above script has been created keeping the intention that the person is a layman he doesn't know about the CLI of Cisco Switch so using the above python script.

What do you want to do:

  1. Show VLANs

  2. Create VLANs

  3. Delete VLANs

  4. Rename VLANs

  5. Assign IP Address to VLAN interface

  6. show the IP address assigned the interface

  7. Assign default-route

  8. Assign Static route

  9. Test Ping






NextGen Digital... Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...