aileeao / fix_ssh_banner_timeout.sh
aihuashanying's picture
修复不能push的问题
386b079
#!/bin/bash
echo "=== 修复 SSH Banner Exchange 超时问题 ==="
echo ""
echo "问题分析:"
echo "TCP 连接已建立,但 SSH 握手在 banner exchange 阶段超时"
echo "这可能是因为 TUN 模式没有正确处理 SSH 流量"
echo ""
SSH_CONFIG="$HOME/.ssh/config"
KEY_PATH="$HOME/.ssh/id_ed25519"
# 备份配置
if [ -f "$SSH_CONFIG" ]; then
cp "$SSH_CONFIG" "$SSH_CONFIG.backup.$(date +%Y%m%d_%H%M%S)"
fi
# 删除旧的 hf.co 配置
if grep -q "Host hf.co" "$SSH_CONFIG" 2>/dev/null; then
sed -i '/^Host hf.co$/,/^$/d' "$SSH_CONFIG"
fi
echo "尝试不同的配置方案..."
echo ""
# 方案 1: 增加超时时间,添加更多选项
echo "方案 1: 增加超时和重试选项..."
cat >> "$SSH_CONFIG" << EOF
# Hugging Face SSH 配置(增加超时和重试)
Host hf.co
HostName hf.co
User git
Port 443
IdentityFile $KEY_PATH
IdentitiesOnly yes
StrictHostKeyChecking accept-new
ConnectTimeout 60
ServerAliveInterval 10
ServerAliveCountMax 10
TCPKeepAlive yes
Compression no
LogLevel DEBUG1
EOF
chmod 600 "$SSH_CONFIG"
chmod 600 "$KEY_PATH"
chmod 644 "$KEY_PATH.pub"
echo "测试方案 1..."
timeout 70 ssh -T -o ConnectTimeout=60 [email protected] 2>&1 | head -20
SSH_EXIT=${PIPESTATUS[0]}
if [ $SSH_EXIT -eq 0 ] || [ $SSH_EXIT -eq 1 ]; then
if ! timeout 70 ssh -T [email protected] 2>&1 | grep -qi "timeout"; then
echo ""
echo "✓ 方案 1 成功!"
exit 0
fi
fi
echo "方案 1 失败,尝试方案 2..."
echo ""
# 方案 2: 尝试端口 22
sed -i '/^Host hf.co$/,/^$/d' "$SSH_CONFIG"
cat >> "$SSH_CONFIG" << EOF
# Hugging Face SSH 配置(尝试端口 22)
Host hf.co
HostName hf.co
User git
Port 22
IdentityFile $KEY_PATH
IdentitiesOnly yes
StrictHostKeyChecking accept-new
ConnectTimeout 60
ServerAliveInterval 10
ServerAliveCountMax 10
TCPKeepAlive yes
Compression no
LogLevel DEBUG1
EOF
echo "测试方案 2(端口 22)..."
timeout 70 ssh -T -o ConnectTimeout=60 [email protected] 2>&1 | head -20
SSH_EXIT=${PIPESTATUS[0]}
if [ $SSH_EXIT -eq 0 ] || [ $SSH_EXIT -eq 1 ]; then
if ! timeout 70 ssh -T [email protected] 2>&1 | grep -qi "timeout"; then
echo ""
echo "✓ 方案 2 成功(端口 22)!"
exit 0
fi
fi
echo "方案 2 失败,尝试方案 3..."
echo ""
# 方案 3: 使用 IP 地址而不是域名
HF_IP=$(getent hosts hf.co | awk '{print $1}' | head -1)
if [ -n "$HF_IP" ]; then
sed -i '/^Host hf.co$/,/^$/d' "$SSH_CONFIG"
cat >> "$SSH_CONFIG" << EOF
# Hugging Face SSH 配置(使用 IP 地址)
Host hf.co
HostName $HF_IP
User git
Port 443
IdentityFile $KEY_PATH
IdentitiesOnly yes
StrictHostKeyChecking accept-new
ConnectTimeout 60
ServerAliveInterval 10
ServerAliveCountMax 10
TCPKeepAlive yes
Compression no
LogLevel DEBUG1
EOF
echo "测试方案 3(使用 IP: $HF_IP)..."
timeout 70 ssh -T -o ConnectTimeout=60 [email protected] 2>&1 | head -20
SSH_EXIT=${PIPESTATUS[0]}
if [ $SSH_EXIT -eq 0 ] || [ $SSH_EXIT -eq 1 ]; then
if ! timeout 70 ssh -T [email protected] 2>&1 | grep -qi "timeout"; then
echo ""
echo "✓ 方案 3 成功(使用 IP)!"
exit 0
fi
fi
fi
echo ""
echo "所有方案都失败了"
echo ""
echo "可能的原因:"
echo "1. Clash Verge 的 TUN 模式可能没有正确处理 SSH 协议"
echo "2. 需要检查 Clash Verge 的规则,确保 SSH 流量被正确代理"
echo "3. 可能需要使用显式代理而不是依赖 TUN 模式"
echo ""
echo "建议:"
echo "1. 检查 Clash Verge 日志,查看是否有 hf.co 的连接记录"
echo "2. 尝试在 Clash Verge 中临时设置为全局代理模式测试"
echo "3. 或者尝试使用 HTTPS + token 的方式(虽然你不想用)"