I’ve been looking for a script that pulls the Distributed routing table from each host in the DC.
I’ve considered Python initially to finish the task but due to the compatibility between power-CLI and vSphere ecosystem and after about 3 hours of skimming through Docs and blogs, refrained from python and ended with the PowerCLI.
This is tested on
Powercli – 6.5
ESXi – 5.5
vCenter- 5.5
This script has two steps.
1> To collect the host information from the VC
#Connect to the VC. Connect-VIServer -Server 192.168.110.22 -Protocol https -User 'administrator@vsphere.local' -Password 'VMware1!' #Print the MGMT IP, in this case, I've considered vmk0 as the MGMT Kernal interface: Get-VMHostNetworkAdapter | Where-Object {$_.Name -eq "vmk0"} #Saving the output of the above command to a directory: Get-VMHostNetworkAdapter | Where-Object {$_.Name -eq "vmk0"} > F:\scripts\mgmtinterfaces.txt #Filter the IP from the above Output Get-Content .\mgmtinterfaces.txt | %{ $_.Split(' ')[15]; } #Saves the IPs to a text file. Get-Content .\mgmtinterfaces.txt | %{ $_.Split(' ')[15]; } > F:\scripts\mgmtips.txt
2>To log-on to the ESXi host and run the command “net-vdr -l -R default+edge-11”
to get the routing table info from each ESXi host. Considering the VDR instance name is default+edge-11, you may have to edit this accordingly.
This script enables SSH to each host and disables it after collecting the information.
#Disclaimer: Please test this script in lab environment before testing on any production system. Start-Transcript F:\scripts\logging.txt $root = "root" $Passwd = "VMware1!" $esxlist = Get-Content F:\scripts\mgmtips.txt $cmd = "net-vdr -l -R default+edge-11" #Download plink from http://the.earth.li/~sgtatham/putty/0.63/x86/ $plink = "echo y | C:\Users\administrator\Downloads\plink.exe" $remoteCommand = '"' + $cmd + '"' foreach ($esx in $esxlist) { Connect-VIServer $esx -User $root -Password $Passwd Write-Host -Object "starting ssh services on $esx" -ForegroundColor Green $sshstatus= Get-VMHostService -VMHost $esx| where {$psitem.key -eq "tsm-ssh"} if ($sshstatus.Running -eq $False) { Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService } Write-Host -Object "Executing Command on $esx" -ForegroundColor Green $output = $plink + " " + "-ssh" + " " + $root + "@" + $esx + " " + "-pw" + " " + $Passwd + " " + $remoteCommand $message = Invoke-Expression -command $output $message } foreach ($esx in $esxlist) { Connect-VIServer $esx -User $root -Password $Passwd Write-Host -Object "Stopping ssh services on $esx" -ForegroundColor Green $sshstatus= Get-VMHostService -VMHost $esx| where {$psitem.key -eq "tsm-ssh"} if ($sshstatus.Running -eq $True) { Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Stop-VMHostService } } Stop-Transcript Write-Host "Collected the route information from $esxlist and saved it at F:\scripts\logging.txt" -ForegroundColor Yellow
Executing Script-1, sample output:
PS F:\scripts> .\format_hostname.ps1 Name Port User ---- ---- ---- 192.168.110.22 443 VSPHERE.LOCAL\Administrator #ESXi hosts connected to corresponding VC 192.168.210.52 192.168.210.53 192.168.110.51 192.168.110.52 192.168.210.56 192.168.210.57
Executing Script-2, sample output:
PS F:\scripts> .\esxi_1.ps1 #Executing the script sequentially starting with host 192.168.210.52 Name Port User ---- ---- ---- 192.168.210.52 443 root starting ssh services on 192.168.210.52 Executing Command on 192.168.210.52 VDR default+edge-11 Route Table Legend: [U: Up], [G: Gateway], [C: Connected], [I: Interface] Legend: [H: Host], [F: Soft Flush] [!: Reject] [E: ECMP] Destination GenMask Gateway Flags Ref Origin UpTime Interface ----------- ------- ------- ----- --- ------ ------ --------- 1.1.1.0 255.255.255.252 0.0.0.0 UCI 1 MANUAL 32867 27100000000a 172.16.1.0 255.255.255.0 0.0.0.0 UCI 1 MANUAL 32867 271000000002 172.16.10.0 255.255.255.0 0.0.0.0 UCI 1 MANUAL 32867 27100000000c 172.16.20.0 255.255.255.0 0.0.0.0 UCI 1 MANUAL 32867 27100000000b ---Output Omitted for rest of the hosts---
192.168.210.52 443 root Stopping ssh services on 192.168.210.52 Perform operation? Perform operation Stop host service. on SSH? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y SSH Service stopped.
Scripts are saved at: fetch_command_output.ps1
https://github.com/anudeep404/vmware_automation
Hope this is informative and please feel free to comment.
Regards,
Anudeep