Script file not be loaded #12813
-
Hello, folks! In 3.5.2, as I understand it, the approach to working with scripts has changed and everything now works through the GUI. Code: # Modules
from icmplib import ping
# Netbox artifcats
from extras.scripts import Script, ObjectVar, MultiObjectVar
from dcim.models import Site, Device
# Classes
class DeviceChecker(Script):
class Meta:
name = "Validate state of network devices"
description = "Sample Script to set the status of the device based on its availability"
field_order = ("site", "devices")
site = ObjectVar(
model=Site
)
devices = MultiObjectVar(
model=Device,
query_params={
"site_id": "$site"
}
)
def run(self, data, commit) -> None:
print_in_nb = []
for device in data["devices"]:
result = ping(str(device.primary_ip.address.ip), count=3, interval=0.2, privileged=False)
if result.is_alive:
device.status = "active"
else:
device.status = "offline"
print_in_nb.append(f"{device.name} is set to {device.status}")
device.save()
return "\n".join(print_in_nb) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Yes, debugging scripts is currently a pain if they have an error which prevents them from being loaded in the first place. I gave a workaround at #12766. Basically you take a copy of your script, insert the following lines at the top of it:
Then run it using If I test your script this way, I get
because icmplib isn't in my virtualenv. I installed the required library temporarily using The long-term solution to this is for you to create |
Beta Was this translation helpful? Give feedback.
Yes, debugging scripts is currently a pain if they have an error which prevents them from being loaded in the first place.
I gave a workaround at #12766. Basically you take a copy of your script, insert the following lines at the top of it:
Then run it using
/opt/netbox/venv/bin/python myscript.py
(or you can dochmod +x myscript.py
then./myscript.py
). Errors which prevent the script being loaded should then be shown.If I test your scr…