• Chris Dixon

Upload bulk Interface info to Netbox

Netbox is a great tool to be used as part of an automation suite, to control the single source of truth of the intended state of your network. It has an abundant set of APIs for polling and uploading information, as well as very useful GUI features for performing bulk tasks. For example in the GUI you can upload a variety of information from csv files. One thing I found that was missing from the GUI bulk upload feature, which I needed, was the ability to upload interface details in bulk from CSV.


The below script makes use of one of the APIs, to allow you to add bulk interface information from a CSV file.


The CSV file will be in the following format:


name,enabled,is_connected,description,mode
Gi0/0,true,false,Mgmt - OOB,100
Gi1/0/1,true,true,FIREWALL-OUTSIDE,100

The Script to call the relevant Netbox API and carry out the upload task is below:


#! /usr/bin/python
import requests
import json
import socket
import sys
import os
import uuid
import random
import csv

API_ENDPOINT = "https://netbox.your_domain.xyz/api/dcim/interfaces/"
API_TOKEN = raw_input("Enter API Token: ")                        DEVICE_ID = raw_input("Enter Device ID: ") 


headers = {"Content-Type": "application/json",
           "Authorization": "Token {}".format(API_TOKEN)}
#test csv reading capability
with open('interfaces.csv', mode='r') as csv_file:
   csv_reader = csv.DictReader(csv_file)
   line_count = 0
   for row in csv_reader:                                                 
      # uncomment below if you want to see progress in the output
      # if line_count == 0:
      #    print("Column names are " + {.join(row)})
      #    line_count += 1
      # print(row['name'] + "is " + row['enabled'] + "enabled,  is " + row['is_connected'] + "connected and is in " + row['mode'] + " mode. Description is + " + row['description'])                        
            payload = {"device":DEVICE_ID,
                  "name": row['name'],
                  "enabled": row['enabled'],
                  "form_factor": 1000,
                  "is_connected": row['is_connected'],
                  "mode": row['mode'],
                  "description": row['description']
                 }

       print(payload)
       r = requests.post(url=API_ENDPOINT,data=json.dumps(payload),headers=headers,allow_redirects=False,verify=False)
       if int(r.status_code) == 201:
         break    # break here
         print('Got a 201')



The script above prompts you for an API key for access to your Netbox API, and the DeviceID (the Netbox DeviceID of whatever device it is that you are uploading the interface information about) at the time you run the script. Also bear in mind the Netbox URL would need amending to your own Netbox URL.

  • Twitter Social Icon
  • LinkedIn Social Icon

©2019 by Squanto Tech.

Follow us on