Verified Commit 2af4565f authored by Adrian Schollmeyer's avatar Adrian Schollmeyer
Browse files
parents
fem-netbox-user-scripts.conf
*.swp
# Changelog
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
* initial config file format
* Script `get-aruba-ap-uplinks.py`
# Scripts for Netbox users
## Configuration
Configuration is provided via a global config file.
The default path is `/etc/fem-netbox-user-scripts.conf`.
See `fem-netbox-user-scripts.example.conf` for an example configuration.
## con
## Dependencies
* python3
* extra python3 libraries: see `requirements.txt`
[auth]
url = https://netbox.fem.tu-ilmenau.de
token = token
#!/usr/bin/env python3
import pynetbox
import argparse
import sys
import configparser
DEFAULT_CONFIG_PATH="/etc/fem-netbox-user-scripts.conf"
# Use Slugs!
AP_ROLE="wlan-ap"
AP_MANUFACTURER="aruba"
def main():
arg_parser = create_argument_parser()
args = arg_parser.parse_args()
config = parse_config_file(args.config)
netbox = create_netbox_instance(
config.get("auth", "url"),
config.get("auth", "token")
)
if args.block:
aps = get_matching_aps(netbox, args.block)
else:
aps = get_matching_aps(netbox)
interfaces = get_interfaces(netbox, aps)
if args.switch:
uplink_switchports = get_uplink_switchports(netbox, interfaces,
args.switch)
else:
uplink_switchports = get_uplink_switchports(netbox, interfaces)
for port in uplink_switchports:
print(f"{port[0]}/{port[1]}")
return 0
def create_argument_parser():
parser = argparse.ArgumentParser(description="Tool to fetch uplink"
"switchports of Aruba APs from netbox")
parser.add_argument("--config", "-c", type=str,
default=DEFAULT_CONFIG_PATH, help="Path to configuration file")
parser.add_argument("--block", "-b", type=str,
help="Fetch only uplinks in the specified house")
parser.add_argument("--switch", "-s", type=str,
help="Fetch only uplinks for APs connected to this switch")
return parser
def parse_config_file(config_file_path):
try:
config = configparser.RawConfigParser()
config.read(config_file_path)
return config
except Exception as e:
raise Exception(f"Failed to read configuration file {config_file_path}!")
def create_netbox_instance(url, token):
netbox = pynetbox.api(url, token=token)
return netbox
def get_matching_aps(netbox, block=None):
devices = netbox.dcim.devices.filter(
role=AP_ROLE,
manufacturer=AP_MANUFACTURER,
site=block
)
return devices
def get_interfaces(netbox, aps):
interfaces = []
for ap in aps:
interfaces += netbox.dcim.interfaces.filter(
device = ap.name,
connected = True
)
return interfaces
def get_uplink_switchports(netbox, interfaces, switch=None):
switchports = []
for interface in interfaces:
if not switch == None and not (
interface.connected_endpoint.device.name == switch):
continue
switchports.append(
(interface.connected_endpoint.device.name,
interface.connected_endpoint.name
))
return switchports
if __name__ == '__main__':
sys.exit(main())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment