59 lines
2.6 KiB
PowerShell
59 lines
2.6 KiB
PowerShell
# 配置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 ""
|
||
|
||
# 使用专用密钥名称,避免与其他项目冲突
|
||
$sshKeyPath = "$env:USERPROFILE\.ssh\id_rsa_frp_180.97.221.140"
|
||
if (-not (Test-Path $sshKeyPath)) {
|
||
Write-Host "正在生成FRP服务器专用SSH密钥对..." -ForegroundColor Yellow
|
||
ssh-keygen -t rsa -b 4096 -f $sshKeyPath -N '""' -q -C "frp-server-180.97.221.140"
|
||
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 ""
|
||
|
||
# 使用专用密钥复制公钥(需要手动输入密码一次)
|
||
$sshCopyIdCmd = "type `"$sshKeyPath.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 -i $sshKeyPath -o StrictHostKeyChecking=no -p $FRP_PORT $FRP_USER@$FRP_SERVER "echo 'SSH密钥配置成功!可以免密登录了。' && hostname"
|
||
|
||
Write-Host ""
|
||
Write-Host "配置完成!" -ForegroundColor Green
|
||
Write-Host "以后可以使用以下命令连接,无需输入密码:" -ForegroundColor Green
|
||
$connectCmd = "ssh -i `"$sshKeyPath`" -p $FRP_PORT $FRP_USER@$FRP_SERVER"
|
||
Write-Host $connectCmd -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "或者创建SSH配置文件,使用别名连接:" -ForegroundColor Yellow
|
||
Write-Host "在 ~/.ssh/config 中添加以下内容:" -ForegroundColor Yellow
|
||
Write-Host "Host frp-server" -ForegroundColor Cyan
|
||
Write-Host " HostName $FRP_SERVER" -ForegroundColor Cyan
|
||
Write-Host " User $FRP_USER" -ForegroundColor Cyan
|
||
Write-Host " Port $FRP_PORT" -ForegroundColor Cyan
|
||
Write-Host " IdentityFile `"$sshKeyPath`"" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "然后就可以使用: ssh frp-server" -ForegroundColor Green |