mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-24 17:03:45 +01:00
98 lines
2.4 KiB
Markdown
98 lines
2.4 KiB
Markdown
The *enter-host.ps1* Script
|
|
===========================
|
|
|
|
This PowerShell script logs into a remote host via secure shell (SSH).
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/enter-host.ps1 [[-remoteHost] <String>] [<CommonParameters>]
|
|
|
|
-remoteHost <String>
|
|
Specifies the remote hostname or IP address
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
[<CommonParameters>]
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
```
|
|
|
|
Example
|
|
-------
|
|
```powershell
|
|
PS> ./enter-host.ps1 tux
|
|
✅ tux is up and running (3ms latency).
|
|
⏳ Connecting as user 'markus' using OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2
|
|
markus@tux's password:
|
|
...
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Enter another host via SSH
|
|
.DESCRIPTION
|
|
This PowerShell script logs into a remote host via secure shell (SSH).
|
|
.PARAMETER remoteHost
|
|
Specifies the remote hostname or IP address
|
|
.EXAMPLE
|
|
PS> ./enter-host.ps1 tux
|
|
✅ tux is up and running (3ms latency).
|
|
⏳ Connecting as user 'markus' using OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2
|
|
markus@tux's password:
|
|
...
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
param([string]$remoteHost = "")
|
|
|
|
try {
|
|
if ($remoteHost -eq "") {
|
|
$remoteHost = Read-Host "Enter the remote hostname or IP address"
|
|
$remoteUser = Read-Host "Enter the username at $remoteHost"
|
|
} elseif ($IsLinux) {
|
|
$remoteUser = $(whoami)
|
|
} else {
|
|
$remoteUser = $env:USERNAME
|
|
$remoteUser = $remoteUser.toLower()
|
|
}
|
|
|
|
& "$PSScriptRoot/ping-host.ps1" $remoteHost
|
|
if ($lastExitCode -ne "0") {
|
|
Write-Host "Let's try to wake '$remoteHost' up..."
|
|
& "$PSScriptRoot/wake-up-host.ps1"
|
|
}
|
|
|
|
Write-Host "⏳ Connecting as user '$remoteUser' using " -noNewline
|
|
& ssh -V
|
|
if ($lastExitCode -ne "0") { throw "'ssh -V' failed with exit code $lastExitCode" }
|
|
|
|
& ssh "$($remoteUser)@$($remoteHost)"
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:17)*
|