V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Leonkennedy2
V2EX  ›  宽带症候群

Centos7.9 如何优雅地批量添加 IPv6 地址

  •  
  •   Leonkennedy2 · 242 天前 · 1154 次点击
    这是一个创建于 242 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有个需求,某独立服务器的网卡 1 ,需要绑定 256 个 IPv6 地址,

    从 2xxx:xxxx:xxxx:xxxx::1070  到 2xxx:xxxx:xxxx:xxxx::116f

    我知道如何配置 v6 ,网卡文件加入:

    IPV6ADDR=""
    IPV6ADDR_SECONDARIE=""

    问题在于这个 IPV6ADDR_SECONDARIE 的范围,一定要将这 256 个 IPv6 地址全都手写进去吗?

    我的做法比较”蠢“,我把这 256 个 IPv6 地址,用 word 处理了一下字符串,每个 v6 地址之间有个空格,然后直接复制,粘贴到 IPV6ADDR_SECONDARIE="" 的引号中。

    这只是 256 个 IPv6 ,如果是/48 的 v6 ,完全不是手动写入能处理的,想问问各位大佬,有没有更优雅的处理办法?
    10 条回复    2023-10-27 00:21:43 +08:00
    raysonx
        1
    raysonx  
       242 天前
    问题是这么做的意义在什么地方?你机器上的服务要同时使用这么多地址吗?
    terrancesiu
        2
    terrancesiu  
       242 天前 via iPhone
    @raysonx 可能用来流媒体解锁
    raysonx
        3
    raysonx  
       242 天前
    @terrancesiu 256 个容易,可以写脚本搞定。添加整个/48 段的地址这种太夸张了,还不如写个脚本来动态换地址。
    Leonkennedy2
        4
    Leonkennedy2  
    OP
       242 天前
    @raysonx 不好意思,我想错了,因为我有/48 的 v6 地址,光想着怎么配置大量 ip ,我忽略了一般人没有/48 部署在一个机器上的需求
    xinge666
        5
    xinge666  
       241 天前 via iPhone
    @terrancesiu 流媒体解锁也是直接封禁一个/64 的而不是按地址封禁
    Fangliding
        6
    Fangliding  
       241 天前
    eg 2a01:4f8:13a:19e5:106d:95e1:e10c:0001/112 ()
    julyclyde
        7
    julyclyde  
       241 天前
    @raysonx 估计是发营销电子邮件之类的,需要很多客户端 IP
    Leonkennedy2
        8
    Leonkennedy2  
    OP
       241 天前
    @julyclyde 不是,只是好奇问问
    Leonkennedy2
        9
    Leonkennedy2  
    OP
       191 天前
    时隔一个月,对于我提问的这个问题,我想到一个新的处理办法。shell 不方便处理 ipv6 的计算,用 python 脚本

    比如说我要配置从 a 之后 256 个 ipv6 到 ens33 网卡上,python 脚本可以这么写:

    import subprocess
    import ipaddress

    def add_ipv6_addresses(interface, start_ipv6, count, gateway):
    # 将 IPv6 地址转换为 ipaddress.IPv6Address 对象
    start_address = ipaddress.IPv6Address(start_ipv6)

    # 递增 IPv6 地址并配置到指定网卡
    for i in range(count):
    current_address = start_address + i
    command = f"ip -6 addr add {current_address}/56 dev {interface}"

    try:
    subprocess.run(command, shell=True, check=True)
    print(f"Successfully added {current_address} to {interface}")
    except subprocess.CalledProcessError as e:
    print(f"Failed to add {current_address} to {interface}: {e}")

    # 设置网关为指定的 IPv6 地址
    gateway_command = f"ip -6 route add default via {gateway}"

    try:
    subprocess.run(gateway_command, shell=True, check=True)
    print(f"Gateway set to {gateway}")
    except subprocess.CalledProcessError as e:
    print(f"Failed to set gateway to {gateway}: {e}")

    # IPv6 起始 IP ,数量,网关 变量

    interface = "ens33"
    start_ipv6 = "ipv6 的起始 IP"
    count = 256
    gateway = "ipv6 的网关"

    add_ipv6_addresses(interface, start_ipv6, count, gateway)


    这个 py 脚本另存为一个 add256v6.py ,目录在/etc/sysconfig/network-script/ , 配置 systemctl ,创建一个服务让这个 py 脚本开机自启一次

    touch /etc/systemd/system/add256v6.service


    add256v6.service:

    [Unit]
    Description=Add256v6 Service
    After=network.target

    [Service]
    ExecStart=/usr/bin/python3 /etc/sysconfig/network-script/add256v6.py

    [Install]
    WantedBy=default.target


    最后 sudo systemctl enable add256v6.service && sudo systemctl start add256v6.service && sudo systemctl status add256v6.service
    Leonkennedy2
        10
    Leonkennedy2  
    OP
       191 天前
    这样的话,2k 个 IP 也可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2715 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:59 · PVG 20:59 · LAX 05:59 · JFK 08:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.