enet-validator/scripts/session/deploy_tests.ps1

30 lines
1.5 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = "Stop"
Set-ExecutionPolicy -Scope Process Bypass -Force
Import-Module Posh-SSH
$sec = ConvertTo-SecureString "0" -AsPlainText -Force
$cred = New-Object PSCredential("tonycao", $sec)
$s = New-SSHSession -ComputerName 192.168.58.7 -Credential $cred -AcceptKey -ConnectionTimeout 15
if (-not $s) { throw "SSH 连接失败" }
$src = "C:\Users\tonycao\work\light\enet-validator\src\tests.rs"
$b64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($src))
Write-Host ("本地文件 {0} 字节, base64 {1} 字符" -f (Get-Item $src).Length, $b64.Length)
# 分块传输
$chunkSize = 6000
$chunks = [Math]::Ceiling($b64.Length / $chunkSize)
$r = Invoke-SSHCommand -SSHSession $s -Command "rm -f /tmp/tests.b64 && touch /tmp/tests.b64 && echo OK"
for ($i = 0; $i -lt $chunks; $i++) {
$start = $i * $chunkSize
$len = [Math]::Min($chunkSize, $b64.Length - $start)
$part = $b64.Substring($start, $len)
$r = Invoke-SSHCommand -SSHSession $s -Command "echo '$part' >> /tmp/tests.b64 && wc -c < /tmp/tests.b64"
if (($i % 3) -eq 0) { Write-Host (" chunk {0}/{1}: {2}" -f ($i+1), $chunks, ($r.Output -join "")) }
}
$r = Invoke-SSHCommand -SSHSession $s -Command "base64 -d /tmp/tests.b64 > ~/enet-validator/src/tests.rs && wc -c < ~/enet-validator/src/tests.rs && md5sum ~/enet-validator/src/tests.rs"
Write-Host ("解码: " + ($r.Output -join " | "))
$localMd5 = (Get-FileHash $src -Algorithm MD5).Hash.ToLower()
Write-Host ("本地MD5: $localMd5")
Remove-SSHSession $s | Out-Null