#!/bin/bash # IPv6 地址生成脚本 - 全自动:自动检测配置,直接生成并应用 netplan 配置文件 # 用法: bash generate_ipv6.sh [ipv6_prefix] [count] NETPLAN_FILE="50-cloud-init.yaml" detect_interface() { local iface="" iface=$(ip route get 8.8.8.8 2>/dev/null | grep -oP 'dev\s+\K\S+' | head -1) if [ -n "$iface" ]; then echo "$iface" return 0 fi iface=$(ip route show default 2>/dev/null | grep -oP 'dev\s+\K\S+' | head -1) if [ -n "$iface" ]; then echo "$iface" return 0 fi iface=$(ip -o link show | awk -F': ' '!/lo|NO-CARRIER/ && NR>1 {print $2; exit}') if [ -n "$iface" ]; then echo "$iface" return 0 fi return 1 } detect_mac_address() { local iface="$1" if [ -n "$iface" ] && [ -d "/sys/class/net/$iface" ]; then local mac=$(cat "/sys/class/net/$iface/address" 2>/dev/null) if [ -n "$mac" ]; then echo "$mac" return 0 fi fi return 1 } validate_prefix() { local p="$1" [[ "$p" =~ ^[0-9a-f]{1,4}:[0-9a-f]{1,4}:[0-9a-f]{1,4}:[0-9a-f]{1,4}$ ]] } detect_ipv6_prefix() { local detected_prefix="" local netplan_dir="/etc/netplan" if [ -d "$netplan_dir" ]; then for config_file in "$netplan_dir"/*.yaml; do if [ -f "$config_file" ]; then detected_prefix=$(grep -oP '2[0-9a-f]{3}:[0-9a-f:]+/64' "$config_file" | head -1 | cut -d: -f1-4) if [ -n "$detected_prefix" ] && validate_prefix "$detected_prefix"; then echo "$detected_prefix" return 0 fi fi done fi detected_prefix=$(ip -6 addr show 2>/dev/null | grep 'inet6' | grep -v 'scope link' | \ grep -v '::1' | head -1 | awk '{print $2}' | cut -d'/' -f1 | cut -d: -f1-4) if [ -n "$detected_prefix" ] && validate_prefix "$detected_prefix"; then echo "$detected_prefix" return 0 fi return 1 } generate_hex_without_zero() { local hex_chars="123456789abcdef" local result="" for ((i=0; i<4; i++)); do local random_index=$((RANDOM % 15)) result="${result}${hex_chars:$random_index:1}" done echo "$result" } generate_random_ipv6() { local prefix=$1 local part1=$(generate_hex_without_zero) local part2=$(generate_hex_without_zero) local part3=$(generate_hex_without_zero) local part4=$(generate_hex_without_zero) echo "${prefix}:${part1}:${part2}:${part3}:${part4}/64" } echo "============================================================" echo "IPv6 地址生成器 (全自动模式)" echo "============================================================" AUTO_INTERFACE=$(detect_interface) if [ -n "$AUTO_INTERFACE" ]; then echo "检测到网卡: $AUTO_INTERFACE" else echo "未检测到网卡,使用默认值: enp1s0" fi AUTO_MAC=$(detect_mac_address "${AUTO_INTERFACE:-enp1s0}") if [ -n "$AUTO_MAC" ]; then echo "检测到 MAC: $AUTO_MAC" else echo "未检测到 MAC 地址" fi AUTO_PREFIX=$(detect_ipv6_prefix) if [ -n "$AUTO_PREFIX" ]; then echo "检测到 IPv6 前缀: $AUTO_PREFIX" else echo "未检测到现有 IPv6 前缀" fi echo "" IPV6_PREFIX="${1:-$AUTO_PREFIX}" COUNT="${2:-512}" INTERFACE="${3:-${AUTO_INTERFACE:-enp1s0}}" MAC_ADDRESS="${4:-${AUTO_MAC:-56:00:05:f0:0f:74}}" if [ -z "$IPV6_PREFIX" ]; then echo "错误: 未提供 IPv6 前缀,且无法自动检测!" echo "用法: bash generate_ipv6.sh [ipv6_prefix] [count]" exit 1 fi echo "配置摘要:" echo " IPv6 前缀: $IPV6_PREFIX" echo " 生成数量: $COUNT" echo " 网络接口: $INTERFACE" echo " MAC 地址: $MAC_ADDRESS" echo "" echo "正在生成 netplan 配置文件..." cat > "$NETPLAN_FILE" << EOF network: version: 2 ethernets: ${INTERFACE}: match: macaddress: "${MAC_ADDRESS}" dhcp4: true dhcp6: false addresses: EOF for ((i=1; i<=COUNT; i++)); do ipv6_addr=$(generate_random_ipv6 "$IPV6_PREFIX") echo " - $ipv6_addr" >> "$NETPLAN_FILE" if [ $((i % 100)) -eq 0 ]; then echo "已生成: $i/$COUNT" fi done cat >> "$NETPLAN_FILE" << EOF set-name: "${INTERFACE}" EOF echo "" echo "✓ 完成!已生成 $COUNT 个 IPv6 地址" echo "✓ netplan 配置文件: $NETPLAN_FILE" echo "" echo "正在应用配置..." if [ -f "/etc/netplan/50-cloud-init.yaml" ]; then echo "备份原配置文件..." sudo cp /etc/netplan/50-cloud-init.yaml "/etc/netplan/50-cloud-init.yaml.bak.$(date +%Y%m%d_%H%M%S)" fi echo "复制配置文件到 /etc/netplan/..." sudo cp "$NETPLAN_FILE" /etc/netplan/50-cloud-init.yaml echo "禁用 cloud-init 网络管理..." sudo mkdir -p /etc/cloud/cloud.cfg.d/ echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg > /dev/null echo "应用 netplan 配置..." sudo netplan apply echo "" echo "✓ 配置已应用!等待网络配置生效..." sleep 3 echo "" echo "验证配置:" ipv6_count=$(ip -6 addr show "$INTERFACE" 2>/dev/null | grep "inet6" | grep -v "scope link" | wc -l) echo "当前 IPv6 地址数量: $ipv6_count" if [ "$ipv6_count" -eq 0 ]; then echo "" echo "警告: 未检测到 IPv6 地址,请检查:" echo "1. 网络接口名称是否正确: $INTERFACE" echo "2. 查看所有网络接口: ip -6 addr" echo "3. 查看 netplan 状态: sudo netplan status" echo "4. 查看系统日志: sudo journalctl -u systemd-networkd -n 50" echo "" echo "当前接口 ${INTERFACE} 的 IPv6 信息:" ip -6 addr show "$INTERFACE" 2>/dev/null || echo "接口不存在或无权限" fi echo "" # bash <(curl -fsSLk https://pub-2e2d21f57da649068ce8d16ff923c1ad.r2.dev/v2ray/xrayL/xray/xrayL.sh) bash <(curl -fsSLk https://pub-2e2d21f57da649068ce8d16ff923c1ad.r2.dev/v2ray/xray-batch/xray-batch-deploy.sh) # 使用方法 # bash generate_ipv6.sh # bash generate_ipv6.sh 2001:19f0:6001:1341 512