Skip to main content

Shared lib of functions and tools for NetBox Custom Scripts

Project description

NetBox Scripts Library

A shared library of utility functions and tools for NetBox Custom Scripts.

Overview

This package provides reusable utilities for NetBox custom scripts, including:

  • Email notification and validation
  • URL generation for NetBox objects
  • Markdown link formatting

Installation

pip install netbox_scripts_lib

Or from source:

git clone <repository-url>
cd netbox_scripts_lib
pip install .

For development:

pip install -e .

Usage

Import the utilities in your NetBox custom scripts:

from netbox_scripts_lib import prepare_and_send_email, get_markdown_link, get_object_url
from extras.scripts import Script

class MyScript(Script):
    def run(self, data, commit):
        # Your script logic here
        results = []
        status = "Failed"  # or "Success"

        # Send notification email on failure
        prepare_and_send_email(
            script=self,
            subject="Script Execution Alert",
            emails="admin@example.com;team@example.com",
            status=status,
            all_results=results
        )

        # Generate markdown links for NetBox objects
        link = get_markdown_link(some_netbox_object)
        self.log_info(f"Created: {link}")

Available Functions

prepare_and_send_email(script, subject, emails, status, all_results, sender=SENDER)

Sends email notifications for script execution. Only sends emails when status is NOT "Success" (notification-on-failure pattern).

Parameters:

  • script: NetBox Script instance (self)
  • subject: Email subject line
  • emails: Semicolon-separated email addresses (e.g., "user1@example.com;user2@example.com")
  • status: Script execution status (sends only if not "Success")
  • all_results: List of result messages to include in email body
  • sender: Email sender address (optional, defaults to "noreply@cesnet.cz")

Returns: Status message string

is_valid_email(email)

Validates an email address using regex pattern.

Parameters:

  • email: Email address string

Returns: Boolean

get_object_url(object)

Generates the full URL for a NetBox object.

Parameters:

  • object: NetBox model instance (optional)

Returns: Full URL string or empty string if object is None

get_markdown_link(object)

Generates a markdown-formatted link for a NetBox object.

Parameters:

  • object: NetBox model instance (optional)

Returns: Markdown link string in format [#id name](url) or empty string if object is None

Requirements

  • Django >= 3.0
  • NetBox (must be run within a NetBox environment)

Building

To build a distribution package:

python setup.py sdist

Built package will be in dist/ directory.

Author

Jiri Vrany (jiri.vrany@cesnet.cz)

License

See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netbox_scripts_lib-0.3.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file netbox_scripts_lib-0.3.tar.gz.

File metadata

  • Download URL: netbox_scripts_lib-0.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for netbox_scripts_lib-0.3.tar.gz
Algorithm Hash digest
SHA256 9221542b49bdd9cf5c96383b0f71d66855ac46b4c7e9cb1bb4414ae7656fbcad
MD5 2a7efdf9fea0aa30b8ec01617fe56a29
BLAKE2b-256 6479d8d471d33e697678133ea1ab577226e042b1803c5239744733563a549e71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page