2024-10-01 21:59:47 +02:00
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
2024-10-04 21:13:53 +02:00
|
|
|
|
Login to another host via SSH
|
2024-10-01 21:59:47 +02:00
|
|
|
|
.DESCRIPTION
|
2024-10-04 21:13:53 +02:00
|
|
|
|
This PowerShell script logs into a remote host via secure shell (SSH).
|
2024-10-01 22:10:48 +02:00
|
|
|
|
.PARAMETER remoteHost
|
|
|
|
|
Specifies the remote hostname or IP address
|
2024-10-01 21:59:47 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> ./enter.ps1 linuxhost
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
#>
|
|
|
|
|
|
2024-10-01 22:10:48 +02:00
|
|
|
|
param([string]$remoteHost = "")
|
2024-10-01 21:59:47 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2024-10-01 22:10:48 +02:00
|
|
|
|
if ($remoteHost -eq "") { $remoteHost = Read-Host "Enter the remote hostname" }
|
2024-10-01 21:59:47 +02:00
|
|
|
|
if ($IsLinux) { $username = $(whoami) } else { $username = $env:USERNAME }
|
2024-10-01 22:10:48 +02:00
|
|
|
|
$username = $username.toLower()
|
2024-10-01 21:59:47 +02:00
|
|
|
|
|
2024-10-04 21:13:53 +02:00
|
|
|
|
Write-Host "Trying to enter 💻$remoteHost as user '$username' using " -noNewline
|
2024-10-01 21:59:47 +02:00
|
|
|
|
& ssh -V
|
2024-10-04 21:13:53 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'ssh -V' failed with exit code $lastExitCode" }
|
|
|
|
|
|
2024-10-01 22:10:48 +02:00
|
|
|
|
& ssh "$($username)@$($remoteHost)"
|
2024-10-04 21:13:53 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'ssh -V' failed with exit code $lastExitCode" }
|
2024-10-01 21:59:47 +02:00
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|