-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetbox_connection.py
More file actions
38 lines (32 loc) · 1.44 KB
/
netbox_connection.py
File metadata and controls
38 lines (32 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pynetbox
import requests
def connect_to_netbox(url, token):
"""
Connect to the Netbox API using the provided URL and token.
Args:
- url (str): The base URL of the Netbox instance.
- token (str): The authentication token for accessing the Netbox API.
Returns:
- netbox (pynetbox.core.api.Api): The Netbox API object configured to use the provided URL and token.
Raises:
- Exception: If the connection to Netbox fails.
"""
# Create a custom requests session with SSL verification disabled
session = requests.Session()
session.verify = False # Disabling SSL verification for the session
# Test the connection by making a direct request to /api/status/
try:
headers = {"Authorization": f"Token {token}"}
response = session.get(f"{url}/api/status/", headers=headers)
response.raise_for_status() # Raise an error for HTTP status codes 4xx/5xx
# Check if the response contains the "netbox-version" key
status_data = response.json()
if "netbox-version" in status_data:
# Create and return the Netbox API object
netbox = pynetbox.api(url, token)
netbox.http_session = session
return netbox
else:
raise Exception("Unexpected response from Netbox /api/status/ endpoint.")
except Exception as e:
raise Exception(f"Failed to connect to Netbox: {e}")