auto-turning/ssh_setup.ps1

127 lines
6.8 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ============================================================
# SSH INSTALLER
# ============================================================
$repo = "https://git.help-d.ru/helmut/auto-turning/raw/branch/main"
$temp = "$env:TEMP\ssh_setup"
Write-Host "`n========== SSH УСТАНОВКА ==========`n" -ForegroundColor Cyan
# Проверка наличия SSH
$sshInstalled = Get-WindowsCapability -Online | Where-Object Name -like "OpenSSH.Server*" | Select-Object -ExpandProperty State
if ($sshInstalled -eq "Installed") {
Write-Host "✓ SSH уже установлен" -ForegroundColor Green
# Проверка настройки sshd_config
$cfg = "C:\ProgramData\ssh\sshd_config"
$currentPort = (Get-Content $cfg | Select-String "^Port" | Select-Object -Last 1).ToString().Split()[1]
if ($currentPort -eq "2222" -and (Get-Content $cfg | Select-String "^#Match Group administrators")) {
Write-Host "✓ SSH уже настроен согласно скрипту" -ForegroundColor Green
Write-Host " Пропускаем настройку..." -ForegroundColor Yellow
} else {
Write-Host "! SSH установлен, но настроен не по шаблону" -ForegroundColor Yellow
Write-Host " Выполняю настройку..." -ForegroundColor Yellow
# Настраиваю авторизацию...
Write-Host "`n3. Настраиваю авторизацию..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path "$env:USERPROFILE\.ssh" -Force | Out-Null
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHceolzaC/nnN14/lVqeXzcXcIANtQgJcIaFczLIchVo efgufk/admin@ISERVER" | Out-File "$env:USERPROFILE\.ssh\authorized_keys" -Force
Write-Host " ✓ Готово" -ForegroundColor Green
# Настраиваю sshd_config...
Write-Host "`n4. Настраиваю sshd_config..." -ForegroundColor Yellow
$c = Get-Content $cfg
$c = $c -replace '^#Port 22', 'Port 2222' -replace '^Port 22', 'Port 2222' -replace '^Match Group administrators', '#Match Group administrators' -replace '^ AuthorizedKeysFile', '# AuthorizedKeysFile'
$c | Set-Content $cfg
Write-Host " ✓ Готово" -ForegroundColor Green
# Перезапускаю службу...
Write-Host "`n5. Перезапускаю службу..." -ForegroundColor Yellow
Restart-Service sshd -Force
Write-Host " ✓ Готово" -ForegroundColor Green
}
} else {
Write-Host "! SSH не установлен, выполняю установку..." -ForegroundColor Yellow
# 1. Скачиваю установщик...
Write-Host "`n1. Скачиваю установщик..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path $temp -Force | Out-Null
Invoke-WebRequest -Uri "$repo/installers/OpenSSH.msi" -OutFile "$temp\OpenSSH.msi" -ErrorAction Stop
Write-Host " ✓ Готово" -ForegroundColor Green
# 2. Устанавливаю SSH...
Write-Host "`n2. Устанавливаю SSH..." -ForegroundColor Yellow
msiexec /i "$temp\OpenSSH.msi" /quiet /norestart
Write-Host " ✓ Готово" -ForegroundColor Green
# 3. Настраиваю авторизацию...
Write-Host "`n3. Настраиваю авторизацию..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path "$env:USERPROFILE\.ssh" -Force | Out-Null
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHceolzaC/nnN14/lVqeXzcXcIANtQgJcIaFczLIchVo efgufk/admin@ISERVER" | Out-File "$env:USERPROFILE\.ssh\authorized_keys" -Force
Write-Host " ✓ Готово" -ForegroundColor Green
# 4. Настраиваю sshd_config...
Write-Host "`n4. Настраиваю sshd_config..." -ForegroundColor Yellow
$cfg = "C:\ProgramData\ssh\sshd_config"
$c = Get-Content $cfg
$c = $c -replace '^#Port 22', 'Port 2222' -replace '^Match Group administrators', '#Match Group administrators' -replace '^ AuthorizedKeysFile', '# AuthorizedKeysFile'
$c | Set-Content $cfg
Write-Host " ✓ Готово" -ForegroundColor Green
# 5. Перезапускаю службу...
Write-Host "`n5. Перезапускаю службу..." -ForegroundColor Yellow
Restart-Service sshd -Force
Write-Host " ✓ Готово" -ForegroundColor Green
}
# 6. Чистка мусора...
Write-Host "`n6. Чистка мусора..." -ForegroundColor Yellow
if (Test-Path $temp) {
Remove-Item $temp -Recurse -Force
Write-Host " ✓ Временные файлы удалены" -ForegroundColor Green
}
# Очистка кэша Windows
CleanMgr /sagerun:1 | Out-Null
Write-Host " ✓ Системный мусор очищен" -ForegroundColor Green
# 7. Запуск антивирусной проверки...
Write-Host "`n7. Запуск антивирусной проверки..." -ForegroundColor Yellow
# Проверяем наличие Windows Defender
$defenderStatus = Get-MpComputerStatus
if ($defenderStatus) {
Write-Host " Запуск быстрой проверки Windows Defender..." -ForegroundColor Yellow
Start-MpScan -ScanType QuickScan
Write-Host " ✓ Антивирусная проверка запущена" -ForegroundColor Green
} else {
Write-Host " ! Windows Defender не обнаружен" -ForegroundColor Red
# Попытка запуска стороннего антивируса (пример для Kaspersky)
$antivirusPaths = @(
"C:\Program Files\Windows Defender\MpCmdRun.exe",
"C:\Program Files\Kaspersky Lab\Kaspersky*\avp.exe",
"C:\Program Files\ESET\ESET Security\ecls.exe"
)
foreach ($av in $antivirusPaths) {
if (Test-Path $av) {
Write-Host " Запуск проверки: $av" -ForegroundColor Yellow
Start-Process $av -ArgumentList "/scan" -NoNewWindow
Write-Host " ✓ Антивирусная проверка запущена" -ForegroundColor Green
break
}
}
}
# Результат
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -notlike "*Loopback*" -and $_.InterfaceAlias -notlike "*vEthernet*"}).IPAddress
Write-Host "`n========== ГОТОВО ==========" -ForegroundColor Green
Write-Host "Подключайтесь: ssh -p 2222 $env:USERNAME@$ip" -ForegroundColor Cyan
Write-Host "============================`n" -ForegroundColor Green
# Уведомление о завершении
Write-Host "✓ Установка завершена" -ForegroundColor Green
Write-Host "Мусор очищен" -ForegroundColor Green
Write-Host "✓ Антивирусная проверка запущена в фоновом режиме" -ForegroundColor Green