Check ESXi to see if sshd is listening

In a recent service request with VMware, Tech Support enabled ssh on one of my ESXi hosts. Unfortunately, the outage that prompted my support request impacted multiple hosts in different clusters, so we were looking at many things in the entire environment. I didn’t write down the name of the host where we enabled SSH, or even the cluster it was in. The following script helped me identify which one I was looking for – I’m sure there are better ways to check this, but I was looking for something quick which prompted this combination of existing scripts:

[cc lang=”powershell”]
$VMH = Get-VMHost | Sort Name

#If the port is closed/filtered PowerShell will return a warning. This will suppress:
$ErrorActionPreference=”SilentlyContinue”

#The port I want to check for SSHD
$portnum =22

#The loop to check all hosts in a vCenter
$VMH | %{
#port check sample from http://halr9000.com/article/418
$socket = new-object Net.Sockets.TcpClient
$socket.Connect($_.name, $portnum)
if ($socket.Connected) {
$status = “Open”
$socket.Close()
}
else {
$status = “Closed / Filtered”
}
$socket = $null
write-output “$_`t$portnum`t$status”
}
[/cc]

This pointed out which of my hosts (it was only 1) that had SSH running.

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.