Master #1

Open
cnbugs wants to merge 20 commits from master into 0.0.1
2 changed files with 41 additions and 2 deletions
Showing only changes of commit 13c457acb3 - Show all commits
+2 -2
View File
@@ -14,9 +14,9 @@ class NetworkService:
@staticmethod @staticmethod
def calculate_total_ips(cidr: str) -> int: def calculate_total_ips(cidr: str) -> int:
"""计算网段的总IP数量""" """计算网段中实际分配的IP数量(排除网络地址和广播地址)"""
network = ipaddress.ip_network(cidr, strict=False) network = ipaddress.ip_network(cidr, strict=False)
return network.num_addresses return network.num_addresses - 2 if network.num_addresses > 2 else network.num_addresses
@staticmethod @staticmethod
def get_network_addresses(cidr: str) -> List[str]: def get_network_addresses(cidr: str) -> List[str]:
+39
View File
@@ -0,0 +1,39 @@
"""
修复已有网段的 total_ips 字段:将 num_addresses 改为 hosts 数量(排除网络地址和广播地址)
用法: cd /root/ipam/backend && source venv/bin/activate && python scripts/fix_network_total_ips.py
"""
import sys
import os
# 确保能找到 app 模块
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.core.database import SessionLocal
from app.models.network import Network
from app.services.network_service import NetworkService
def fix_total_ips():
db = SessionLocal()
try:
networks = db.query(Network).all()
fixed = 0
for net in networks:
correct = NetworkService.calculate_total_ips(net.cidr)
actual_hosts = len(list(NetworkService.get_network_addresses(net.cidr)))
if net.total_ips != correct:
print(f" [{net.cidr:20s}] {net.total_ips:>5} -> {correct:>5} (hosts={actual_hosts})")
net.total_ips = correct
fixed += 1
db.commit()
print(f"\n✅ 已修复 {fixed} 个网段")
except Exception as e:
db.rollback()
print(f"❌ 错误: {e}")
finally:
db.close()
if __name__ == "__main__":
fix_total_ips()