Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NETBOX_URL=
NETBOX_TOKEN=
REPO_URL=https://github.com/netbox-community/devicetype-library.git
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ dmypy.json
.pyre/

# Editor
.vscode
.vscode

repo
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Alexander Gittings

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Netbox Device Type Import

This library is intended to be your friend and help you import all the device-types defined within the the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library).

> Tested working with 2.9.4, 2.10.4

## 🪄 Description

This script will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow it to import the device types you would like without copy and pasting them into the Netbox UI.

## 🚀 Getting Started

1. This script is written in Python, so lets setup a virtual environment.

```
git clone https://github.com/minitriga/Netbox-Device-Type-Library-Import
cd Netbox-Device-Type-Library-Import
python3 -m venv venv
source venv/bin/activate
```

2. Now that we have the basics setup, we'll need to install the requirements.

```
pip install -r requirements.txt
```

3. There are two variables that are required when using this script to import device types into your Netbox installation. (1) Your Netbox instance URL and (2) a token with **write rights**.

Copy the existing `.env.example` to your own `.env` file, and fill in the variables.

```
cp .env.example .env
vim .env
```

Finally, we are able to execute the script and import some device templates!

## 🔌 Usage

To use the script, simply execute the script as follows. Make sure you're still in the activated virtual environment we created before.

```
./nb-dt-import.py
```

This will clone the latest master branch from the `netbox-community/devicetype-library` from Github and install it into the `repo` subdirectory. If this directory already exists, it will perform a `git pull` to update the reposity instead.

Next, it will loop over every manufacturer and every device of every manufacturer and begin checking if your Netbox install already has them, and if not, creates them. It will skip preexisting manufacturers, devices, interfaces, etc. so as to not end up with duplicate entries in your Netbox instance.

### 🧰 Arguments

This script currently accepts a list of vendors as an arugment, so that you can selectively import devices.

To import only device by APC, for example:

```
./nb-dt-import.py --vendors apc
```

`--vendors` can also accept a space separated list of vendors if you want to import multiple.

```
./nb-dt-import.py --vendors apc juniper
```

## 🧑‍💻 Contributing

We're happy about any pull requests!

## 📜 License

MIT
7 changes: 6 additions & 1 deletion nb-dt-import.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env python3
from git import Repo, exc, RemoteProgress
from collections import Counter
from datetime import datetime
import yaml
import pynetbox
import glob
import argparse
import os
import settings

REPO_URL = 'https://github.com/netbox-community/devicetype-library.git'
REPO_URL = settings.REPO_URL
parser = argparse.ArgumentParser(description='Import Netbox Device Types')
parser.add_argument('--vendor', nargs='+',
help="List of vendors to import eg. apc cisco")
Expand All @@ -19,6 +21,7 @@
counter = Counter(added=0, updated=0, manufacturer=0)
nbUrl = settings.NETBOX_URL
nbToken = settings.NETBOX_TOKEN
startTime = datetime.now()


def update_package(path: str):
Expand Down Expand Up @@ -357,6 +360,8 @@ def createDeviceTypes(deviceTypes, nb):
createDeviceTypes(deviceTypes, nb)

print('---')
print('Script took {} to run'.format(datetime.now() - startTime))
print('{} devices created'.format(counter['added']))
print('{} interfaces/ports updated'.format(counter['updated']))
print('{} manufacturers created'.format(counter['manufacturer']))

68 changes: 0 additions & 68 deletions readme.md

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ requests==2.22.0
six==1.14.0
smmap2==2.0.5
urllib3==1.25.8
python-dotenv==0.15.0
7 changes: 5 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import os
from dotenv import load_dotenv
load_dotenv()

REPO_URL = str(os.getenv("REPO_URL"))
NETBOX_URL = str(os.getenv("NETBOX_URL"))
NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN"))

MANDATORY_ENV_VARS = ["NETBOX_URL", "NETBOX_TOKEN"]
MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"]

for var in MANDATORY_ENV_VARS:
if var not in os.environ:
raise EnvironmentError("Failed because {} is not set.".format(var))
raise EnvironmentError("Failed because {} is not set.".format(var))