NSX Automation Part 3 – Create CSV from Existing Logical Switches Partial Data
In part 2 of this series, we looked at how to extract all the information for the Logical Switches from NSX manager. There are some instances where we might want to extract certain fields. This post shows how to get certain fields for the Logical Switches from the NSX manager and put it in a CSV file. We will massage that file in later posts for other uses.
Items required:
- curl
- xmllint
- python
- python setuptools
- xmlutils.py
The Fields I think are most useful. This is up to you:
- //virtualWire/objectId = Logical Switch Id or virtualWireId <— Must have in the fields set
- //virtualWire/name = Logical Switch name
- //virtualWire/description = Logical Switch description
- //virtualWire/vdnId = Logical Switch segment Id or number
- //virtualWire/vdnScopeId = The Transport Zone Id that this Logical Switch belongs to
- //virtualWire/vdsContextWithBacking/mtu = The Logical Switch MTU
- //virtualWire/vdsContextWithBacking/backingValue = The vDS port-group that represents this Logical Switch
The Command, replace “Fields1…” with the desired fields from above separated by “|”:
ndaher@ubuntu:~$ curl -k -u admin:Password! -H 'content-type: application/xml'-X GET https://nsxman.local/api/2.0/vdn/virtualwires | xmllint --xpath "Filed1|Field2|Field3" - | sed -e 's/<objectId>/<virtualWire><objectId>/' -e 's/<\/objectId>/<\/objectId><\/virtualWire>/' | sed -e 's/^/<virtualWires>/' -e 's/$/<\/virtualWires>/'>logswitches.xml
ndaher@ubuntu:~$ xml2csv --input "logswitches.xml" --output "logswitches.out" --tag "virtualWire"
ndaher@ubuntu:~$ sed s/\"//g logswitches.out > logswitches.csv
Permanent link to this article: https://daherlabs.mywire.org/wordpress/?p=469