Updated enter-host.ps1 and ping-host.ps1

This commit is contained in:
Markus Fleschutz 2024-11-04 15:25:02 +01:00
parent e09c53bcc5
commit 70ebba06ff
2 changed files with 13 additions and 14 deletions

View File

@ -7,7 +7,8 @@
Specifies the remote hostname or IP address Specifies the remote hostname or IP address
.EXAMPLE .EXAMPLE
PS> ./enter-host.ps1 tux PS> ./enter-host.ps1 tux
Connecting to 'tux' as user 'markus' using OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2 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: markus@tux's password:
... ...
.LINK .LINK
@ -22,7 +23,6 @@ try {
if ($remoteHost -eq "") { if ($remoteHost -eq "") {
$remoteHost = Read-Host "Enter the remote hostname or IP address" $remoteHost = Read-Host "Enter the remote hostname or IP address"
$remoteUser = Read-Host "Enter the username at $remoteHost" $remoteUser = Read-Host "Enter the username at $remoteHost"
} elseif ($IsLinux) { } elseif ($IsLinux) {
$remoteUser = $(whoami) $remoteUser = $(whoami)
} else { } else {
@ -30,7 +30,10 @@ try {
$remoteUser = $remoteUser.toLower() $remoteUser = $remoteUser.toLower()
} }
Write-Host "⏳ Connecting to '$remoteHost' as user '$remoteUser' using " -noNewline & "$PSScriptRoot/ping-host.ps1" $remoteHost
if ($lastExitCode -ne "0") { exit 1 }
Write-Host "⏳ Connecting as user '$remoteUser' using " -noNewline
& ssh -V & ssh -V
if ($lastExitCode -ne "0") { throw "'ssh -V' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'ssh -V' failed with exit code $lastExitCode" }

View File

@ -7,7 +7,7 @@
Specifies the hostname or IP address to ping (windows.com by default) Specifies the hostname or IP address to ping (windows.com by default)
.EXAMPLE .EXAMPLE
PS> ./ping-host.ps1 x.com PS> ./ping-host.ps1 x.com
x.com is up with 10ms latency. x.com is up and running (11ms latency).
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -18,23 +18,19 @@ param([string]$hostname = "windows.com")
function GetPingLatency([string]$hostname) { function GetPingLatency([string]$hostname) {
$hostsArray = $hostname.Split(",") $hostsArray = $hostname.Split(",")
$tasks = $hostsArray | foreach { $tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500) }
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500)
}
[Threading.Tasks.Task]::WaitAll($tasks) [Threading.Tasks.Task]::WaitAll($tasks)
foreach($ping in $tasks.Result) { foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
if ($ping.Status -eq "Success") { return $ping.RoundtripTime }
}
return 1500 return 1500
} }
try { try {
[int]$latency = GetPingLatency($hostname) [int]$latency = GetPingLatency($hostname)
if ($latency -eq 1500) { if ($latency -eq 1500) {
Write-Host "⚠️ Host '$hostname' is offline" Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or wake the host up."
} else { exit 1
Write-Host "$hostname is up with $($latency)ms ping latency." }
} Write-Host "$hostname is up and running ($($latency)ms latency)."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"