-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
221 lines (182 loc) · 6.73 KB
/
Copy pathinstall.ps1
File metadata and controls
221 lines (182 loc) · 6.73 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# sshx Automatic Installation Script for Windows
# PowerShell script to install sshx on Windows
param(
[string]$Version = "latest",
[string]$InstallDir = "$env:LOCALAPPDATA\Programs\sshx"
)
$ErrorActionPreference = "Stop"
# Configuration
$Repo = "talkincode/sshx"
$BinaryName = "sshx.exe"
# Functions
function Write-ColorOutput {
param(
[string]$Message,
[string]$Type = "Info"
)
switch ($Type) {
"Success" { Write-Host "✓ $Message" -ForegroundColor Green }
"Error" { Write-Host "✗ $Message" -ForegroundColor Red }
"Warning" { Write-Host "⚠ $Message" -ForegroundColor Yellow }
"Info" { Write-Host "ℹ $Message" -ForegroundColor Blue }
default { Write-Host $Message }
}
}
function Get-LatestVersion {
Write-ColorOutput "Fetching latest version..." "Info"
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest"
return $response.tag_name
}
catch {
Write-ColorOutput "Failed to fetch latest version: $_" "Error"
exit 1
}
}
function Get-Platform {
$arch = $env:PROCESSOR_ARCHITECTURE
switch ($arch) {
"AMD64" { return "windows-amd64" }
"ARM64" { return "windows-arm64" }
default {
Write-ColorOutput "Unsupported architecture: $arch" "Error"
exit 1
}
}
}
function Install-Sshx {
$platform = Get-Platform
$targetVersion = $Version
if ($targetVersion -eq "latest") {
$targetVersion = Get-LatestVersion
}
Write-ColorOutput "Platform: $platform" "Info"
Write-ColorOutput "Version: $targetVersion" "Info"
# Construct download URL
$filename = "sshx-$platform.zip"
$downloadUrl = "https://github.com/$Repo/releases/download/$targetVersion/$filename"
Write-ColorOutput "Downloading from: $downloadUrl" "Info"
# Create temporary directory
$tmpDir = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Path $tmpDir | Out-Null
$zipFile = Join-Path $tmpDir $filename
try {
# Download
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile -UseBasicParsing
Write-ColorOutput "Downloaded successfully" "Success"
# Verify checksum
Write-ColorOutput "Downloading checksums..." "Info"
$checksumFile = Join-Path $tmpDir "checksums.txt"
Invoke-WebRequest -Uri "https://github.com/$Repo/releases/download/$targetVersion/checksums.txt" -OutFile $checksumFile -UseBasicParsing
$escapedFilename = [regex]::Escape($filename)
$line = Get-Content $checksumFile | Where-Object { $_ -match "\s$escapedFilename$" } | Select-Object -First 1
if (-not $line) {
throw "Checksum for $filename not found"
}
$expectedHash = ($line -split '\s+')[0].ToLowerInvariant()
$actualHash = (Get-FileHash -Algorithm SHA256 -Path $zipFile).Hash.ToLowerInvariant()
if ($actualHash -ne $expectedHash) {
throw "Checksum verification failed. Expected $expectedHash, got $actualHash"
}
Write-ColorOutput "Checksum verified" "Success"
# Extract
Write-ColorOutput "Extracting..." "Info"
Expand-Archive -Path $zipFile -DestinationPath $tmpDir -Force
# Create installation directory if it doesn't exist
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
# Move binary
Write-ColorOutput "Installing to $InstallDir..." "Info"
$sourceBinary = Join-Path $tmpDir $BinaryName
$destBinary = Join-Path $InstallDir $BinaryName
if (Test-Path $destBinary) {
Remove-Item $destBinary -Force
}
Move-Item $sourceBinary $destBinary -Force
Write-ColorOutput "Installation complete!" "Success"
}
catch {
Write-ColorOutput "Installation failed: $_" "Error"
exit 1
}
finally {
# Cleanup
Remove-Item $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
function Add-ToPath {
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -notlike "*$InstallDir*") {
Write-ColorOutput "Adding $InstallDir to PATH..." "Info"
$newPath = "$currentPath;$InstallDir"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
# Update current session
$env:Path = "$env:Path;$InstallDir"
Write-ColorOutput "Added to PATH successfully" "Success"
Write-ColorOutput "Please restart your terminal for PATH changes to take effect" "Warning"
}
else {
Write-ColorOutput "$InstallDir is already in PATH" "Info"
}
}
function Test-Installation {
$binaryPath = Join-Path $InstallDir $BinaryName
if (Test-Path $binaryPath) {
Write-ColorOutput "$BinaryName installed successfully" "Success"
Write-ColorOutput "Location: $binaryPath" "Info"
# Try to get version
try {
$versionOutput = & $binaryPath --version 2>&1
Write-ColorOutput "Version: $versionOutput" "Info"
}
catch {
# Ignore version check errors
}
return $true
}
else {
Write-ColorOutput "Installation verification failed" "Error"
return $false
}
}
function Show-QuickStart {
Write-Host ""
Write-ColorOutput "Quick Start:" "Info"
Write-Host " # Execute remote command"
Write-Host " sshx -h=192.168.1.100 -u=Administrator 'systeminfo'"
Write-Host ""
Write-Host " # Save password (optional)"
Write-Host " sshx --password-set=master"
Write-Host ""
Write-ColorOutput "Documentation: https://github.com/$Repo" "Info"
}
# Main
function Main {
Write-Host ""
Write-Host "╔════════════════════════════════════════╗"
Write-Host "║ sshx Automatic Installer ║"
Write-Host "║ SSH & SFTP Tool with Password Mgr ║"
Write-Host "╚════════════════════════════════════════╝"
Write-Host ""
# Check for existing installation
$existingBinary = Join-Path $InstallDir $BinaryName
if (Test-Path $existingBinary) {
Write-ColorOutput "$BinaryName is already installed at: $existingBinary" "Warning"
$response = Read-Host "Do you want to overwrite it? [y/N]"
if ($response -notmatch "^[Yy]$") {
Write-ColorOutput "Installation cancelled" "Info"
exit 0
}
}
Install-Sshx
Add-ToPath
if (Test-Installation) {
Show-QuickStart
}
else {
exit 1
}
}
# Run
Main