{"id":592,"date":"2011-06-03T18:28:06","date_gmt":"2011-06-03T17:28:06","guid":{"rendered":"http:\/\/www.amikkelsen.com\/?p=592"},"modified":"2011-06-03T18:45:39","modified_gmt":"2011-06-03T17:45:39","slug":"list-hosts-cdp-information","status":"publish","type":"post","link":"https:\/\/www.amikkelsen.com\/?p=592","title":{"rendered":"List hosts CDP information"},"content":{"rendered":"<p>If you have your hosts connected to a Cisco network infrastructure, you can see a hosts CDP information directly from within the VI Client.<\/p>\n<p>As you can see in the exampels below it\u00e2\u20ac\u2122s quite impossible to get the full CDP picture if you have many hosts with multiple NICs.<br \/>\nSo I created a script that retrieves all CDP info from all your hosts (even across multiple vCenters) and displays it as a webpage.<br \/>\nNow it\u00e2\u20ac\u2122s possible to search and share the information \ud83d\ude42<\/p>\n<p><a href=\"http:\/\/www.amikkelsen.com\/images\/cdp_web.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"show cdp info \" src=\"..\/images\/cdp_web.jpg\" alt=\"\" width=\"576\" height=\"274\" \/><\/a><\/p>\n<p>The script is build up by a few functions<\/p>\n<ul>\n<li>Retrieve the vCenter servers to retrieve host from.\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$objvCenterServer = Import-Csv -Path $strvCenterFilePath -Delimiter &quot;;&quot; | sort vCenter\r\nforeach($strvCenterServer in $objvCenterServer){\r\n# Check if VC is uncommented\r\nif ((!($strvCenterServer.vCenter.Contains(&quot;#&quot;))) -and ($strvCenterServer.vCenter.Length -gt 0)){\r\n# Connect to vCenter Server\r\n Connect-VIServer -Server $strvCenterServer.vCenter -User $strvCenterServer.UserName -Password $strvCenterPWD\r\n\r\n# Add logic\r\n\r\n# Disconnect from vCenter server\r\nDisConnect-VIServer -Confirm:$false\r\n}}\r\n<\/pre>\n<\/li>\n<li>Retrieve hosts from clusters.\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$arrDC = Get-Datacenter | Sort\r\nforeach($objDC in $arrDC){\r\n $arrCluster = Get-Cluster -Location $objDC | Sort\r\n # Only proceed if the Cluster isn't blank\r\n if ($($arrCluster | Measure-Object).count -gt 0){\r\n foreach($Cluster in $arrCluster){\r\n $vmhosts = Get-VMHost -Location $Cluster | Sort Name | Where-Object {$_.State -eq &quot;Connected&quot;} | Get-View\r\n #Only proceed if any hosts in cluster\r\n if ($vmhosts.Count -gt 0){\r\n foreach ($vmhost in $vmhosts){\r\n\r\n# Add logic for each host\r\n\r\n}}}}}\r\n<\/pre>\n<\/li>\n<li>Retrieve hosts NICs CDP info.\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nforeach ($vmhost in $vmhosts){\r\n $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem\r\n\r\n foreach($pnic in $networkSystem.NetworkConfig.Pnic | Sort Device){\r\n $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)\r\n\r\n foreach($Hint in $pnicInfo){\r\n\r\n # LinkSpeed &amp; MAC\r\n $record = 0\r\n $tmpSpeed = &quot;&quot;\r\n $tmpMAC = &quot;&quot;\r\n Do{\r\n If ($Hint.Device -eq $vmhost.Config.Network.Pnic&#x5B;$record].Device){\r\n $tmpSpeed = $vmhost.Config.Network.Pnic&#x5B;$record].LinkSpeed.SpeedMb\r\n $tmpMAC = $vmhost.Config.Network.Pnic&#x5B;$record].Mac\r\n }\r\n $record ++\r\n }\r\n Until ($record -eq ($vmhost.Config.Network.Pnic.Length))\r\n\r\n # Duplex\r\n $tmpDuplex = &quot;&quot;\r\n if($Hint.ConnectedSwitchPort.FullDuplex -eq $true){\r\n $tmpDuplex = &quot;Full&quot;\r\n }\r\n if($Hint.ConnectedSwitchPort.FullDuplex -eq $false){\r\n $tmpDuplex = &quot;Half&quot;\r\n }\r\n\r\n # Status\r\n $tmpStatus = &quot;&quot;\r\n If (($tmpSpeed -ge 1000) -and ($tmpDuplex -eq &quot;Full&quot;)){\r\n $tmpStatus = &quot;OK&quot;\r\n }else{\r\n If (($tmpSpeed -gt 0) -and ($tmpDuplex -eq &quot;&quot;)){\r\n $tmpStatus = &quot;CDP not working&quot;\r\n }\r\n elseif (($tmpSpeed -gt 0) -and ($tmpDuplex -eq &quot;Half&quot;)){\r\n if ($tmpStatus -ne &quot;&quot;){$tmpStatus += &quot; \/ &quot;}\r\n $tmpStatus = &quot;Duplex config error&quot;\r\n }\r\n elseif (($tmpSpeed -gt 0) -and ($tmpSpeed -lt 1000)){\r\n if ($tmpStatus -ne &quot;&quot;){$tmpStatus += &quot; \/ &quot;}\r\n $tmpStatus = &quot;Speed config error&quot;\r\n }\r\n else{\r\n $tmpStatus = &quot;Link Down&quot;\r\n }}}}\r\n<\/pre>\n<\/li>\n<li>Seperate information included\/excluded.\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$isExcluded = func_exclude_from_list -ExcludeHost $vmhost.Name -ExcludeVMNIC $Hint.Device -FromObj $objExcludeList\r\n<\/pre>\n<\/li>\n<li>Generate html outputfile.\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nAdd-Content -Path $($strOutputPath + $strOutputFileName + &quot;.TMP&quot;) -Value $strHTML\r\nif(Test-Path -Path $($strOutputPath + $strOutputFileName + &quot;.htm&quot;)){\r\n Copy-Item -Path $($strOutputPath + $strOutputFileName + &quot;.htm&quot;) -Destination $($strOutputPath + $strOutputFileName + $(Get-Date -uformat &quot;%Y%m%d&quot;) + &quot;.htm&quot;) -Force\r\n}\r\nCopy-Item -Path $($strOutputPath + $strOutputFileName + &quot;.TMP&quot;) -Destination $($strOutputPath + $strOutputFileName + &quot;.htm&quot;) -Force\r\nRemove-Item -Path $($strOutputPath + $strOutputFileName + &quot;.TMP&quot;) -Force\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>vCenter servers are added\/removed from the file &#8220;_All_vCenter_Hosts_.csv&#8221; .<br \/>\nA hosts NICs are moved to the exclude section, by adding it to the host exclude list &#8220;host_CDP_exclude.csv&#8221;.<\/p>\n<p><a href=\"..\/images\/cdp_web_excluded.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"show cdp info exclude\" src=\"..\/images\/cdp_web_excluded.jpg\" alt=\"\" width=\"546\" height=\"134\" \/><\/a><\/p>\n<p>The CDP information is sorted and saved in a HTML output file. If the script has been run before and an older version of the output file exist, it\u00e2\u20ac\u2122s renamed and the new is saved (This supply you with a sort of history).<\/p>\n<p>Get all script files here.<br \/>\n<a title=\"cdp web\" href=\"http:\/\/www.amikkelsen.com\/?dl_id=29\">_All_vCenter_Hosts_.csv<br \/>\nhost_CDP_exclude.csv<br \/>\nhost_cdp_info_v01.ps1<\/a><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>For CDP information to be visible\u00c2\u00a0 in the VI client, CDP must also be enabled\/configured on your physical Cisco switch \u00e2\u20ac\u201c see example.<br \/>\n<a href=\"http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1003885\">http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1003885<\/a><\/p>\n<p>To enable\/change the CDP information on a host you have several options.<\/p>\n<ol>\n<li>If you are using dvSwitches in vSphere 4.x you can enable it from GUI .<br \/>\n<a href=\"http:\/\/www.amikkelsen.com\/images\/cdp_gui.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"enable CDP gui\" src=\"http:\/\/www.amikkelsen.com\/images\/cdp_gui.jpg\" alt=\"\" width=\"423\" height=\"328\" \/><\/a><\/p>\n<ul>\n<li>Connect to vCenter using VI Client<\/li>\n<li>From <strong>Home<\/strong> click <strong>Networking<\/strong><\/li>\n<li>Right click your dvSwitch and select<strong> Edit Settings<\/strong><\/li>\n<li>Under the <strong>Properties<\/strong> tab select <strong>Advanced<\/strong><\/li>\n<li>Check <strong>Cisco Discovery Protocol<\/strong><\/li>\n<li>Set <strong>Operation<\/strong> to <strong>Both<\/strong><\/li>\n<li>Click <strong>OK<\/strong><\/li>\n<\/ul>\n<\/li>\n<li> Using ESX Command line<br \/>\n<a href=\"http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1003885\">http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1003885<\/a><br \/>\n(exchange vSwitch1 with the name of your vSwitch)<\/p>\n<ul>\n<li>Show current setting\n<pre>esxcfg-vswitch -b vSwitch1<\/pre>\n<\/li>\n<li>Set the new status (down, listen, advertise, both)\n<pre>esxcfg-vswitch -B both vSwitch1<\/pre>\n<\/li>\n<li>Verify new settings\n<pre>esxcfg-vswitch -b vSwitch1<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Using vMA<br \/>\n<a href=\"http:\/\/spininfo.homelinux.com\/news\/vSphere_PowerCLI\/2010\/03\/24\/Enable_CDP_on_vSwitch_on_all_hosts\">http:\/\/spininfo.homelinux.com\/news\/vSphere_PowerCLI\/2010\/03\/24\/Enable_CDP_on_vSwitch_on_all_hosts<\/a><\/p>\n<pre> vicfg-vswitch --server &lt;vcenter.domain.com&gt; -h &lt;esxi.domain.com&gt; -B both &lt;vSwitch1&gt;<\/pre>\n<\/li>\n<\/ol>\n<p>To view your CDP info you also a few options<br \/>\nSee all options here:<br \/>\n<a href=\"http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1007069\">http:\/\/kb.vmware.com\/selfservice\/microsites\/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1007069<\/a><\/p>\n<ol>\n<li>GUI<br \/>\n<a href=\"http:\/\/www.amikkelsen.com\/images\/show_cdp.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"show cdp info gui\" src=\"http:\/\/www.amikkelsen.com\/images\/show_cdp.jpg\" alt=\"\" width=\"250\" height=\"238\" \/><\/a><\/p>\n<ul>\n<li>Connect to vCenter or ESX using VI Client<\/li>\n<li>Select a ESX host<\/li>\n<li>Click the Configuration tab<\/li>\n<li>Select Networking<\/li>\n<li>Click the Info icon to the right of the vSwitch<\/li>\n<li>A tool tip opens with CDP information for the selected physical network interface<\/li>\n<\/ul>\n<\/li>\n<li>PowerShell\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n Get-VMHost | Where-Object {$_.State -eq &quot;Connected&quot;} |\r\n %{Get-View $_.ID} |\r\n %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |\r\n %{ foreach($physnic in $_.NetworkInfo.Pnic){\r\n    $pnicInfo = $_.QueryNetworkHint($physnic.Device)\r\n    foreach($hint in $pnicInfo){\r\n       Write-Host $esxname $physnic.Device\r\n       if( $hint.ConnectedSwitchPort ) {\r\n          $hint.ConnectedSwitchPort\r\n       }\r\n       else {\r\n          Write-Host &quot;No CDP information available.&quot;; Write-Host\r\n       }\r\n    }\r\n }\r\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If you have your hosts connected to a Cisco network infrastructure, you can see a hosts CDP information directly from within the VI Client. As you can see in the exampels below it\u00e2\u20ac\u2122s quite impossible to get the full CDP picture if you have many hosts with multiple NICs. So I created a script that\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.amikkelsen.com\/?p=592\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295,224,21],"tags":[293,138,330,294],"class_list":["post-592","post","type-post","status-publish","format-standard","hentry","category-networking","category-powercli-scripts","category-powershell","tag-cdp","tag-powercli","tag-powershell","tag-web"],"_links":{"self":[{"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=592"}],"version-history":[{"count":36,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}