How to use pynetbox inside a custom script? #17168
Replies: 2 comments 2 replies
-
One possible answer is to instruct all users to create an API token with a specific name (e.g. try:
netbox_token = self.request.user.tokens.get(description='local').key
except Token.DoesNotExist:
raise AbortScript("You must create an API token with name: `local` and allowed IPs: `127.0.0.1/32`.") |
Beta Was this translation helpful? Give feedback.
-
The better, canonical way is not to use pynetbox from within a custom script at all. You should use the Django ORM instead. Yes, it's a different interface to learn, but your custom scripts will be much safer because they run inside a database transaction (i.e. all-or-nothing with rollback if something fails) |
Beta Was this translation helpful? Give feedback.
-
How do you call pynetbox from within a custom script? I don't want to store an API token in the script because then the changelogs will always show that user's name, instead of the user who ran the script. I tried creating a temporary token in the script but that doesn't work, since the script runs atomically, that token is not actually available yet.
Example script, which fails:
RequestError: The request failed with code 403 Forbidden: {'detail': 'Invalid token'}
Beta Was this translation helpful? Give feedback.
All reactions