Script to remove all interfaces, change tenant, change location #15144
-
Hey all, I'm working on a script to prepare hosts for decommissioning. Part of our process for doing that is removing all cables, changing the tenant, and moving the host to a storage location. To date, I have been able to do all of this via Python script that uses the API via HTTP requests, but I want to move this into the Scripts pane in NetBox and have a GUI to accompany it so members of my team can have easier access to doing this. Are there any functions or anything I should be aware of that can aide me in simply deleting all cables connected to interfaces on a given host? I'm sure I can figure out setting the tenant and location otherwise, but recursively removing cables is where I've been getting stuck. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
You can iterate over the interfaces on a device, and then delete the cable attached to that interface (there can only ever be one per interface). I don't think there's any need for recursion, unless you want to follow a chain of cables through patch panels etc and delete all of those cables - is that the case? A good way to try out Django ORM code interactively is within nbshell, which is most easily started using
Then you can experiment:
If that's all you need, then you can simply delete that cable. Iteration is just a question of looping over the interfaces:
If you're using cables with multiple terminations though, you might want to delete just the termination into that interface, rather than the entire cable (which deletes all terminations). However, doing so risks leaving "orphaned" cables which are not connected at one or both ends. There is a WIP script here which can find orphans.
If you want to trace where the cable goes then there's
Again beware that you can't blindly delete call cables in the path because they might be connecting other things (e.g. one cable may link device 1 interface A and device 2 interface B at one end, to device 3 interface C and device 4 interface D at the other end). Such cables are uncommon, but typically used for things like QSFP+ breakout cables. |
Beta Was this translation helpful? Give feedback.
You can iterate over the interfaces on a device, and then delete the cable attached to that interface (there can only ever be one per interface). I don't think there's any need for recursion, unless you want to follow a chain of cables through patch panels etc and delete all of those cables - is that the case?
A good way to try out Django ORM code interactively is within nbshell, which is most easily started using