如何用脚本快速建立虚拟局域网

wen 实用脚本 1

本文目录导读:

如何用脚本快速建立虚拟局域网

  1. 使用 ZeroTier One(推荐)
  2. 使用 Tailscale(最简单)
  3. 使用 WireGuard(自定义VPN)
  4. 使用 n2n(轻量级)
  5. 一键部署脚本(综合)
  6. 使用建议

我来介绍几种快速建立虚拟局域网的方法,使用脚本自动化部署。

使用 ZeroTier One(推荐)

安装脚本

#!/bin/bash
# ZeroTier 快速安装和加入网络
# 设置你的网络ID
NETWORK_ID="你的网络ID"
# 安装 ZeroTier
curl -s https://install.zerotier.com | sudo bash
# 加入网络
sudo zerotier-cli join $NETWORK_ID
# 允许通过防火墙
sudo ufw allow 9993/udp
echo "已加入网络: $NETWORK_ID"
echo "设备ID: $(sudo zerotier-cli info | cut -d ' ' -f 3)"

快速加入脚本(Windows)

# ZeroTier-Windows.ps1
$networkID = "你的网络ID"
# 下载安装
Invoke-WebRequest -Uri "https://download.zerotier.com/dist/ZeroTier%20One.msi" -OutFile "$env:TEMP\ZeroTier.msi"
Start-Process msiexec.exe -ArgumentList "/i `"$env:TEMP\ZeroTier.msi`" /quiet" -Wait
# 加入网络
Start-Sleep -Seconds 10
& "C:\Program Files\ZeroTier\One\zerotier-cli.bat" join $networkID
Write-Host "已加入网络: $networkID"

使用 Tailscale(最简单)

Linux/Mac 安装脚本

#!/bin/bash
# Tailscale 一键安装和配置
# 安装
curl -fsSL https://tailscale.com/install.sh | sh
# 启动并认证
sudo tailscale up
echo "请访问上面的URL进行认证"
echo "认证完成后,所有设备都在同一网络"

多节点快速部署脚本

#!/bin/bash
# 批量部署Tailscale
# 节点列表
NODES=("node1" "node2" "node3")
for node in "${NODES[@]}"; do
    echo "部署 $node..."
    ssh $node "curl -fsSL https://tailscale.com/install.sh | sh && sudo tailscale up"
done
echo "所有节点已加入虚拟局域网"

使用 WireGuard(自定义VPN)

快速配置脚本

#!/bin/bash
# WireGuard 快速搭建
# 安装
sudo apt update && sudo apt install -y wireguard
# 生成密钥
wg genkey | tee privatekey | wg pubkey > publickey
# 创建配置
cat > /etc/wireguard/wg0.conf << EOF
[Interface]
PrivateKey = $(cat privatekey)
Address = 10.0.0.1/24
ListenPort = 51820
# 其他节点配置(示例)
[Peer]
PublicKey = 其他节点公钥
AllowedIPs = 10.0.0.2/32
EOF
# 启动
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
echo "WireGuard 已启动,IP: 10.0.0.1"

客户端配置生成脚本

#!/usr/bin/env python3
# gen_wg_config.py - 生成WireGuard配置
def generate_peer_config(peer_ip, private_key, server_public_key, server_endpoint):
    config = f"""
[Interface]
PrivateKey = {private_key}
Address = {peer_ip}/24
DNS = 1.1.1.1
[Peer]
PublicKey = {server_public_key}
Endpoint = {server_endpoint}:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
"""
    return config
# 使用示例
if __name__ == "__main__":
    import subprocess
    # 生成密钥对
    private_key = subprocess.run(['wg', 'genkey'], capture_output=True, text=True).stdout.strip()
    config = generate_peer_config(
        peer_ip="10.0.0.2",
        private_key=private_key,
        server_public_key="服务器公钥",
        server_endpoint="服务器IP"
    )
    with open("client.conf", "w") as f:
        f.write(config)
    print("客户端配置文件已生成: client.conf")

使用 n2n(轻量级)

超级节点+边缘节点脚本

#!/bin/bash
# n2n 快速搭建
# 安装
sudo apt install -y n2n
# 启动超级节点 (公网服务器)
sudo supernode -l 10000 -v
# 边缘节点连接脚本
cat > connect_vpn.sh << 'EOF'
#!/bin/bash
sudo edge -d edge0 \
    -c 网络名 \
    -k 密码 \
    -l 超级节点IP:10000 \
    -a 10.0.0.2/24
EOF
chmod +x connect_vpn.sh

一键部署脚本(综合)

#!/bin/bash
# auto_vpn.sh - 自动选择并配置虚拟局域网
echo "选择虚拟局域网方案:"
echo "1) ZeroTier (推荐)"
echo "2) Tailscale (最简单)"
echo "3) WireGuard (高性能)"
echo "4) n2n (轻量级)"
read -p "选择 (1-4): " choice
case $choice in
    1)
        # ZeroTier
        curl -s https://install.zerotier.com | sudo bash
        read -p "输入网络ID: " net_id
        sudo zerotier-cli join $net_id
        ;;
    2)
        # Tailscale
        curl -fsSL https://tailscale.com/install.sh | sh
        sudo tailscale up
        ;;
    3)
        # WireGuard
        sudo apt install -y wireguard
        wg genkey | tee privatekey | wg pubkey > publickey
        echo "公钥: $(cat publickey)"
        echo "请手动配置其他节点"
        ;;
    4)
        # n2n
        sudo apt install -y n2n
        read -p "超级节点IP: " supernode
        read -p "网络名: " netname
        sudo edge -d edge0 -c $netname -k vpn123 -l $supernode:10000
        ;;
esac
echo "虚拟局域网配置完成"

使用建议

  1. 个人使用: 推荐 Tailscale,最简单的配置
  2. 团队协作: 使用 ZeroTier,支持Web管理
  3. 高性能需求: WireGuard,延迟最低
  4. 资源受限: n2n,系统占用最小

选择适合你的方案,使用这些脚本基本上5分钟内就能建立虚拟局域网,需要我可以帮你定制特定场景的脚本。

抱歉,评论功能暂时关闭!