Page MenuHomePhabricator
Paste P11632

Netbox cleanup of offline device's interfaces and IP addresses
ActivePublic

Authored by Volans on Jun 22 2020, 10:16 PM.
Tags
None
Referenced Files
F31875711: raw.txt
Jun 22 2020, 10:16 PM
Subscribers
None
(venv) root@netbox1001:/srv/deployment/netbox/deploy/src/netbox# python manage.py nbshell
/srv/deployment/netbox/deploy-cache/revs/5251cf14234a945a529f72863af30797baeed8fd/src/netbox/netbox/settings.py:192: UserWarning: The 'webhooks' REDIS configuration section has been renamed to 'tasks'. Please update your configuration as support for the old name will be removed in a future release.
"The 'webhooks' REDIS configuration section has been renamed to 'tasks'. Please update your configuration as "
### NetBox interactive shell (netbox1001)
### Python 3.7.3 | Django 3.0.6 | NetBox 2.8.4
### lsmodels() will show available models. Use help(<model>) for more info.
>>> import uuid
>>> request_id = uuid.uuid4()
>>> user = User.objects.get(username='volans')
>>> devices = Device.objects.filter(status='offline', device_role__slug='server', interfaces__gt=0)
>>> len(devices)
125
>>> def clean_interfaces(device, user, request_id):
... for interface in device.interfaces.all():
... for address in interface.ip_addresses.all():
... print(f'Deleting address {address} of device {device}')
... log = address.to_objectchange('delete')
... log.request_id = request_id
... log.user = user
... log.save()
... address.delete()
... print(f'Deleting iface {interface} of device {device}')
... log = interface.to_objectchange('delete')
... log.request_id = request_id
... log.user = user
... log.save()
... interface.delete()
... device.save()
...
>>> for device in devices:
... clean_interfaces(device, user, request_id)
...
Deleting address 10.64.40.205/26 of device americium
Deleting iface mgmt of device americium
Deleting address 10.65.5.180/16 of device analytics1001
Deleting iface mgmt of device analytics1001
Deleting address 10.65.5.184/16 of device analytics1002
Deleting iface mgmt of device analytics1002
Deleting address 10.65.3.215/16 of device analytics1032
Deleting iface mgmt of device analytics1032
Deleting address 10.65.2.127/16 of device cobalt
Deleting iface mgmt of device cobalt
Deleting iface mgmt of device cp2016
Deleting iface mgmt of device cp2017
Deleting iface mgmt of device cp2018
Deleting iface mgmt of device cp2019
Deleting iface mgmt of device cp2020
Deleting iface mgmt of device cp2022
Deleting iface mgmt of device cp2023
Deleting iface mgmt of device cp2024
Deleting iface mgmt of device cp2025
Deleting iface mgmt of device cp2026
Deleting address 10.65.2.4/16 of device dbproxy1001
Deleting iface mgmt of device dbproxy1001
Deleting address 10.65.2.3/16 of device dbproxy1002
Deleting iface mgmt of device dbproxy1002
Deleting address 10.65.2.24/16 of device dbproxy1007
Deleting iface mgmt of device dbproxy1007
Deleting address 10.65.2.23/16 of device dbproxy1010
Deleting iface mgmt of device dbproxy1010
Deleting address 10.65.2.22/16 of device dbproxy1011
Deleting iface mgmt of device dbproxy1011
Deleting address 10.65.7.60/16 of device elastic1017
Deleting iface mgmt of device elastic1017
Deleting address 10.65.7.61/16 of device elastic1018
Deleting iface mgmt of device elastic1018
Deleting address 10.65.7.62/16 of device elastic1019
Deleting iface mgmt of device elastic1019
Deleting address 10.65.7.63/16 of device elastic1020
Deleting iface mgmt of device elastic1020
Deleting address 10.65.7.65/16 of device elastic1022
Deleting iface mgmt of device elastic1022
Deleting address 10.65.7.66/16 of device elastic1023
Deleting iface mgmt of device elastic1023
Deleting address 10.65.7.67/16 of device elastic1024
Deleting iface mgmt of device elastic1024
Deleting address 10.65.7.68/16 of device elastic1025
Deleting iface mgmt of device elastic1025
Deleting address 10.65.7.69/16 of device elastic1026
Deleting iface mgmt of device elastic1026
Deleting address 10.65.7.70/16 of device elastic1027
Deleting iface mgmt of device elastic1027
Deleting address 10.65.7.71/16 of device elastic1028
Deleting iface mgmt of device elastic1028
Deleting address 10.65.7.72/16 of device elastic1029
Deleting iface mgmt of device elastic1029
Deleting address 10.65.7.73/16 of device elastic1030
Deleting iface mgmt of device elastic1030
Deleting address 10.65.7.74/16 of device elastic1031
Deleting iface mgmt of device elastic1031
Deleting address 10.193.1.235/16 of device es2001
Deleting iface mgmt of device es2001
Deleting address 10.193.1.236/16 of device es2002
Deleting iface mgmt of device es2002
Deleting address 10.193.1.237/16 of device es2003
Deleting iface mgmt of device es2003
Deleting address 10.193.1.238/16 of device es2004
Deleting iface mgmt of device es2004
Deleting iface mgmt of device labsdb1006
Deleting iface mgmt of device labsdb1007
Deleting address 10.65.3.106/16 of device labstore1001
Deleting iface mgmt of device labstore1001
Deleting address 10.65.3.107/16 of device labstore1002
Deleting iface mgmt of device labstore1002
Deleting address 10.65.4.18/16 of device lvs1007
Deleting iface mgmt of device lvs1007
Deleting address 10.65.4.19/16 of device lvs1008
Deleting iface mgmt of device lvs1008
Deleting address 10.65.4.20/16 of device lvs1009
Deleting iface mgmt of device lvs1009
Deleting address 10.65.4.21/16 of device lvs1010
Deleting iface mgmt of device lvs1010
Deleting address 10.65.4.22/16 of device lvs1011
Deleting iface mgmt of device lvs1011
Deleting address 10.65.4.23/16 of device lvs1012
Deleting iface mgmt of device lvs1012
Deleting address 10.65.2.71/16 of device mw1221
Deleting iface mgmt of device mw1221
Deleting address 10.65.2.72/16 of device mw1222
Deleting iface mgmt of device mw1222
Deleting address 10.65.2.73/16 of device mw1223
Deleting iface mgmt of device mw1223
Deleting address 10.65.2.74/16 of device mw1224
Deleting iface mgmt of device mw1224
Deleting address 10.65.2.75/16 of device mw1225
Deleting iface mgmt of device mw1225
Deleting address 10.65.2.76/16 of device mw1226
Deleting iface mgmt of device mw1226
Deleting address 10.65.2.77/16 of device mw1227
Deleting iface mgmt of device mw1227
Deleting address 10.65.2.78/16 of device mw1228
Deleting iface mgmt of device mw1228
Deleting address 10.65.2.79/16 of device mw1229
Deleting iface mgmt of device mw1229
Deleting address 10.65.2.80/16 of device mw1230
Deleting iface mgmt of device mw1230
Deleting address 10.65.2.81/16 of device mw1231
Deleting iface mgmt of device mw1231
Deleting address 10.65.2.82/16 of device mw1232
Deleting iface mgmt of device mw1232
Deleting address 10.65.2.83/16 of device mw1233
Deleting iface mgmt of device mw1233
Deleting address 10.65.2.84/16 of device mw1234
Deleting iface mgmt of device mw1234
Deleting address 10.65.2.85/16 of device mw1235
Deleting iface mgmt of device mw1235
Deleting address 10.65.2.88/16 of device mw1238
Deleting iface mgmt of device mw1238
Deleting address 10.65.2.89/16 of device mw1239
Deleting iface mgmt of device mw1239
Deleting address 10.65.2.90/16 of device mw1240
Deleting iface mgmt of device mw1240
Deleting address 10.65.2.91/16 of device mw1241
Deleting iface mgmt of device mw1241
Deleting address 10.65.2.92/16 of device mw1242
Deleting iface mgmt of device mw1242
Deleting address 10.65.2.93/16 of device mw1243
Deleting iface mgmt of device mw1243
Deleting address 10.65.2.94/16 of device mw1244
Deleting iface mgmt of device mw1244
Deleting address 10.65.2.95/16 of device mw1245
Deleting iface mgmt of device mw1245
Deleting address 10.65.2.96/16 of device mw1246
Deleting iface mgmt of device mw1246
Deleting address 10.65.2.97/16 of device mw1247
Deleting iface mgmt of device mw1247
Deleting address 10.65.2.98/16 of device mw1248
Deleting iface mgmt of device mw1248
Deleting address 10.65.2.99/16 of device mw1249
Deleting iface mgmt of device mw1249
Deleting address 10.65.2.100/16 of device mw1250
Deleting iface mgmt of device mw1250
Deleting address 10.65.2.101/16 of device mw1251
Deleting iface mgmt of device mw1251
Deleting address 10.65.2.102/16 of device mw1252
Deleting iface mgmt of device mw1252
Deleting address 10.65.2.103/16 of device mw1253
Deleting iface mgmt of device mw1253
Deleting address 10.65.2.104/16 of device mw1254
Deleting iface mgmt of device mw1254
Deleting address 10.65.2.105/16 of device mw1255
Deleting iface mgmt of device mw1255
Deleting address 10.65.2.106/16 of device mw1256
Deleting iface mgmt of device mw1256
Deleting address 10.65.2.107/16 of device mw1257
Deleting iface mgmt of device mw1257
Deleting address 10.65.2.108/16 of device mw1258
Deleting iface mgmt of device mw1258
Deleting address 10.65.1.45/16 of device mw1280
Deleting iface mgmt of device mw1280
Deleting address 10.193.2.50/16 of device mw2150
Deleting iface mgmt of device mw2150
Deleting address 10.193.2.51/16 of device mw2151
Deleting iface mgmt of device mw2151
Deleting address 10.193.2.52/16 of device mw2152
Deleting iface mgmt of device mw2152
Deleting address 10.193.2.53/16 of device mw2153
Deleting iface mgmt of device mw2153
Deleting address 10.193.2.54/16 of device mw2154
Deleting iface mgmt of device mw2154
Deleting address 10.193.2.55/16 of device mw2155
Deleting iface mgmt of device mw2155
Deleting address 10.193.2.56/16 of device mw2156
Deleting iface mgmt of device mw2156
Deleting address 10.193.2.57/16 of device mw2157
Deleting iface mgmt of device mw2157
Deleting address 10.193.2.58/16 of device mw2158
Deleting iface mgmt of device mw2158
Deleting address 10.193.2.59/16 of device mw2159
Deleting iface mgmt of device mw2159
Deleting address 10.193.2.60/16 of device mw2160
Deleting iface mgmt of device mw2160
Deleting address 10.193.2.61/16 of device mw2161
Deleting iface mgmt of device mw2161
Deleting address 10.193.2.62/16 of device mw2162
Deleting iface mgmt of device mw2162
Deleting address 10.193.2.63/16 of device mw2163
Deleting iface mgmt of device mw2163
Deleting address 10.193.2.64/16 of device mw2164
Deleting iface mgmt of device mw2164
Deleting address 10.193.2.65/16 of device mw2165
Deleting iface mgmt of device mw2165
Deleting address 10.193.2.66/16 of device mw2166
Deleting iface mgmt of device mw2166
Deleting address 10.193.2.67/16 of device mw2167
Deleting iface mgmt of device mw2167
Deleting address 10.193.2.68/16 of device mw2168
Deleting iface mgmt of device mw2168
Deleting address 10.193.2.69/16 of device mw2169
Deleting iface mgmt of device mw2169
Deleting address 10.193.2.70/16 of device mw2170
Deleting iface mgmt of device mw2170
Deleting address 10.193.2.71/16 of device mw2171
Deleting iface mgmt of device mw2171
Deleting address 10.193.2.72/16 of device mw2172
Deleting iface mgmt of device mw2172
Deleting address 10.193.2.73/16 of device mw2173
Deleting iface mgmt of device mw2173
Deleting address 10.193.2.74/16 of device mw2174
Deleting iface mgmt of device mw2174
Deleting address 10.193.2.75/16 of device mw2175
Deleting iface mgmt of device mw2175
Deleting address 10.193.2.76/16 of device mw2176
Deleting iface mgmt of device mw2176
Deleting address 10.193.2.77/16 of device mw2177
Deleting iface mgmt of device mw2177
Deleting address 10.193.2.78/16 of device mw2178
Deleting iface mgmt of device mw2178
Deleting address 10.193.2.79/16 of device mw2179
Deleting iface mgmt of device mw2179
Deleting address 10.193.2.80/16 of device mw2180
Deleting iface mgmt of device mw2180
Deleting address 10.193.2.81/16 of device mw2181
Deleting iface mgmt of device mw2181
Deleting address 10.193.2.82/16 of device mw2182
Deleting iface mgmt of device mw2182
Deleting address 10.193.2.83/16 of device mw2183
Deleting iface mgmt of device mw2183
Deleting address 10.193.2.84/16 of device mw2184
Deleting iface mgmt of device mw2184
Deleting address 10.193.2.85/16 of device mw2185
Deleting iface mgmt of device mw2185
Deleting address 10.193.2.86/16 of device mw2186
Deleting iface mgmt of device mw2186
Deleting address 10.193.1.65/16 of device oresrdb2002
Deleting iface mgmt of device oresrdb2002
Deleting address 10.65.3.57/16 of device rhodium
Deleting iface mgmt of device rhodium
Deleting address 10.65.2.111/16 of device wmf4658
Deleting iface mgmt of device wmf4658
>>>