Skip to content

Instantly share code, notes, and snippets.

@martbhell
Created May 4, 2022 12:12
Show Gist options
  • Save martbhell/ffa751efd09f156bc6eb3d4ce7890460 to your computer and use it in GitHub Desktop.
Save martbhell/ffa751efd09f156bc6eb3d4ce7890460 to your computer and use it in GitHub Desktop.
import json
import traceback
from pyaoscx.session import Session
from pyaoscx.vlan import Vlan
from pyaoscx.mac import Mac
import urllib3
urllib3.disable_warnings()
# There are two approaches to workflows, both using the session.
# Older API versions: 10.0.4 and v1
VERSION = "10.08"
def get_aruba_macs():
switch_ip = "10.1.1.1"
sessio = Session(switch_ip, VERSION)
password = "censored_password"
sessio.open("censored_read_only_user", password)
allmacs = {} # { vlanid: [ "dynamic,mac1", "dynamic, mac2"], vlanid2: [..], .. }
#########
### Get all MACs from all VLANs using pyaoscx library
# Try block is used so that session closes even on error.
try:
## Get all MACs from all VLANs using pyaoscx library
allvlans = Vlan.get_all(sessio)
for vlan in allvlans:
vlana = Vlan(sessio, vlan)
allmacs[vlana.id] = []
macs = Mac.get_all(sessio, parent_vlan=vlana)
for mac in macs:
# mac variable here is "dynamic,AA%3ABB...."
allmacs[vlana.id].append(mac)
# no mac.port available here :/
print(type(macs[mac]))
print(dir(macs[mac]))
print(macs[mac].port)
except Exception as error:
print(f"Ran into exception: {error}. Closing session.")
print(traceback.print_exc())
finally:
# At the end, the session MUST be closed
sessio.close()
print(allmacs)
get_aruba_macs()
# Output of this is:
#$ python example_mac.py
#<class 'pyaoscx.mac.Mac'>
#['_Mac__modified', '__abstractmethods__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_extract_missing_parameters_from', '_get_and_copy_data', '_get_data', '_is_replace_required', '_mac_path', '_original_attributes', '_parent_vlan', '_post_data', '_put_data', '_send_data', '_set_configuration_items', '_set_vlan', '_uri', 'apply', 'base_uri', 'config_attrs', 'connected', 'create', 'delete', 'deprecated', 'display_name', 'from_id', 'from_response', 'from_uri', 'get', 'get_all', 'get_info', 'get_info_format', 'get_uri', 'indices', 'info_format', 'mac_address', 'mac_format', 'materialized', 'modified', 'path', 'resource_uri_name', 'session', 'update', 'uri_path', 'was_modified']
#Ran into exception: 'Mac' object has no attribute 'port'. Closing session.
#Traceback (most recent call last):
# File "example_mac.py", line 46, in get_aruba_macs
# print(macs[mac].port)
#AttributeError: 'Mac' object has no attribute 'port'
#None
#{1: [], 51: ['dynamic,AA%3ABB%3ACC%3ADD%3AEE%3AFF']}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment