#!/bin/bash

# 分发SSH公钥到所有Nomad节点
echo "分发SSH公钥到Nomad节点..."

# 节点列表
NODES=(
    "100.81.26.3"      # ash1d.global
    "100.103.147.94"   # ash2e.global  
    "100.90.159.68"    # ch2.global
    "100.86.141.112"   # ch3.global
    "100.117.106.136"  # master
    "100.116.80.94"    # ash3c
)

PUB_KEY=$(cat /home/ben/.ssh/id_ed25519.pub)

for NODE in "${NODES[@]}"; do
    echo "正在配置节点: $NODE"
    
    # 尝试使用现有密钥连接并添加新密钥
    ssh-keyscan -H $NODE >> ~/.ssh/known_hosts 2>/dev/null
    
    # 使用现有认证方式添加密钥
    ssh root@$NODE "echo '$PUB_KEY' >> /root/.ssh/authorized_keys" 2>/dev/null && \
        echo "✓ $NODE 配置成功" || echo "✗ $NODE 配置失败"
done

echo "密钥分发完成"