计算结果如下:

  • IPv6 数据巨细:0.01kb
  • 总数据巨细:0.02kb
  • IPv6 数据占比:47.37%
  • ⚠️ 未支撑IPv6的域名信息:
  • 域名:ipv4.lookup.test-ipv6.com,数据巨细:0.00kb,占比:5.26%
  • 域名:test4.vyncke.org,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.master.test-ipv6.com,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.test-ipv6.go6.si,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.test-ipv6.sin.vr.org,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.test-ipv6.hu,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.test-ipv6.is,数据巨细:0.00kb,占比:5.26%
  • 域名:v4-only.steffann.nl,数据巨细:0.00kb,占比:5.26%
  • 域名:ipv4.ipv6-test.pl,数据巨细:0.00kb,占比:5.26%

最近在做IPV6晋级支撑改造,发现一篇文章中有计算IPV6浓度,计算代码有点小问题,以下是我修正往后的:

计算代码 analysis_ipv6.py:

import argparse
import json
import os
import glob
import ipaddress
# 获取url的domain
from urllib.parse import urlparse
# 导出表格数据
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Alignment
# IPv6占比基线
ipv6_baseline = 1.0
def get_domain(url):
    return urlparse(url).netloc
def check_ipv4_address(ip):
    try:
        ip_obj = ipaddress.ip_address(ip)
        print("ip_obj",ip_obj.version)
        return ip_obj.version == 4
    except ValueError:
        print("无效的IP地址")
        return False
def check_ipv6_address(ip):
    try:
        ip_obj = ipaddress.ip_address(ip)
        print("ip_obj",ip_obj.version)
        return ip_obj.version == 6
    except ValueError:
        print("无效的IP地址")
        return False
def calculate_sizes(entries):
    domains = {}
    totalsizes = 0
    ipv6sizes = 0
    for entry in entries:
        url = entry['request']['url']
        response = entry['response']
        size = response.get('_transferSize', response.get(
            'bodySize', 0) + response.get('headersSize', 0))
        if 'serverIPAddress' in entry:
            serverIPAddress = entry['serverIPAddress']
            totalsizes += 1
            if check_ipv6_address(serverIPAddress):
                ipv6sizes += 1
            else:
                domain = get_domain(url)
                domains[domain] = domains.get(domain, 0) + 1
    return domains, totalsizes, ipv6sizes
def print_results(domains, sizes, ipv6sizes):
    ipv6point = ipv6sizes / sizes * 100
    print("IPv6 数据巨细:{:.2f}kb".format(ipv6sizes / 1024))
    print("总数据巨细:{:.2f}kb".format(sizes / 1024))
    print("IPv6 数据占比:{:.2f}%".format(ipv6point))
    print("⚠️  未支撑IPv6的域名信息:")
    ipv4info = ''
    for key, value in domains.items():
        info = "域名:{},数据巨细:{:.2f}kb,占比:{:.2f}%".format(
            key, value / 1024, value / sizes * 100)
        ipv4info += info + 'n'
        print(info)
    print("====================")
    print("")
    return ipv6point, ipv4info
def analysis_request(entries):
    domains, sizes, ipv6sizes = calculate_sizes(entries)
    ipv6point, ipv4info = print_results(domains, sizes, ipv6sizes)
    results = {}
    if ipv6sizes / sizes < ipv6_baseline:
        results['ipv6point'] = '{:.2f}%'.format(ipv6point)
        results['ipv4list'] = ipv4info
    return results
def read_jsonfile(file):
    json_data = json.load(file)
    log = json_data['log']
    pages = log.get('pages', [])
    if not pages:
        print('har无页面数据')
        page_title = "未知页面"
    else:
        page = pages[0]
        page_title = page['title']
        print("")
        print("加载的页面:", page_title)
    entries = log.get('entries', [])
    if 'entries' in log:
        results = analysis_request(entries)
        if results:
            results['page'] = page_title
            return results
def export_excel(data):
    # 创立一个数据字典
    # data = {'Name': ['Tom', 'Nick', 'John'],'Age': [20, 21, 19]}
    # 将数据字典转换为pandas DataFrame
    df = pd.DataFrame(data)
    # 将DataFrame导出为Excel文件
    df.to_excel(output_file, index=False)
def format_excel():
    # 加载Excel文件
    wb = load_workbook(output_file)
    # 选择要修正的作业表
    ws = wb.active
    # 设置第一列的宽度为特定值
    ws.column_dimensions['A'].width = 40
    ws.column_dimensions['B'].width = 15
    ws.column_dimensions['C'].width = 80
    # 遍历一切单元格,设置其对齐方法为上下居中
    for row in ws.iter_rows():
        for cell in row:
            cell.alignment = Alignment(vertical='center', wrap_text=True)
    # 获取C列的一切单元格
    # c_column_cells = ws['C']
    # for cell in c_column_cells:
    #     # 设置其对齐方法为主动换行
    #     cell.alignment = Alignment(wrap_text=True)
    # 保存修正后的Excel文件
    wb.save(output_file)
def traverse_files(directory):
    # 指定当前目录途径
    # directory = os.getcwd()
    print(directory)
    # 指定文件格局
    file_format = '*.har'
    # 运用glob模块获取匹配的文件途径列表
    file_list = glob.glob(os.path.join(directory, file_format))
    pages = []
    ipv6points = []
    ipv4lists = []
    # 遍历文件列表
    for file_path in file_list:
        # 打开文件进行处理
        with open(file_path, 'r') as file:
            result = read_jsonfile(file)
            if result:
                pages.append(result['page'])
                ipv6points.append(result['ipv6point'])
                ipv4lists.append(result['ipv4list'])
    if pages:
        data = {
            '页面': pages,
            'IPv6占比': ipv6points,
            'IPv4域名列表': ipv4lists
        }
        export_excel(data)
        format_excel()
# 创立一个解析器
parser = argparse.ArgumentParser(description='剖析IPv6数据')
# 增加一个命令行参数
parser.add_argument('--directory', type=str, help='要剖析的文件的目录')
parser.add_argument('--d', type=str, help='要剖析的文件的目录')
# 解析命令行参数
args = parser.parse_args()
directory = os.getcwd()
if args.directory:
    directory = args.directory
elif args.d:
    directory = args.d
output_file = directory + '/output.xlsx'
# 运用命令行参数
traverse_files(directory)

履行代码:
python3 analysis_ipv6.py --directory '/Users/mac/Desktop/IPV6/other'

如何获取抓包内容,请参阅原文。
原文在知乎:zhuanlan.zhihu.com/p/665965753