VMware Cloud Community
meistermn
Expert
Expert

How to get CDP values from Cisco Switch?

Since ESX 3.5 now has CDP implemented, how can we get the physical switch name and the switch port, like it is in VC 2.5 showed?

13 Replies
LucD
Leadership
Leadership

Only via the SDK I'm afraid.

The CDP information is returned in the PhysicalNicCdpInfo object.

This object can be retrieved with the QueryNetworkHint method.

Something like this should give you the CDP information for each pNIC on each ESX server under the VC.

Get-VMHost | %{Get-View $_.ID} | %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | %{
  foreach($physnic in $_.NetworkInfo.Pnic){
    $pnicInfo = $_.QueryNetworkHint($physnic.Device)
    foreach($hint in $pnicInfo){
      Write-Host $esxname $physnic.Device $hint.connectedSwitchPort
    }
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
lamw
Community Manager
Community Manager

If you enable CDP on the vSwitches on your ESX 3.5.x systems, you can actually get this info via the commandline using "esxcfg-info | grep cdp", this information is listed in the network section contained in XML like tags, but you'll get all the info you see on the Virtual Center GUI. As in the previous post, you can retrieve this info via the toolkit, but if you want to manage one less component you can still get it through the traditional esx commands.

Reply
0 Kudos
jasonah
Contributor
Contributor

I tried that script. Instead of the cdp info I am getting this... VMware.Vim.PhysicalNicCdpInfo on all of my nics that are cdp enabled. Any thoughts?

Thanks,

Jason

Reply
0 Kudos
jasonah
Contributor
Contributor

I figured it out. I changed the write-host line to write-output and then it showed me the cdp info.

Thanks,

Jason

Reply
0 Kudos
DennieTidwell
Enthusiast
Enthusiast

From ESX server

esxcfg-info | grep -C 18 "\==+CDP Summary"

Reply
0 Kudos
Milk_Man
Contributor
Contributor

I am not able to get this to work. When i execute the code, I receive this error. Any ideas?

Unexpected token 'foreach' in expression or statement.

At line:1 char:200

+ Get-VMHost | %{Get-View $_.ID} | %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | %{ foreach($physnic in $_.NetworkInfo.Pnic){ $pnicInfo = $_.QueryNetworkHint($physnic.Device) foreach( <<<< $hint in $pnicInfo){ Write-Host $esxname $physnic.Device $hint.connectedSwitchPort } } }

Reply
0 Kudos
LucD
Leadership
Leadership

It seems you lost the newlines when copying the script code.

Afaik this only seems to work correctly with FF.

See also (and many other posts) Smiley Sad


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Milk_Man
Contributor
Contributor

That was the problem. Thanks! I was able to save the webpage from IE7 and open it in Word 2007 and it did reflect the correct formatting.

Reply
0 Kudos
arturka
Expert
Expert

Hi all

How can I export specifi fields to CSV file ? I would like to have an report with similar layout:

Hostname | vmnic | DeviceID | PortID

Thanks in advance

Artur

VCDX77 My blog - http://vmwaremine.com
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Hi Artur,

I did a little rewrite of Luc's script to present the requested information in a .csv file:

Get-VMHost | Get-View | ForEach-Object {
  $VMHost = $_
  Get-View $VMHost.ConfigManager.NetworkSystem
} | ForEach-Object {
  foreach ($physnic in $_.NetworkInfo.Pnic) {
    $pnicInfo = $_.QueryNetworkHint($physnic.Device)
    foreach($hint in $pnicInfo){
      $Report = "" | Select-Object -Property VMHost,vmnic,DeviceId,PortId
      $Report.VMHost = $VMHost.Name
      $Report.vmnic = $physnic.Device
      $Report.DeviceId = $hint.connectedSwitchPort.DevId
      $Report.PortId = $hint.connectedSwitchPort.PortId
      $Report
    }
  }
} | Sort-Object -Property VMHost,vmnic | `
Export-Csv -Path VMHostSwitchPorts.csv -NoTypeInformation -UseCulture

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
arturka
Expert
Expert

Hi Robert,

It works perfectly

Thnak you very much

Artur

VCDX77 My blog - http://vmwaremine.com
Reply
0 Kudos
jeffwitt41
Enthusiast
Enthusiast

Robert,

This works great. Thanks!

Regards,

Jeff

Reply
0 Kudos
esxi1979
Expert
Expert

Reply
0 Kudos