add options source networks from netbox
This commit is contained in:
@@ -49,6 +49,8 @@ docker compose up netbox-importer
|
|||||||
- `NETWORKS`: comma-separated networks to scan
|
- `NETWORKS`: comma-separated networks to scan
|
||||||
- `TENANT`: NetBox tenant name
|
- `TENANT`: NetBox tenant name
|
||||||
- `SSL_VERIFY`: whether to verify SSL (`false`, `0`, `no` disable verification)
|
- `SSL_VERIFY`: whether to verify SSL (`false`, `0`, `no` disable verification)
|
||||||
|
- `SCAN_SOURCE`: `env`, `netbox`, or `mixed` (use NetBox prefix data for scan networks)
|
||||||
|
- `NETBOX_PREFIX_STATUS`: optional prefix status filter for NetBox prefixes (for example `active`)
|
||||||
|
|
||||||
## Output format
|
## Output format
|
||||||
|
|
||||||
|
|||||||
@@ -21,3 +21,4 @@ services:
|
|||||||
- NETWORKS=192.168.85.0/24,192.168.86.0/24
|
- NETWORKS=192.168.85.0/24,192.168.86.0/24
|
||||||
- TENANT=Xxxxx Praha
|
- TENANT=Xxxxx Praha
|
||||||
- SSL_VERIFY=false
|
- SSL_VERIFY=false
|
||||||
|
- SCAN_SOURCE=env
|
||||||
|
|||||||
25
ipscan-v2.py
25
ipscan-v2.py
@@ -18,6 +18,10 @@ nm = nmap.PortScanner()
|
|||||||
networks_env = os.getenv("NETWORKS", "192.168.85.0/24,192.168.86.0/24")
|
networks_env = os.getenv("NETWORKS", "192.168.85.0/24,192.168.86.0/24")
|
||||||
networks = [network.strip() for network in networks_env.split(",") if network.strip()]
|
networks = [network.strip() for network in networks_env.split(",") if network.strip()]
|
||||||
|
|
||||||
|
# Scan source configuration: env, netbox, or mixed
|
||||||
|
scan_source = os.getenv("SCAN_SOURCE", "env").strip().lower()
|
||||||
|
netbox_prefix_status = os.getenv("NETBOX_PREFIX_STATUS", "").strip().lower()
|
||||||
|
|
||||||
# NetBox configuration
|
# NetBox configuration
|
||||||
netbox_url = os.getenv("NETBOX_URL", "https://netbox.xxxxx.xx/")
|
netbox_url = os.getenv("NETBOX_URL", "https://netbox.xxxxx.xx/")
|
||||||
netbox_token = os.getenv("NETBOX_TOKEN", "xxxxx")
|
netbox_token = os.getenv("NETBOX_TOKEN", "xxxxx")
|
||||||
@@ -26,6 +30,27 @@ netbox = pynetbox.api(url=netbox_url, token=netbox_token, ssl_verify=ssl_verify)
|
|||||||
|
|
||||||
tenant = os.getenv("TENANT", "Xxxxx Praha")
|
tenant = os.getenv("TENANT", "Xxxxx Praha")
|
||||||
|
|
||||||
|
|
||||||
|
def load_networks_from_netbox():
|
||||||
|
print("Loading networks from NetBox...")
|
||||||
|
prefixes = netbox.ipam.prefixes.filter(status=netbox_prefix_status) if netbox_prefix_status else netbox.ipam.prefixes.all()
|
||||||
|
networks_from_netbox = []
|
||||||
|
for prefix in prefixes:
|
||||||
|
address = getattr(prefix, 'prefix', None)
|
||||||
|
if address:
|
||||||
|
networks_from_netbox.append(address)
|
||||||
|
print(f"Found NetBox prefix: {address}")
|
||||||
|
return networks_from_netbox
|
||||||
|
|
||||||
|
|
||||||
|
if scan_source == 'netbox':
|
||||||
|
networks = load_networks_from_netbox()
|
||||||
|
elif scan_source == 'mixed':
|
||||||
|
networks = networks + load_networks_from_netbox()
|
||||||
|
|
||||||
|
if not networks:
|
||||||
|
raise ValueError('No networks configured to scan. Set NETWORKS or SCAN_SOURCE to include NetBox prefixes.')
|
||||||
|
|
||||||
def scan_network(network):
|
def scan_network(network):
|
||||||
print(f"Scanning network: {network}")
|
print(f"Scanning network: {network}")
|
||||||
nm.scan(hosts=network, arguments='-p 1-32768 -T4 --host-timeout 2m') # Adding a host-timeout of 2 minutes
|
nm.scan(hosts=network, arguments='-p 1-32768 -T4 --host-timeout 2m') # Adding a host-timeout of 2 minutes
|
||||||
|
|||||||
Reference in New Issue
Block a user