mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-21 04:44:37 +02:00
__init__.py: Improve tag checking (With the previous version of check wasn't working, don't know if it was dependant on old Netbox version)
nmap.py: Add extra function to improve DNS resolution since nmap is not always consistent
This commit is contained in:
parent
14335e4960
commit
afecb475a5
@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
|
|
||||||
|
import requests
|
||||||
from pynetbox import api
|
from pynetbox import api
|
||||||
|
|
||||||
|
|
||||||
@ -47,7 +47,12 @@ class NetBoxScanner(object):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
if nbhost:
|
if nbhost:
|
||||||
if (self.tag in nbhost.tags):
|
tag_update = False
|
||||||
|
for tag in nbhost.tags:
|
||||||
|
if (self.tag == tag.name):
|
||||||
|
tag_update = True
|
||||||
|
break
|
||||||
|
if tag_update:
|
||||||
if (host[1] != nbhost.description):
|
if (host[1] != nbhost.description):
|
||||||
aux = nbhost.description
|
aux = nbhost.description
|
||||||
nbhost.description = host[1]
|
nbhost.description = host[1]
|
||||||
@ -59,7 +64,7 @@ class NetBoxScanner(object):
|
|||||||
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
|
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
|
||||||
self.stats['unchanged'] += 1
|
self.stats['unchanged'] += 1
|
||||||
else:
|
else:
|
||||||
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
|
logging.info(f'no-tag(unchanged): {host[0]}/32 "{host[1]}"')
|
||||||
self.stats['unchanged'] += 1
|
self.stats['unchanged'] += 1
|
||||||
else:
|
else:
|
||||||
self.netbox.ipam.ip_addresses.create(
|
self.netbox.ipam.ip_addresses.create(
|
||||||
|
25
nbs/nmap.py
25
nbs/nmap.py
@ -1,5 +1,8 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
import nmap3
|
import nmap3
|
||||||
|
|
||||||
|
|
||||||
class Nmap(object):
|
class Nmap(object):
|
||||||
|
|
||||||
def __init__(self, unknown, networks):
|
def __init__(self, unknown, networks):
|
||||||
@ -13,13 +16,26 @@ class Nmap(object):
|
|||||||
for item in self.networks:
|
for item in self.networks:
|
||||||
temp_scan_result = nmap.nmap_no_portscan(item.replace('\n', ''))
|
temp_scan_result = nmap.nmap_no_portscan(item.replace('\n', ''))
|
||||||
self.scan_results = {**self.scan_results, **temp_scan_result}
|
self.scan_results = {**self.scan_results, **temp_scan_result}
|
||||||
|
self.scan_results.pop("stats")
|
||||||
|
self.scan_results.pop("runtime")
|
||||||
return self.scan_results
|
return self.scan_results
|
||||||
|
|
||||||
|
def dns_resolution(self):
|
||||||
|
# Try to improve DNS resolution since NMAP is not consistent
|
||||||
|
for ip, v in self.scan_results.items():
|
||||||
|
try:
|
||||||
|
name, arpa, ip = socket.gethostbyaddr(ip)
|
||||||
|
try:
|
||||||
|
v["hostname"][0]["name"]
|
||||||
|
except (TypeError, IndexError):
|
||||||
|
v.update({"hostname": {"name": name, "type": 'PTR'}})
|
||||||
|
except socket.herror:
|
||||||
|
pass
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
scan_result = self.scan()
|
self.scan()
|
||||||
scan_result.pop("stats")
|
self.dns_resolution()
|
||||||
scan_result.pop("runtime")
|
for k,v in self.scan().items():
|
||||||
for k,v in scan_result.items():
|
|
||||||
try:
|
try:
|
||||||
self.hosts.append((
|
self.hosts.append((
|
||||||
k,
|
k,
|
||||||
@ -30,4 +46,3 @@ class Nmap(object):
|
|||||||
k,
|
k,
|
||||||
self.unknown
|
self.unknown
|
||||||
))
|
))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user