# 🔧 Gom Nhóm IP Thành Subnet CIDR Tối Ưu

🎯 Mục tiêu

Tự động gom các IP rời rạc thành **các subnet nhỏ nhất có thể** sử dụng CIDR notation – giúp:

* Giảm dòng cấu hình cần gõ
* Tối ưu rule firewall
* Dễ kiểm soát, bảo trì về sau

***

### 💡 Ví dụ đầu vào:

Danh sách IP ban đầu:

```
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.10
192.168.1.11
192.168.2.100
192.168.2.101
192.168.2.102
192.168.2.103
192.168.2.200
```

***

### 🧠 Kết quả sau khi tối ưu subnet:

| `192.168.1.1/32` | 192.168.1.1 |
| ---------------- | ----------- |

| `192.168.1.2/31` | 192.168.1.2 – 192.168.1.3 |
| ---------------- | ------------------------- |

| `192.168.1.4/32` | 192.168.1.4 |
| ---------------- | ----------- |

| `192.168.1.10/31` | 192.168.1.10 – 192.168.1.11 |
| ----------------- | --------------------------- |

| `192.168.2.100/30` | 192.168.2.100 – 192.168.2.10 |
| ------------------ | ---------------------------- |

| `192.168.2.200/32` | 192.168.2.200 |
| ------------------ | ------------- |

### 🛠 Code Python tự động gom subnet

```python
from netaddr import IPAddress, IPSet, cidr_merge

ip_list = [
    '192.168.1.1',
    '192.168.1.2',
    '192.168.1.3',
    '192.168.1.4',
    '192.168.1.10',
    '192.168.1.11',
    '192.168.2.100',
    '192.168.2.101',
    '192.168.2.102',
    '192.168.2.103',
    '192.168.2.200'
]

ip_set = IPSet(IPAddress(ip) for ip in ip_list)
merged_subnets = cidr_merge(ip_set.iter_cidrs())

print("Các subnet tối ưu:")
for subnet in merged_subnets:
    print(subnet)

```

***

### 📦 Cài đặt thư viện cần thiết:

```bash
pip install netaddr
```

***

### 🧩 Ứng dụng thực tế:

* Tạo whitelist IP cho NGINX, Apache, iptables
* Nhập nhanh security group vào AWS/GCP/Azure
* Giảm tải khi audit hệ thống

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.micsoftvn.com/use-cases/for-engineering/gom-nhom-ip-thanh-subnet-cidr-toi-uu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
