wip
This commit is contained in:
parent
6dd38036e7
commit
097b3dbf09
34 changed files with 1719 additions and 520 deletions
|
@ -5,6 +5,7 @@ from app.core.extensions import db
|
|||
from app.scripts.ip_scanner import scan
|
||||
import ipaddress
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
bp = Blueprint('ipam', __name__, url_prefix='/ipam')
|
||||
|
||||
|
@ -16,7 +17,10 @@ def ipam_home():
|
|||
|
||||
# Calculate usage for each subnet
|
||||
for subnet in subnets:
|
||||
subnet.usage_percent = subnet.used_ips / 254 * 100 if subnet.cidr.endswith('/24') else 0
|
||||
network = ipaddress.ip_network(subnet.cidr, strict=False)
|
||||
max_hosts = network.num_addresses - 2 if network.prefixlen < 31 else network.num_addresses
|
||||
used_count = Server.query.filter_by(subnet_id=subnet.id).count()
|
||||
subnet.usage_percent = (used_count / max_hosts) * 100 if max_hosts > 0 else 0
|
||||
|
||||
return render_template(
|
||||
'ipam/index.html',
|
||||
|
@ -32,42 +36,43 @@ def subnet_new():
|
|||
if request.method == 'POST':
|
||||
cidr = request.form.get('cidr')
|
||||
location = request.form.get('location')
|
||||
auto_scan = 'auto_scan' in request.form
|
||||
auto_scan = request.form.get('auto_scan') == 'on'
|
||||
|
||||
# Basic validation
|
||||
if not cidr or not location:
|
||||
flash('Please fill in all required fields', 'danger')
|
||||
return render_template(
|
||||
'ipam/subnet_form.html',
|
||||
title='New Subnet',
|
||||
now=datetime.now()
|
||||
title='New Subnet'
|
||||
)
|
||||
|
||||
# Check if valid CIDR
|
||||
# Validate CIDR format
|
||||
try:
|
||||
ipaddress.ip_network(cidr)
|
||||
ipaddress.ip_network(cidr, strict=False)
|
||||
except ValueError:
|
||||
flash('Invalid CIDR notation', 'danger')
|
||||
flash('Invalid CIDR format', 'danger')
|
||||
return render_template(
|
||||
'ipam/subnet_form.html',
|
||||
title='New Subnet',
|
||||
now=datetime.now()
|
||||
title='New Subnet'
|
||||
)
|
||||
|
||||
# Check if subnet already exists
|
||||
# Check if CIDR already exists
|
||||
if Subnet.query.filter_by(cidr=cidr).first():
|
||||
flash('Subnet already exists', 'danger')
|
||||
return render_template(
|
||||
'ipam/subnet_form.html',
|
||||
title='New Subnet',
|
||||
now=datetime.now()
|
||||
title='New Subnet'
|
||||
)
|
||||
|
||||
# Create new subnet with JSON string for active_hosts, not a Python list
|
||||
subnet = Subnet(
|
||||
cidr=cidr,
|
||||
location=location,
|
||||
active_hosts=json.dumps([]), # Convert empty list to JSON string
|
||||
last_scanned=None,
|
||||
auto_scan=auto_scan
|
||||
)
|
||||
|
||||
db.session.add(subnet)
|
||||
db.session.commit()
|
||||
|
||||
|
@ -76,70 +81,115 @@ def subnet_new():
|
|||
|
||||
return render_template(
|
||||
'ipam/subnet_form.html',
|
||||
title='New Subnet',
|
||||
now=datetime.now()
|
||||
title='New Subnet'
|
||||
)
|
||||
|
||||
@bp.route('/subnet/<int:subnet_id>')
|
||||
@login_required
|
||||
def subnet_view(subnet_id):
|
||||
"""View subnet details"""
|
||||
"""View a specific subnet"""
|
||||
subnet = Subnet.query.get_or_404(subnet_id)
|
||||
|
||||
# Get all servers in this subnet
|
||||
servers = Server.query.filter_by(subnet_id=subnet_id).all()
|
||||
|
||||
# Get network info
|
||||
network = ipaddress.ip_network(subnet.cidr)
|
||||
total_ips = network.num_addresses - 2 # Excluding network and broadcast addresses
|
||||
used_ips = len(servers)
|
||||
usage_percent = (used_ips / total_ips) * 100 if total_ips > 0 else 0
|
||||
# Parse CIDR for display
|
||||
network = ipaddress.ip_network(subnet.cidr, strict=False)
|
||||
subnet_info = {
|
||||
'network_address': str(network.network_address),
|
||||
'broadcast_address': str(network.broadcast_address),
|
||||
'netmask': str(network.netmask),
|
||||
'num_addresses': network.num_addresses,
|
||||
'host_range': f"{str(network.network_address + 1)} - {str(network.broadcast_address - 1)}" if network.prefixlen < 31 else subnet.cidr
|
||||
}
|
||||
|
||||
return render_template(
|
||||
'ipam/subnet_view.html',
|
||||
title=f'Subnet - {subnet.cidr}',
|
||||
title=subnet.cidr,
|
||||
subnet=subnet,
|
||||
subnet_info=subnet_info,
|
||||
servers=servers,
|
||||
total_ips=total_ips,
|
||||
used_ips=used_ips,
|
||||
usage_percent=usage_percent,
|
||||
now=datetime.now()
|
||||
)
|
||||
|
||||
@bp.route('/subnet/<int:subnet_id>/scan')
|
||||
@bp.route('/subnet/<int:subnet_id>/edit', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def subnet_edit(subnet_id):
|
||||
"""Edit a subnet"""
|
||||
subnet = Subnet.query.get_or_404(subnet_id)
|
||||
|
||||
if request.method == 'POST':
|
||||
cidr = request.form.get('cidr')
|
||||
location = request.form.get('location')
|
||||
auto_scan = request.form.get('auto_scan') == 'on'
|
||||
|
||||
# Validate inputs
|
||||
if not all([cidr, location]):
|
||||
flash('All fields are required', 'danger')
|
||||
return render_template('ipam/subnet_form.html',
|
||||
title='Edit Subnet',
|
||||
subnet=subnet,
|
||||
edit_mode=True)
|
||||
|
||||
# Validate CIDR format
|
||||
try:
|
||||
ipaddress.ip_network(cidr, strict=False)
|
||||
except ValueError:
|
||||
flash('Invalid CIDR format', 'danger')
|
||||
return render_template('ipam/subnet_form.html',
|
||||
title='Edit Subnet',
|
||||
subnet=subnet,
|
||||
edit_mode=True)
|
||||
|
||||
# Update subnet
|
||||
subnet.cidr = cidr
|
||||
subnet.location = location
|
||||
subnet.auto_scan = auto_scan
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
flash(f'Subnet {cidr} has been updated', 'success')
|
||||
return redirect(url_for('ipam.subnet_view', subnet_id=subnet.id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash(f'Error updating subnet: {str(e)}', 'danger')
|
||||
|
||||
return render_template('ipam/subnet_form.html',
|
||||
title='Edit Subnet',
|
||||
subnet=subnet,
|
||||
edit_mode=True)
|
||||
|
||||
@bp.route('/subnet/<int:subnet_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
def subnet_delete(subnet_id):
|
||||
"""Delete a subnet"""
|
||||
subnet = Subnet.query.get_or_404(subnet_id)
|
||||
|
||||
# Check if subnet has servers
|
||||
servers_count = Server.query.filter_by(subnet_id=subnet_id).count()
|
||||
if servers_count > 0:
|
||||
flash(f'Cannot delete subnet {subnet.cidr}. It has {servers_count} servers assigned.', 'danger')
|
||||
return redirect(url_for('ipam.subnet_view', subnet_id=subnet_id))
|
||||
|
||||
db.session.delete(subnet)
|
||||
db.session.commit()
|
||||
|
||||
flash(f'Subnet {subnet.cidr} has been deleted', 'success')
|
||||
return redirect(url_for('ipam.ipam_home'))
|
||||
|
||||
@bp.route('/subnet/<int:subnet_id>/scan', methods=['POST'])
|
||||
@login_required
|
||||
def subnet_scan(subnet_id):
|
||||
"""Scan a subnet for active hosts"""
|
||||
"""Manually scan a subnet"""
|
||||
subnet = Subnet.query.get_or_404(subnet_id)
|
||||
|
||||
try:
|
||||
results = scan(subnet.cidr, save_results=True)
|
||||
flash(f'Scan completed for subnet {subnet.cidr}. Found {len(results)} active hosts.', 'success')
|
||||
# Call the scan function with manual_trigger=True
|
||||
scan(subnet, manual_trigger=True)
|
||||
db.session.commit()
|
||||
flash(f'Scan completed for subnet {subnet.cidr}', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Error scanning subnet: {e}', 'danger')
|
||||
db.session.rollback()
|
||||
flash(f'Error scanning subnet: {str(e)}', 'danger')
|
||||
|
||||
return redirect(url_for('ipam.subnet_view', subnet_id=subnet_id))
|
||||
|
||||
@bp.route('/subnet/<int:subnet_id>/visualize')
|
||||
@login_required
|
||||
def subnet_visualize(subnet_id):
|
||||
"""Visualize IP usage in a subnet"""
|
||||
subnet = Subnet.query.get_or_404(subnet_id)
|
||||
servers = Server.query.filter_by(subnet_id=subnet_id).all()
|
||||
|
||||
# Create a dictionary of used IPs
|
||||
used_ips = {server.ip_address: server.hostname for server in servers}
|
||||
|
||||
# Get network info
|
||||
network = ipaddress.ip_network(subnet.cidr)
|
||||
total_ips = network.num_addresses - 2 # Excluding network and broadcast addresses
|
||||
used_ip_count = len(servers)
|
||||
|
||||
return render_template(
|
||||
'ipam/subnet_visualization.html',
|
||||
title=f'Subnet Visualization - {subnet.cidr}',
|
||||
subnet=subnet,
|
||||
network=network,
|
||||
used_ips=used_ips,
|
||||
total_ips=total_ips,
|
||||
used_ip_count=used_ip_count,
|
||||
now=datetime.now()
|
||||
)
|
||||
return redirect(url_for('ipam.subnet_view', subnet_id=subnet_id))
|
Loading…
Add table
Add a link
Reference in a new issue