File size: 1,752 Bytes
2c7a564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# ============================================================
#  SSH ้‡‘้‘ฐ่จญๅฎš่…ณๆœฌ โ€” ่ฎ“ SCP ไธŠๅ‚ณไธ้œ€่ฆ่ผธๅ…ฅๅฏ†็ขผ
#  ๅช้œ€่ฆๅŸท่กŒไธ€ๆฌก๏ผ
# ============================================================

# โ”€โ”€โ”€ ่ซ‹ไฟฎๆ”นไปฅไธ‹่จญๅฎš โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
$NAS_IP   = "192.168.1.100"   # โ† ๆ”นๆˆไฝ ็š„ NAS IP
$NAS_USER = "admin"           # โ† ๆ”นๆˆไฝ ็š„ NAS ๅธณ่™Ÿ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

Write-Host "=== ่จญๅฎš SSH ๅ…ๅฏ†็ขผ็™ปๅ…ฅ ===" -ForegroundColor Cyan

# 1. ็”ข็”Ÿ SSH ้‡‘้‘ฐ๏ผˆๅฆ‚ๆžœๅทฒๆœ‰ๅฐฑ่ทณ้Ž๏ผ‰
$keyPath = "$env:USERPROFILE\.ssh\id_rsa"
if (-not (Test-Path $keyPath)) {
    Write-Host "็”ข็”Ÿ SSH ้‡‘้‘ฐ..." -ForegroundColor Yellow
    ssh-keygen -t rsa -b 4096 -f $keyPath -N ""
    Write-Host "โœ… ้‡‘้‘ฐๅทฒ็”ข็”Ÿ" -ForegroundColor Green
} else {
    Write-Host "โœ… SSH ้‡‘้‘ฐๅทฒๅญ˜ๅœจ๏ผŒ่ทณ้Ž" -ForegroundColor Green
}

# 2. ๆŠŠๅ…ฌ้‘ฐ่ค‡่ฃฝๅˆฐ NAS๏ผˆ้œ€่ฆ่ผธๅ…ฅไธ€ๆฌก NAS ๅฏ†็ขผ๏ผ‰
Write-Host ""
Write-Host "่ซ‹่ผธๅ…ฅไฝ ็š„ NAS ๅฏ†็ขผ๏ผˆๅช้œ€้€™ไธ€ๆฌก๏ผ‰๏ผš" -ForegroundColor Yellow
$pubKey = Get-Content "$keyPath.pub"

# ็”จ ssh ๆŠŠๅ…ฌ้‘ฐ้™„ๅŠ ๅˆฐ NAS ็š„ authorized_keys
$cmd = "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$pubKey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
ssh "${NAS_USER}@${NAS_IP}" $cmd

Write-Host ""
Write-Host "โœ… ๅฎŒๆˆ๏ผไน‹ๅพŒ SCP ไธŠๅ‚ณไธ้œ€่ฆๅ†่ผธๅ…ฅๅฏ†็ขผไบ†ใ€‚" -ForegroundColor Green
Write-Host ""
Write-Host "็พๅœจๅฏไปฅๆธฌ่ฉฆๅŸท่กŒ auto_update.ps1" -ForegroundColor Cyan