Skip to content

Instantly share code, notes, and snippets.

@Ma233
Last active August 8, 2025 11:32
Show Gist options
  • Select an option

  • Save Ma233/7c27a83903bde0c0e928172d215a4cfd to your computer and use it in GitHub Desktop.

Select an option

Save Ma233/7c27a83903bde0c0e928172d215a4cfd to your computer and use it in GitHub Desktop.
# SSH File Batch Download Script
param(
[string]$IpListFile = "ip_list.txt",
[string]$OutputDirectory = "downloaded_files",
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[string]$Password,
[string]$PlinkPath = ".\plink.exe",
[Parameter(Mandatory=$true)]
[string]$RemoteFilePath
)
# Check if plink.exe exists
if (!(Test-Path $PlinkPath)) {
Write-Error "plink.exe not found at: $PlinkPath"
Write-Host "You can:"
Write-Host "1. Download plink.exe from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html"
Write-Host "2. Put the downloaded plink.exe file to the same path of this script"
exit 1
}
# Check if IP list file exists
if (!(Test-Path $IpListFile)) {
Write-Error "IP list file not found: $IpListFile"
Write-Host "Please create a text file containing IP addresses, one per line"
Write-Host "Example content:"
Write-Host "192.168.1.100"
Write-Host "192.168.1.101"
Write-Host "192.168.1.102"
exit 1
}
# Create output directory if it doesn't exist
if (!(Test-Path $OutputDirectory)) {
try {
New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null
Write-Host "Created output directory: $OutputDirectory" -ForegroundColor Blue
}
catch {
Write-Error "Failed to create output directory: $_"
exit 1
}
}
# Read IP list from file
try {
$ip_list = Get-Content $IpListFile | Where-Object { $_.Trim() -ne "" }
Write-Host "Read $($ip_list.Count) IP addresses from file" -ForegroundColor Blue
}
catch {
Write-Error "Failed to read IP list file: $_"
exit 1
}
# Get filename from remote path
$fileName = Split-Path $RemoteFilePath -Leaf
# Process each IP address
foreach ($ip in $ip_list) {
$ip = $ip.Trim()
if ($ip -eq "") { continue }
Write-Host "========== Processing $ip ==========" -ForegroundColor Green
# Create IP-specific directory
$ipDirectory = Join-Path $OutputDirectory $ip
try {
if (!(Test-Path $ipDirectory)) {
New-Item -ItemType Directory -Path $ipDirectory -Force | Out-Null
Write-Host "Created directory: $ipDirectory" -ForegroundColor Cyan
}
}
catch {
Write-Error "Failed to create directory for IP $ip : $_"
exit 1
}
# Execute SSH command to get file content
try {
Write-Host "Downloading file: $RemoteFilePath" -ForegroundColor Yellow
$file_content = & $PlinkPath -ssh $ip -l $Username -pw $Password -batch "cat `"$RemoteFilePath`"" 2>&1
# Check if command executed successfully
if ($LASTEXITCODE -ne 0) {
$error_msg = ($file_content -join " ").Trim()
Write-Error "SSH connection or file access failed for $ip : $error_msg"
exit 1
}
# Join content and check if file is empty
$file_content_joined = ($file_content -join "`n").Trim()
if ($file_content_joined -eq "") {
Write-Error "File is empty or does not exist on $ip : $RemoteFilePath"
exit 1
}
# Save file to IP directory
$outputFilePath = Join-Path $ipDirectory $fileName
try {
$file_content_joined | Out-File -FilePath $outputFilePath -Encoding UTF8
Write-Host "✓ Successfully saved file to: $outputFilePath" -ForegroundColor Green
}
catch {
Write-Error "Failed to save file for IP $ip : $_"
exit 1
}
}
catch {
Write-Error "Exception occurred while processing $ip : $_"
exit 1
}
Write-Host ""
}
Write-Host "All files downloaded successfully!" -ForegroundColor Green
Write-Host "Files saved to directory: $OutputDirectory" -ForegroundColor Blue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment