Step by step Netbox install

This tutorial covers the installation of Netbox onto Ubuntu Server version 19.1.

As always start with updating your apt repository. Once updated install the required preqs for installation including git which we will use to clone the latest version of netbox. Run the commands individually.

sudo apt-get update
sudo apt-get install -y postgresql libpq-dev
sudo apt-get install -y python3 python3-pip python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev redis-server zlib1g-dev

Once installed you need to create a new database and user.

sudo -u postgres psql

CREATE DATABASE netbox;

CREATE USER netbox WITH PASSWORD 'password';

GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;

exit

Now create the following directory and navigate to /opt

sudo mkdir -p /opt/netbox/ && cd /opt/

Now clone the netbox installation files from github and install the python preqs.

sudo git clone -b master https://github.com/netbox-community/netbox.git

cd netbox

sudo pip3 install -r requirements.txt

#optional
sudo pip3 install napalm
#optional
sudo  pip3 install django-storages

cd netbox/netbox/

Now copy the example configuration file and edit with your preferred IP username and password.

sudo cp configuration.example.py configuration.py

sudo vim configuration.py

The required fields are ALLOWED_HOSTS, NAME, USER, PASSWORD and SECRET_KEY in the DATABASE section of the configuration file. The allowed hosts field is not the IP addresses allowed to access the system, but the IPs or hostnames that netbox will be reachable from. For example, my VM has an IP of 192.168.93.132. So my allowed hosts configuration would look like this the below.

SECRET_KEY must be at least 50 characters long.

ALLOWED_HOSTS = ['192.168.93.132']

For those unfamiliar with editing using vim, press i to enter edit mode and press Esc to exit edit mode. Then type :wq! to save write to file(save) and quit.

Now move back into the top level netbox folder and run the database migration script.

cd ..

sudo python3 manage.py migrate

Now create an admin user for Netbox.

sudo python3 manage.py createsuperuser

Now run the last two admin tasks and start the netbox server.

sudo python3 manage.py collectstatic --no-input

sudo python3 manage.py runserver 0.0.0.0:80 --insecure

If you require more information on operating netbox see https://netbox.readthedocs.io/en/stable/

Advertisements

Scripting SSH access

Windows, Paramiko and Python

This tutorial covers scripting SSH access in Windows using Python and Paramiko. This can be used for a variety of tasks, including network automation and administration.

The following technologies are used:

Windows 10
Python 3
Paramiko

Python Installation

Go to https://www.python.org/downloads/ and download the latest version. At the time of writing it was 3.8.1. be sure to read below before rushing through the installation.

Once python has downloaded run the executable. Make sure you select Customize installation and select next. By default all Optional features are selected, nothing here requires changes. The important Advanced Options to be enabled are “Add Python to environment variables”.

By selecting “Add Python to environmental

Now select install.

Once installed you can confirm the installation with the selected environmental variables by opening a command prompt and typing python. You will see the below if successful.

To exit this screen, type exit()

Installing Paramiko

To begin we need to update the python package manager. This is called pip. To do this open a command prompt and type the below:


python -m pip install –upgrade pip

Once complete we will install paramiko, to do this type the following command:

python -m pip install paramiko

Using Paramiko

Below is a short Python script demonstrating a simple way to use Paramiko. In this example the Windows machine we previously installed Python and Paramiko on will SSH to a Ubuntu desktop, run the command “ifconfig” then print the output.

#This imports the python modules we want to use
import paramiko
import time

#This sets up our SSH session to the client
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname="192.168.93.128",username="thetutorialhub",password="password123")

#This creates a shell connection to the SSH session
ubuntu_desktop = ssh_client.invoke_shell()

#This clears the initial input buffer
#This is only useful if you want to display the output of your commands
ubuntu_desktop.recv(5000)

#This is the command you are sending over the SSH session
ubuntu_desktop.send("ifconfig\n")

#This is to allow enough time for the receive buffer to capture the output of the command
time.sleep(2)

#This is the command that assigns the output into a variable
ubuntu_output = ubuntu_desktop.recv(5000)

#This is the ocommand to print that output
print(ubuntu_output.decode('utf8'))

#This will close the SSH session
ubuntu_desktop.close()



This is the output of the script.

Some important notes:
If you are not concerned with capturing the output, you can remove the sleep, recv and print commands.

The number in brackets in commands such as ubuntu_desktop.recv(5000)
are to specify how many bytes you would like to read from the output. If you do not receive the full amount of output expected, try increasing the sleep time and recv amount.

You need to decode the output if you want it to be human readable, without the command .decode(utf8) the output would look like the below:

The decode statement reads interprets the statements such as \n to be a newline.