Repository navigation
Expand file tree
/
Copy pathnetbox-sync.sh
More file actions
164 lines (137 loc) · 6.55 KB
/
Copy pathnetbox-sync.sh
File metadata and controls
164 lines (137 loc) · 6.55 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# Finales Netbox Switch Synchronisation Script
set -euo pipefail
# Konfiguration
NETBOX_URL="http://192.168.2.6:8462"
API_KEY="52327f1054ab313833846a61f518b8c97d578d49"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MANUFACTURERS_DIR="${SCRIPT_DIR}/manufacturers"
# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() {
echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Netbox API Call
netbox_api_call() {
local endpoint="$1"
local url="${NETBOX_URL}/api${endpoint}"
curl -s -H "Authorization: Token ${API_KEY}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
"$url"
}
# Hauptfunktion
main() {
log "Starte Netbox Switch Synchronisation"
# Prüfe Abhängigkeiten
if ! command -v jq &> /dev/null; then
log_error "jq ist nicht installiert"
exit 1
fi
if ! command -v curl &> /dev/null; then
log_error "curl ist nicht installiert"
exit 1
fi
# Erstelle Hersteller-Ordner
mkdir -p "$MANUFACTURERS_DIR"
# Lade Switches
log "Lade Switches aus Netbox..."
local switches_response=$(netbox_api_call "/dcim/devices/")
if [[ -z "$switches_response" ]]; then
log_error "Fehler beim Abrufen der Switches"
exit 1
fi
# Verarbeite jeden Switch
local switch_count=$(echo "$switches_response" | jq '.results | length')
log "Gefunden: $switch_count Switches"
for ((i=0; i<switch_count; i++)); do
local switch=$(echo "$switches_response" | jq -r ".results[$i]")
local manufacturer=$(echo "$switch" | jq -r '.device_type.manufacturer.name // ""')
# Nur Switches mit Hersteller verarbeiten
if [[ "$manufacturer" != "" && "$manufacturer" != "null" ]]; then
local switch_id=$(echo "$switch" | jq -r '.id')
local switch_name=$(echo "$switch" | jq -r '.name')
local manufacturer_slug=$(echo "$switch" | jq -r '.device_type.manufacturer.slug')
local model=$(echo "$switch" | jq -r '.device_type.model')
local model_slug=$(echo "$switch" | jq -r '.device_type.model')
local primary_ip=$(echo "$switch" | jq -r '.primary_ip4.address // ""')
local status=$(echo "$switch" | jq -r '.status.value')
local site=$(echo "$switch" | jq -r '.site.name // ""')
log "Verarbeite Switch: $switch_name ($manufacturer_slug/$model_slug)"
# Erstelle Ordnerstruktur
local model_dir="${MANUFACTURERS_DIR}/${manufacturer_slug}/${model_slug}"
mkdir -p "$model_dir"
# Lade Interfaces
local interfaces_response=$(netbox_api_call "/dcim/interfaces/?device_id=${switch_id}")
local interfaces_json="[]"
if [[ -n "$interfaces_response" ]]; then
local interface_count=$(echo "$interfaces_response" | jq '.results | length')
if [[ "$interface_count" -gt 0 ]]; then
interfaces_json=$(echo "$interfaces_response" | jq -r '.results[] | {
id: .id,
name: .name,
type: .type.value,
enabled: .enabled,
mtu: .mtu,
mac_address: (if .mac_address then .mac_address else "" end),
description: (if .description then .description else "" end),
vlans: (if .tagged_vlans then [.tagged_vlans[]?.vid // empty] | sort else [] end),
untagged_vlan: (if .untagged_vlan then .untagged_vlan.vid else null end),
mode: (if .mode then .mode.value else "access" end),
dhcp_trust: (if .description and (.description | test("dhcp:trusted"; "i")) then "trusted" elif .description and (.description | test("dhcp:untrusted"; "i")) then "untrusted" else null end),
arp_trust: (if .description and (.description | test("arp:trusted"; "i")) then "trusted" elif .description and (.description | test("arp:untrusted"; "i")) then "untrusted" else null end),
stp_status: (if .description and (.description | test("stp:enabled"; "i")) then "enabled" elif .description and (.description | test("stp:disabled"; "i")) then "disabled" else null end)
}' | jq -s .)
fi
fi
# Lade VLANs
local vlans_response=$(netbox_api_call "/ipam/vlans/")
local vlans_json="[]"
if [[ -n "$vlans_response" ]]; then
local vlan_count=$(echo "$vlans_response" | jq '.results | length')
if [[ "$vlan_count" -gt 0 ]]; then
vlans_json=$(echo "$vlans_response" | jq -r '.results[] | {
id: .id,
vid: .vid,
name: .name,
description: (if .description then .description else "" end),
status: .status.value
}' | jq -s .)
fi
fi
# Erstelle Switch-Objekt
local switch_obj=$(jq -n --arg id "$switch_id" --arg name "$switch_name" --arg manufacturer "$manufacturer" --arg manufacturer_slug "$manufacturer_slug" --arg model "$model" --arg model_slug "$model_slug" --arg primary_ip "$primary_ip" --arg status "$status" --arg site "$site" '{
id: ($id | tonumber),
name: $name,
manufacturer: $manufacturer,
manufacturer_slug: $manufacturer_slug,
model: $model,
model_slug: $model_slug,
primary_ip: $primary_ip,
status: $status,
site: $site
}')
# Erstelle JSON-Datei
local data_file="${model_dir}/netbox_data.json"
jq -n --argjson switch "$switch_obj" --argjson interfaces "$interfaces_json" --argjson vlans "$vlans_json" '{
switch: $switch,
interfaces: $interfaces,
vlans: $vlans
}' | sudo tee "$data_file" > /dev/null
log_success "Daten für $switch_name gespeichert in $data_file"
fi
done
log_success "Netbox Synchronisation abgeschlossen"
}
# Script ausführen
main "$@"