Skip to content

Commit e7a1f71

Browse files
committed
v2
1 parent bb15bc9 commit e7a1f71

9 files changed

+111
-39
lines changed

.netbox-scanner.conf

-21
This file was deleted.

Dockerfile

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ FROM debian:10-slim as build
22

33
ENV USER="casperklein"
44
ENV NAME="netbox-scanner"
5-
ENV VERSION="0.1.1"
5+
ENV VERSION="2.0.0"
66

77
ENV PACKAGES="python3 python3-pip nmap"
88

99
ENV GIT_USER="lopes"
1010
ENV GIT_REPO="netbox-scanner"
11-
ENV GIT_COMMIT="438016caea3e975ce2cae34c443d661ee7b66b20"
11+
ENV GIT_COMMIT="af65c252776127d2ab3505862fca7670e299c45c"
1212
ENV GIT_ARCHIVE="https://github.com/$GIT_USER/$GIT_REPO/archive/$GIT_COMMIT.tar.gz"
1313

1414
# Install packages
1515
RUN apt-get update \
1616
&& apt-get -y install $PACKAGES \
1717
&& rm -rf /var/lib/apt/lists/*
1818

19-
# Copy root filesystem
20-
COPY rootfs /
21-
2219
# Download source
2320
WORKDIR /$GIT_REPO
2421
ADD $GIT_ARCHIVE /
@@ -30,8 +27,12 @@ RUN pip3 install -r requirements.txt
3027
# Cleanup
3128
RUN find /usr/ -name '*.pyc' -delete
3229

30+
# Copy root filesystem
31+
COPY rootfs /
32+
3333
# Build final image
3434
FROM scratch
35-
COPY --from=build / /
3635

3736
CMD ["/run.sh"]
37+
38+
COPY --from=build / /

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ NAME := $(shell grep -P 'ENV\s+NAME=".+?"' Dockerfile | cut -d'"' -f2)
66
VERSION := $(shell grep -P 'ENV\s+VERSION=".+?"' Dockerfile | cut -d'"' -f2)
77

88
build:
9-
./build.sh
9+
@./build.sh
1010

1111
clean:
1212
docker rmi $(USER)/$(NAME):$(VERSION)
1313

1414
scan:
15-
./scan.sh
15+
@./scan.sh
1616

1717
push:
1818
docker push $(USER)/$(NAME):$(VERSION)

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# docker-netbox-scanner
22

3+
Docker version of [netbox-scanner](https://github.com/lopes/netbox-scanner). Scan networks and add them to Netbox.
4+
35
## Build (optional)
46

57
make
6-
7-
## Setup
8-
9-
Configure *address*, *token* and *networks* in ``.netbox-scanner.conf``.
8+
9+
## Setup Netbox
10+
11+
1. Goto `Organization / Tags` and create a new tag: `nmap`.
12+
1. Goto `Profile / API Tokens` and create a token, for use with netbox-scanner.
13+
14+
## Setup Netbox-Scanner
15+
16+
1. Configure *address* and API *token* in `netbox-scanner.conf`.
17+
1. Configure the networks to scan in `networks.txt`.
1018

1119
## Start scan
1220

netbox-scanner.conf

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[NETBOX]
2+
address = <server>
3+
token = <token>
4+
logs = logs/
5+
# use lowercase no if you want to skip ssl verification.
6+
# any other value will verify the server ssl certificate.
7+
tls_verify = no
8+
9+
[NMAP]
10+
path = ./
11+
unknown = autodiscovered:netbox-scanner
12+
tag = nmap
13+
cleanup = no
14+
15+
[NETXMS]
16+
address = https://netxms.domain
17+
username =
18+
password =
19+
unknown = autodiscovered:netbox-scanner
20+
tag = netxms
21+
cleanup = yes
22+
23+
[PRIME]
24+
address = https://prime.domain/webacs/api/v4
25+
username =
26+
password =
27+
unknown = autodiscovered:netbox-scanner
28+
tag = prime
29+
cleanup = yes

networks.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
192.168.0.0/24
2+
192.168.1.0/24

rootfs/netbox-scanner/nmap-scan.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This is just an example.
4+
#
5+
# Since scanning many networks can produce huge XML files,
6+
# the idea is to create one XML file per network, then
7+
# use all of them as input to nbs.nmap.Nmap().
8+
#
9+
# If you scan few networks with few hosts or if you just
10+
# want to experiment, feel free to use the `-iL` option of
11+
# Nmap, passing a list of all networks and hosts to be
12+
# scanned.
13+
#
14+
# If you have a large number of networks, use the mapfile option.
15+
# In order to use mapfile, populate your networks, one per line,
16+
# in a file called networks.txt.
17+
#
18+
# If you have a small number of networks, comment out the mapfile
19+
# lines, and uncomment the "small array" line.
20+
#
21+
# For the purpose of this example, assume that netbox-scanner
22+
# is configured to use the same directory of this script
23+
# to look for XML files.
24+
##
25+
26+
# mapfile
27+
declare -a NETWORKS
28+
mapfile -t NETWORKS < networks.txt
29+
30+
# small array
31+
#NETWORKS="192.168.3.0/24 192.168.252.0/24"
32+
33+
for net in "${NETWORKS[@]}"; do
34+
echo "Scan network $net"
35+
NETNAME=$(echo $net | tr -s '/' '-')
36+
# requires sudo
37+
nmap "$net" -T4 -O -F --host-timeout 30s -oX nmap-"$NETNAME".xml
38+
# does not require sudo
39+
#nmap "$net" -T4 -sn --host-timeout 30s -oX nmap-"$NETNAME".xml
40+
done
41+
echo
42+
43+
echo "Send networks to Netbox.."
44+
python3 netbox-scanner.py nmap

rootfs/run.sh

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#!/bin/bash
22

3-
echo 'a9ebfa600ece03fb0005080c3b1184dfe52cea87 /root/.netbox-scanner.conf' | sha1sum -c &> /dev/null && {
4-
echo -e "Error: You have to configure at least ADDRESS, TOKEN and NETWORKS in '.netbox-scanner.conf'.\n" >&2
3+
set -ueo pipefail
4+
5+
if [ ! -s /netbox-scanner/networks.txt ]; then
6+
echo "Error: 'networks.txt' is empty."
7+
echo
8+
exit 1
9+
fi >&2
10+
11+
echo '4de64ad74607f128bdb5873497a9d85d27e52c0a96dc994016488d050a55dd6c /root/.netbox-scanner.conf' | sha256sum -c &> /dev/null && {
12+
echo "Error: You have to configure at least ADDRESS and TOKEN in 'netbox-scanner.conf'."
13+
echo
514
exit 1
6-
}
15+
} >&2
716

8-
echo 'Netbox-scanner running..'
17+
cd /netbox-scanner
918

10-
exec python3 /netbox-scanner/netbox-scanner/nbscanner
19+
./nmap-scan.sh

scan.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ DIR=${0%/*}
1111
cd "$DIR"
1212

1313
echo "Starting Netbox-Scanner.."
14-
docker run --rm -it -v "$PWD"/.netbox-scanner.conf:/root/.netbox-scanner.conf:ro "$TAG"
14+
docker run --rm -it -v "$PWD"/netbox-scanner.conf:/root/.netbox-scanner.conf:ro -v "$PWD"/networks.txt:/netbox-scanner/networks.txt:ro "$TAG"

0 commit comments

Comments
 (0)