/
opengear-netbox.py
80 lines (69 loc) · 2.69 KB
/
opengear-netbox.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# Find API documentation here: https://ftp.opengear.com/download/documentation/api/cs/og-cs-rest-api-specification-latest.html
import pynetbox
import configparser
import requests
import argparse
config = configparser.ConfigParser()
config.read('config.ini')
parser = argparse.ArgumentParser(description='Update the ports on an Opengear serial console with information from Netbox')
parser.add_argument('--console', help='The hostname of the console')
args = parser.parse_args()
session = requests.Session()
session.verify = False
requests.packages.urllib3.disable_warnings()
CONSOLE = args.console
API_BASE_STRING = 'https://' + CONSOLE + '/api/v1.8/'
r = session.post(API_BASE_STRING + 'sessions', json={
'username': config['opengear']['Username'],
'password': config['opengear']['Password']
})
r.raise_for_status()
response = r.json()
opengear_token = response['session']
netbox = pynetbox.api(config['netbox']['URI'], config['netbox']['Token'])
ports = netbox.dcim.console_server_ports.filter(device=CONSOLE)
for port in ports:
port_api_name = port['name'].replace(" ", "").lower()
if port['connected_endpoints'] is not None and len(port['connected_endpoints']) > 0:
device_name = port['connected_endpoints'][0]['device']['name']
print(port_api_name + " connected device: " + device_name)
new_serial_port = {
"serialport": {
"label": device_name,
"modeSettings": {
"consoleServer": {
"ssh": {
"enabled": True,
"unauthenticated": False
},
"webShell": {
"enabled": True
},
}
},
"mode": "consoleServer",
"hardwareSettings": {
"pinout": "X2",
"protocol": "RS232",
"uart": {
"parity": "none",
"baud": "115200",
"stopBits": "1",
"dataBits": "8",
"flowControl": "none",
"dtrMode": "alwayson"
}
},
"logging": {
"priority": "Default",
"facility": "Default",
"level": "access"
},
},
}
r = session.patch(API_BASE_STRING + 'serialPorts/' + port_api_name, json=new_serial_port, headers={
'Authorization': 'Token ' + opengear_token
})
r.raise_for_status()
print(r.json())