Monday, May 15, 2023

Aruba CX + Python + UBT + Role Based access + Bulk Edit

Aruba's role based access is a very powerful tool that allows you to profile devices which grants you a simple switch config, (no more changing a VLAN if someone plugs in a printer into the wrong port), as well as central managed security profiles via Clearpass.  

But what if you want to make bulk edits to those devices?  what if clearpass or the aruba controller blips and your devices are not connecting?  How can you find all of the devices across your network that are in a failed state?  Below we well work through how to set up Python to allow for you to solve some of these issues and fix them without manually logging into every switch.

Python + Aruba CX

The below code is for Aruba CX switches that are configured for UBT, based on the role that the devices are pulling we can sort by those devices only to be able get the data we need.

* In this program we will be finding all of the devices that are pulling a certain role and then shutting the port off then turning it back on.

1. First thing we will need will be all of the packages.

from netmiko import ConnectHandler from getpass import getpass import requests import urllib3 import re import time import getpass
2.  Next set up our input.  this will allow us to select a "site" which will be a text file with all of our switch IP's that we are going to be logging into.
print(" 1 = Site1 \n 2 = Site2 \n Select Site:") site = input() print(f"Site Selected: {site}") print("----------------------------------") print("----------------------------------") print(" 1 = wired_l1_game_dur-3142-2 \n 2 = wired_l2_print_dur-3134-6 \n Select Role:") role = input() if campus == "1": file = [line.strip() for line in open("site1.txt", 'r')] if campus == "2": file = [line.strip() for line in open("site2.txt", 'r')]

    ##### Add more roles to the print output line if you have more to add  

#print(file[]) #file.close() if role == "1": policyrole = "wired_l1_game_dur-3142-2" if role == "2": policyrole = "wired_l2_print_dur-3134-6" 
## Add more roles if you have more to add print("Enter Username:") username = input() print("Enter Password:") password = getpass.getpass() creds = {"username": {username}, "password": {password}}
3.  We need to be able to log into the switch, we do this with the plugin NetMiko with the Connection Handler, this will read the text file we have loaded and pull in the switch IP address for each line so we can check the switch interfaces.  The program will run through each line from the text file until it reaches the end.
for selectIP in file:
    #print("Enter the Switch IP:")
    ip_add = selectIP
    print(ip_add)
    session = requests.session()
    
    net_connect = ConnectHandler(
    device_type="aruba_procurve",
    host=ip_add,
    username=ausername,
    password=apassword,
    )
4.  Here is where the ports are actually changed.  We will send a command to the switch to show the port role, get the information then bounce each interface with the same role.  The program will print out which switch it is connected to as well as the ports that were changed when you run the command... This will help you know which ports were changed as well as to go back and check if you wanted to.
4.  Here is where the ports are actually changed.  We will send a command to the switch to show the port role get the information then bounce each on of those ports for that specific role.
output = net_connect.send_command(f"show port-access clients role {policyrole}") to1 = output.split("\n") print(f"Ports that have role: {policyrole}:") for to in to1 : to = to[2:] to = to.split(" ")[0] if re.search("[\d]+/[\d]/[\d]+",to): print (to) to2 = to.split("/") output1 = [f"interface {to2[0]}/{to2[1]}/{to2[2]}","shut"] net_connect.send_config_set(output1) time.sleep(1) output2 = [f"interface {to2[0]}/{to2[1]}/{to2[2]}","no shut"] net_connect.send_config_set(output2) net_connect.send_config_set(f"wr mem") net_connect.disconnect() print(f"Logged out from Switch!")


Tuesday, September 21, 2021

Aruba CX Quick Config


Hostname
hostname <building>_<MDF/IDF location>_core_<model>

Username
user manager group administrators password plaintext <password>
user manager authorized-key ssh-rsa VeryLongKeyShouldGoHere

Logging
logging x.x.x.x
logging facility local2

Default gateway and DNS
ip route 0.0.0.0/0 <MGMT gateway>
ip dns server-address priority 1 1.0.0.1
ip dns server-address priority 2 8.8.8.8

NTP and time
ntp server <default gateway> prefer iburst minpoll 4
clock timezone us/central
ntp enable

SNMP
snmp-server vrf default
snmp-server system-location <location>
snmp-server system-contact <ITS>
snmp-server community snmpv1string

Standard VLAN
vlan 100
name VlanName100
exit

vlan 200
name VlanName200
interface vlan 200
ip address X.X.X.X/24

LACP 
interface 1/1/1
no shut
lag 1

interface 1/1/2
no shut 
lag 1

interface lag 1
no shut
vlan trunk native 1
vlan trunk allowed all
lacp mode active

Tag VLAN's on interfaces - if you want to allow all vlan's
interface 1/1/1-1/1/x
vlan trunk native 1
vlan trunk allowed all

Vlan access - if you only want to allow 1 vlan 
interface 1/1/1
vlan access 6