When you don’t use something for a while, you tend to forget how valuable it can be.
Even from its very early iterations, ESXi has been able to display CDP information so you can quickly establish “what’s patched where “. Easy to get from the gui, but far more useful from the shell or cli. This has been valuable to me many times (mostly in remote data centers), and is something I make sure is in my scripts/quicktips cookbook.
The easiest way to see this is directly from the host. You could either ssh in or do it from the dcui using the vim-cmd command and the query_networkhint option;
1 |
vim-cmd hostsvc/net/query_networkhint |
Obviously that’s a very verbose output, so to zero in on the actual information use grep with a context setting of 2. This will isolate the output to include the switch hostname devId, The switch IP address and the portId.
1 |
vim-cmd hostsvc/net/query_networkhint | grep 'devId' -C 2 |
..and of course this can be done remotely from PowerCLI. There’s numerous ways to achieve this, VMware have been kind enough to provide an example here, so I have tweaked this to suit my own requirements, by adding parameters and stripping out checks and data I’m not interested in.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<# Script to retrieve specific CDP Info www.pragmaticio.com Usage: Include Cluster, VMhost or VirtualCenter name in execution example: To connect to a cluster called "Production" -- .\get-cdp.ps1 -Cluster Production example: To connect to a VirtualCenter Server called "ProductionVC" -- .\get-cdp.ps1 -VCServer ProductionVC example: To connect to a host called host1.local -- .\get-cdp.ps1 -VMhost host1.local Add/remove Info as required #> param( [string]$VMhost, [string]$Cluster, [string]$VCServer ) If ($VMhost){$vmh = Get-VMHost -Name $VMhost} If ($Cluster){$vmh = Get-Cluster $Cluster | Get-VMHost} If ($VCServer){$vmh = Get-VMhost -Server $VCServer } $vmh | % {Get-View $_.ID} | ` % { $esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | ` % { foreach ($physnic in $_.NetworkInfo.Pnic) { $pnicInfo = $_.QueryNetworkHint($physnic.Device) foreach( $hint in $pnicInfo ){ if ( $hint.ConnectedSwitchPort ) { $hint.ConnectedSwitchPort | select @{n="VMHost";e={$esxname}},@{n="VMNic";e={$physnic.Device}},DevId,Address,PortId,vLan } } } } |
Because it uses parameters, and because it’s based on pure PowerShell, it’s extensible and flexible, so you can retrieve info for a single host, all hosts in a VC server or all hosts in a Cluster, by specifying the -VMhost, -Cluster or -VCServer parameter in the execution.
Here’s the results;
More data can be included by adding in other available options: CdpVersion, Ttl, Samples, SoftwareVersion, HardwarePlatform, FullDuplex, Mtu etc etc. It all depends on what data is important to you.
Pipe it through out-file and you have a quick bit of connection documentation, and an easy reference.
Works like charm. Thank you.
Works like charm. Thank you.