#!/bin/bash # 配置FRP服务器SSH密钥免密登录 # 在本地Windows PowerShell中执行 $FRP_SERVER = "180.97.221.140" $FRP_USER = "root" $FRP_PASSWORD = "NEjoy4h8B9Fa" $FRP_PORT = "22" Write-Host "=========================================" -ForegroundColor Cyan Write-Host "配置FRP服务器SSH密钥免密登录" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan Write-Host "" # 检查本地是否有SSH密钥 $sshKeyPath = "$env:USERPROFILE\.ssh\id_rsa" if (-not (Test-Path $sshKeyPath)) { Write-Host "正在生成SSH密钥对..." -ForegroundColor Yellow ssh-keygen -t rsa -b 4096 -f $sshKeyPath -N '""' -q Write-Host "SSH密钥已生成: $sshKeyPath" -ForegroundColor Green } else { Write-Host "SSH密钥已存在: $sshKeyPath" -ForegroundColor Green } # 读取公钥 $publicKey = Get-Content "$sshKeyPath.pub" Write-Host "" Write-Host "正在将公钥复制到FRP服务器..." -ForegroundColor Yellow Write-Host "提示: 如果要求输入密码,请输入: $FRP_PASSWORD" -ForegroundColor Yellow Write-Host "" # 使用ssh-copy-id复制公钥(需要手动输入密码一次) $sshCopyIdCmd = "type $env:USERPROFILE\.ssh\id_rsa.pub | ssh -p $FRP_PORT $FRP_USER@$FRP_SERVER `"mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh`"" Write-Host "执行命令: $sshCopyIdCmd" -ForegroundColor Gray Write-Host "" # 执行命令 Invoke-Expression $sshCopyIdCmd Write-Host "" Write-Host "测试免密登录..." -ForegroundColor Yellow ssh -o StrictHostKeyChecking=no -p $FRP_PORT $FRP_USER@$FRP_SERVER "echo 'SSH密钥配置成功!可以免密登录了。' && hostname" Write-Host "" Write-Host "配置完成!" -ForegroundColor Green Write-Host "以后可以直接使用以下命令连接,无需输入密码:" -ForegroundColor Green Write-Host "ssh -p $FRP_PORT $FRP_USER@$FRP_SERVER" -ForegroundColor Cyan