Skip to content

Instantly share code, notes, and snippets.

@michele-tn
Last active August 5, 2024 04:58
Show Gist options
  • Save michele-tn/9afa8a91582b238bfdb009954c98b7b2 to your computer and use it in GitHub Desktop.
Save michele-tn/9afa8a91582b238bfdb009954c98b7b2 to your computer and use it in GitHub Desktop.
(MULTIPLE TCP TUNNELING) Connecting and Loading SSH private keys automatically on plink
# Run as administrator and stays in the current directory
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
Start-Process PowerShell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
}
}
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
# Convert from Base64 to String
$ppk_Path = ".\PrivKey.ppk"
$YouIp_SSH_Server = 'YOUR_IP_SSH_SERVER'
$YouPort_SSH_Server = 'YOUR_PORT_SSH_SERVER'
$Base64_Code = "YourBase64Code==" #BASE64 PPK KEY..
[byte[]]$Bytes = [convert]::FromBase64String($Base64_Code)
[System.IO.File]::WriteAllBytes($ppk_Path,$Bytes)
$downloadUri = 'https://the.earth.li/~sgtatham/putty/latest/w64/plink.exe'
$FileName = 'plink.exe'
try{
Invoke-WebRequest -Uri $downloadUri -Out $FileName #-ErrorAction Stop
# This will only execute if the Invoke-WebRequest is successful.
}
catch{
Write-Host $_.Exception.Response.StatusCode.value__
Write-Host $_.ErrorDetails
}
try{
Start-Process .\plink.exe -Verb RunAs -PassThru -ArgumentList "-ssh root@$YouIp_SSH_Server -P $YouPort_SSH_Server -i $ppk_Path -R localhost:7642:localhost:22 -R localhost:6689:localhost:3389 -2 -N -noshare -noagent -C"
}
catch{
Write-Host $_.Exception.Response.StatusCode.value__
Write-Host $_.ErrorDetails
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment