Find all ESXi hosts in a subnet

I have got a task to clear subnets off non-VMware hosts so we can turn on dhcp, PXE for Auto Deploy. I have identified inactive IPs and our DNS admin reclaimed them. I need a way to verify the active IPs are VMware ESXi.

My first method is to ssh to the IPs and run vmware -v in a for loop.

for i in `cat list`
do 
    ssh -q -o "BatchMode yes" root@$i vmware -v
done

We have ssh keys setup but for every subnet there are a bunch of hosts where I cannot ssh with keys. I do not have the time to fix them all.

Next I try nmap -sV -p 80,443 192.168.1.0/24 -oA vmware_scan which isn’t working or takes a long time.

Then I find this from the community forum which makes use of nc to scan port 902.

net='192.168.1.'
for i in $(seq 255)
do
  echo | nc -nvw1 $net$i 902 | if grep -iq "VMware"
  then
    echo "$net$i looks like an ESX(i) host."
  else
    echo "$net$i does not look like an ESX(i) host."
  fi
done

When you scan for port 902 against a valid ESXi host

nc -vw1 myesxihost 902
Connection to myesxihost 902 port [tcp/ideafarm-door] succeeded!
220 VMware Authentication Daemon Version 1.10: SSL Required, ServerDaemonProtocol:SOAP, MKSDisplayProtocol:VNC , VMXARGS supported, NFCSSL supported

I already have a list of active hostnames in a file. I change the script ot

for i in $( cat list)
do
  echo | nc -vw1 $i 902 | if grep -iq "VMware"
  then
    echo "$i looks like an ESX(i) host."
  else
    echo "$i does not look like an ESX(i) host."
  fi
done

What I really need is just those hosts which refuses connection on port 902. Hence

for i in $( cat list); do   echo | nc -vw1 $i 902 | grep refused; done
nc: connect to 172.20.130.0 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.1 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.2 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.3 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.43 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.44 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.45 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.46 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.47 port 902 (tcp) failed: Connection refused
nc: connect to 172.20.130.48 port 902 (tcp) failed: Connection refused
...
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s