#This script is used to scan the IP addresses in the network for the netbox to discover all subnets with the environment
#Prerequisites - This needs to be done on netbox server
sudo -i
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.8
python3.8 --version
sudo apt install python3.8-pip [IF YOU GET ERROR WITH THIS COMMAND , TRY COMMAND BELOW]
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py
python3.8 -m pip --version
pip3.8 install ipcalc
pip3.8 install networkscan
pip3.8 install python-netbox
[If any issues -with above commands try below, if it works , skip this part and go to mkdir]
python3 -m pip install ipcalc
python3 -m pip install networkscan
python3 -m pip install python-netbox
mkdir /scripts && cd /scripts
nano netbox_scan_ip_subnet.py
[PASTE BELOW]
import ipcalc
import networkscan
from netbox import NetBox
import requests
import urllib3
import socket
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # disable InsecureRequestWarning
API_TOKEN = "7155d266ebc84bd45494504620e542e8df8a028d"
HEADERS = {'Authorization': f'Token {API_TOKEN}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
netbox = NetBox(host='172.16.10.200', port=80, use_ssl=False, auth_token='7155d266ebc84bd45494504620e542e8df8a028d')
NB_URL = "http://172.16.10.200"
if _name_ == '__main__':
Define the network to scan
my_network_list = ["172.16.10.0/24", "172.16.4.0/24", "10.145.193.0/24"] # 1 or more subnets can be added to the list 10.250.12.0/24 and 10.250.13.0/24
Create the object
for network in my_network_list:
my_scan = networkscan.Networkscan(network)
Run the scan of hosts using pings
my_scan.run()
Here we define exists ip address in our network and write it to list
found_ip_in_network = []
for address1 in my_scan.list_of_hosts_found:
found_ip_in_network.append(str(address1))
Get all ip from prefix
for ipaddress in ipcalc.Network(network):
Doing get request to netbox
request_url = f"{NB_URL}/api/ipam/ip-addresses/?q={ipaddress}/"
ipaddress1 = requests.get(request_url, headers = HEADERS, verify=False)
netboxip = ipaddress1.json()
print(ipaddress)
print(netboxip)
print(netboxip['count'])
If not in netbox
if netboxip['count'] == 0:
Check if in network exists and not exist in netbox
if ipaddress in found_ip_in_network:
Adding in IP netbox
netbox.ipam.create_ip_address(str(ipaddress))
else:
pass
else:
#If exists in netbox and network
if ipaddress in found_ip_in_network:
netbox.ipam.update_ip(str(ipaddress),status="active")
do nslookup
if ipaddress in found_ip_in_network:
try:
name = socket.gethostbyaddr(str(ipaddress))
name = name[0]
netbox.ipam.update_ip(str(ipaddress),dns_name=name)
except socket.error:
pass
else:
If not exists in network but exists in netbox then delete or change the status in netbox
netbox.ipam.delete_ip_address(str(ipaddress))
netbox.ipam.update_ip(str(ipaddress),status="deprecated")
#ends here
To run the scanner and start scanning the network : Make sure you are in the scripts folder.
python3.8 netbox_scan_ip_subnet.py