Automates the bulk synchronization of network device data from LibreNMS to NetBox using the netbox-librenms-plugin. This script processes multiple sites and devices automatically, syncing interfaces, cables, IP addresses, and intelligently setting primary IPs.
Instead of manually clicking through the LibreNMS plugin UI for each device:
- Click "Refresh Interfaces" β Select all β Click "Sync"
- Click "Refresh Cables" β Select all β Click "Sync"
- Click "Refresh IP Addresses" β Select all β Click "Sync"
- Manually select and set primary IP
This script does all of that automatically for every device at specified sites.
Time savings: ~5-7 minutes per device β ~3 minutes for an entire site
- NetBox (tested on v3.6+) with the netbox-librenms-plugin installed
- LibreNMS configured and monitoring your devices
- Python 3.8+
The NetBox user account (for both web login and API token) needs:
dcim.view_device- View devicesdcim.change_device- Update device primary IPipam.view_ipaddress- View IP addresses- Access to the LibreNMS plugin interface
Generate an API token in NetBox:
- Log into NetBox
- Click your username (top right) β "API Tokens"
- Click "Add Token"
- Give it a name (e.g., "Bulk Sync Script")
- Select the required permissions above
- Copy the token immediately (you won't see it again!)
# Clone the repository
git clone https://github.com/yourusername/netbox-librenms-bulk-sync.git
cd netbox-librenms-bulk-sync
# Install dependencies
pip install -r requirements.txt
# Copy and configure environment variables
cp .env.example .env
# Edit .env with your NetBox credentialsEdit .env with your NetBox details:
NETBOX_URL=http://your-netbox-instance:8080
NETBOX_TOKEN=your_api_token_from_netbox
NETBOX_USERNAME=your_username
NETBOX_PASSWORD=your_passwordEdit the target_sites list in manual_sync.py (around line 471):
target_sites = [
"your-site-slug", # Must match NetBox site slug (lowercase, hyphenated)
"another-site",
]Note: Site names should match your NetBox site slugs (not display names). Find these in NetBox at: Organization β Sites β [Your Site] β Slug field.
# Load environment variables and run
export $(cat .env | xargs)
python manual_sync.pyIf you prefer to run in a container:
docker run --rm \
--env-file .env \
-v $(pwd):/app \
python:3.11-slim \
bash -c "cd /app && pip install -r requirements.txt && python manual_sync.py"The script uses two authentication methods because the LibreNMS plugin has different requirements:
- Used to interact with the LibreNMS Plugin web UI
- Scrapes HTML forms using BeautifulSoup
- Triggers refresh/sync actions via POST requests
- Why: The plugin doesn't expose an API for these actions
- Used to query devices via NetBox REST API
- Sets primary IP addresses
- Why: Standard NetBox API for data operations
For each device, the script syncs:
- All physical and virtual interfaces
- Interface names, types, MAC addresses
- Enabled/disabled status
- Handles pagination (50 interfaces per page)
- CDP/LLDP discovered connections
- Links to neighboring devices
- Creates cable objects in NetBox
- All IPv4/IPv6 addresses discovered on interfaces
- Assigns addresses to correct interfaces
Intelligently selects the primary IP by preferring (in order):
- IP on management interfaces:
Vlan1,vlan1,mgmt0,Management0,lo0,Loopback0 - First available IP if no management interface found
- Check your
NETBOX_USERNAMEandNETBOX_PASSWORD - Verify the user has permissions to log into the web interface
- Check
NETBOX_URLis correct and accessible
- Check your
NETBOX_TOKENis valid - Verify token has required permissions (
dcim.view_device, etc.) - Check
NETBOX_URLpoints to your NetBox instance - Ensure NetBox API is accessible from where you're running the script
- Verify site slug matches exactly (case-sensitive)
- Check devices have role='switch' in NetBox
- Confirm devices are assigned to the correct site
- Device may not exist in LibreNMS
- Check LibreNMS plugin is installed and configured in NetBox
- Verify device has been discovered in LibreNMS
- Normal behavior if device has fewer interfaces than expected
- Check logs for actual interface count discovered
# Install missing dependencies
pip install -r requirements.txt
# Or install individually
pip install requests beautifulsoup4 psycopg2-binary======================================================================
Starting NetBox LibreNMS Plugin Bulk Sync
======================================================================
======================================================================
Processing Site: Winchester
======================================================================
Fetching devices for site 'winchester' with role 'switch'...
Found 3 devices via API.
Found 3 switch(es) to process in Winchester.
----------------------------------------------------------------------
Processing device 1/3: sw-winchester-core01 (ID: 42)
----------------------------------------------------------------------
--- Step 1: Syncing INTERFACES ---
Triggering 'Refresh Interfaces' to populate data from LibreNMS...
β Successfully triggered interface data refresh.
Starting paginated interface discovery and sync...
--- Fetching Page 1 ---
Found 48 interfaces on page 1
Syncing 48 interfaces from page 1...
β Successfully synced 48 interfaces.
--- Fetching Page 2 ---
No interfaces found on page 2. End of data.
Paginated sync complete: Synced 48 interfaces across 1 pages.
--- Step 2: Syncing CABLES ---
Triggering 'Refresh Cables' from LibreNMS...
β Successfully triggered cable data refresh.
Found 6 cables to sync.
β Successfully synced 6 cables.
--- Step 3: Syncing IP ADDRESSES ---
Triggering 'Refresh IP Addresses' from LibreNMS...
β Successfully triggered IP Address data refresh.
Found 12 IP Addresses to sync.
β Successfully synced 12 IP Addresses.
--- Step 4: Setting PRIMARY IP ---
Found candidate IP 10.1.100.1/24 on interface 'Vlan1'.
Setting 10.1.100.1/24 (ID: 156) as the primary IP for device 42...
β Successfully set the primary IP.
β Completed all sync processes for sw-winchester-core01
[... continues for remaining devices ...]
======================================================================
Bulk sync process complete!
======================================================================
- Never commit
.envfile to version control - The
.gitignorefile is configured to exclude.env - Use environment variables or a secrets manager in production
- Use the minimum required permissions
- Create a dedicated service account for automation
- Rotate tokens periodically
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- NetBox - The amazing network IPAM/DCIM tool
- LibreNMS - Network monitoring system
- netbox-librenms-plugin - The plugin that makes this integration possible
- Issues: Please open an issue on GitHub for bugs or feature requests
- Discussions: Use GitHub Discussions for questions and community support
- YouTube Video showing plugin - Video of Plugin and Python sync in a lab environment.