add ip merge script

This commit is contained in:
Lord_Alfred
2021-07-29 17:40:19 +03:00
parent 3fb05f839c
commit 7be1d52f62
3 changed files with 47 additions and 0 deletions

View File

@@ -14,6 +14,29 @@ jobs:
- name: Download Google IPs
run: bash google/downloader.sh
- name: Set up Python 3.7
uses: actions/setup-python@v2
with:
python-version: '3.7'
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('utils/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install dependencies
run: |
pip install -r utils/requirements.txt
- name: Merge IPv4 ranges
run: |
set -euo pipefail
python utils/merge.py --source=google/ipv4.txt | sort -h > google/ipv4_merged.txt
- name: Commit files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

23
utils/merge.py Normal file
View File

@@ -0,0 +1,23 @@
import argparse
import netaddr
def read(fh):
for line in fh:
yield line.rstrip()
fh.close()
def merge(fh):
for addr in netaddr.cidr_merge(read(fh)):
print(addr)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Merge IP addresses into the smallest possible list of CIDRs.')
parser.add_argument('--source', nargs='?', type=argparse.FileType('r'), required=True, help='Source file path')
args = parser.parse_args()
merge(args.source)

1
utils/requirements.txt Normal file
View File

@@ -0,0 +1 @@
netaddr==0.8.0