Scenario
To immediately see what device is connected to a port, we want to change the interface name to the neighbor’s name and the remote port.
More information about the ArubaOS REST API can be found in my guides.
Devices
This code is specified for ArubaOS-S switches, ArubaOS-CX will be added in a future article.
I worked with:
- Aruba 2930F (JL258A) WC.16.10.0003
- Python 3
Enable the REST interface
We need to connect to the switch API and authenticate using a manager privileged user.
To use the REST API add this command to the configuration: “rest-interface“.
To use an encrypted SSL web-management needs to be enabled and a certificate needs to be installed or created on the switch.
I will use a self-created, self-signed certificate by the switch using this command: “crypto key generate autorun-key rsa“.
Enable web-management with “web-management ssl“.
Make sure the REST interface and web-management are up using “show rest-interface”.
Switch01# show rest-interface
REST Interface - Server Configuration
REST Interface : Enabled
REST Operational Status : Up
REST Session Idle Timeout : 600 seconds
HTTP Access : Disabled
HTTPS Access : Enabled
SSL Port : 443
Connecting to the switch
If a manager user is configured on the switch, we need to authenticate before we can use the API. For this, we send an HTTP post request, which returns us an authentication cookie. This cookie is our session and we need to reference it in every action we take.
We also should close our session after we finished our operation.
To connect we can use a simple Python code using requests and JSON.
Please note that because of the self-signed certificates, I will not verify them in the requests.
def login(api_url, api_user=None, api_pw=None):
print("Getting cookie")
s = requests.Session()
target_url = api_url + "login-sessions"
data = json.dumps({'userName': api_user, 'password': api_pw})
try:
r = s.post(target_url, data=data, verify=False)
if r.ok:
return {'s': s, 'cookie': r.json()['cookie'].split('=')[1]}
else:
print("HTTP Code: %i \n %s \n Message %s" % (r.status_code, r.reason, r.text))
return
except requests.exceptions.ConnectTimeout:
print('ERROR: Error connecting to host: connection attempt timed out.')
exit(-1)
def logout(api_user='admin', **session_dict):
target_url = session_dict['url'] + "login-sessions"
header = {"SessionId": session_dict["cookie"]}
data = json.dumps({'userName': api_user})
r = session_dict['s'].delete(target_url, headers=header, data=data, verify=False)
if r.ok:
print("Logout {} successful! \n".format(session_dict['url']))
return
else:
print("HTTP Code: %i \n %s \n Message %s" % (r.status_code, r.reason, r.text))
return
Getting LLDP neighbors
After getting the authentication cookie, we can send an HTTP get request to get the LLDP neighbors in a JSON format. We save that information and print out a simple table.
def get_all(**session_dict):
print("Getting all LLDP neighbors")
cookies = {'sessionId': session_dict["cookie"]}
r = session_dict['s'].get(session_dict['url'] + "lldp/remote-device", cookies=cookies, verify=False)
if r.ok:
return r.json()
else:
print("HTTP Code: %i \n %s \n Message %s" % (r.status_code, r.reason, r.text))
return
def print_lldp(lldp_data):
for n in lldp_data['lldp_remote_device_element']:
print("#############################################")
print("Local Port: \t\t {}".format(n['local_port']))
print("Remote Port: \t\t {}".format(n['port_id']))
print("Remote Device: \t\t {}".format(n['system_name']))
print("Remote ID: \t\t\t {}".format(n['chassis_id']))
print("#############################################\n")
Changing the port name
Now we can use the LLDP data to change the port name. I decided to change the name to the remote device name followed by the remote port.
For that, we once again use our cookie and send an HTTP put to change it:
import json
def new_name(port, port_name, **session_dict):
print("Changing name of port {} to {}\n".format(port, port_name))
cookies = {'sessionId': session_dict["cookie"]}
json_name = json.dumps({'id': port, 'name': port_name})
r = session_dict['s'].put(session_dict['url'] + f"/ports/{port}", cookies=cookies, data=json_name,
verify=False)
if r.ok:
print("Change successful!\n")
return
else:
print("HTTP Code: %i \n %s \n Message %s" % (r.status_code, r.reason, r.text))
return
Checking on the switch
We can now run all blocks together to see our change:
import api_connect
import api_lldp
import api_port
def name_int_lldp(switchdata):
base_url = f'https://{switchdata["switchip"]}/rest/{switchdata["version"]}/'
# Get login cookie
print("Starte Login...\n")
connection = api_connect.login(base_url, switchdata['username'], switchdata['password'])
print("Login succesfull: {}\n".format(connection['cookie']))
# Create dict for connection info
session_dict = dict(s=connection['s'], cookie=connection['cookie'], url=base_url, ip=switchdata['switchip'])
# Get LLDP neighbors
neigh = api_lldp.get_all(**session_dict)
api_lldp.print_lldp(neigh)
for n in neigh:
api_port.name(url, login_cookie, n['local_port'], n['system_name'] + '__' + n['port_id'])
api_connect.logout(**session_dict)
if __name__ == "__main__":
switch_info = {
"switchip": "172.16.78.65",
"username": "admin",
"password": "Aruba123",
"version": "v7"
}
name_int_lldp(**switch_info )
On the switch:
Switch01# show name
Port Names
Port Type Name
----- ---------- ---------------------------------------------------------
1 100/1000T 5710_01__GigabitEthernet1/0/24
2 100/1000T
3 100/1000T
4 100/1000T
5 100/1000T
6 100/1000T
7 100/1000T
8 100/1000T Aruba-2930F-8G-PoEP-2SFPP__1
9
10