Skip to content

Instantly share code, notes, and snippets.

@jbarber
Created April 4, 2011 11:20
Show Gist options
  • Save jbarber/901485 to your computer and use it in GitHub Desktop.
Save jbarber/901485 to your computer and use it in GitHub Desktop.
Example of using vijava API from clojure to list all hostnames/IPs for those VMs that VMware knows the IP of
; Return a list of vectors of hostname/ip address for VMware virtual machines from vCenter
; Export the environment variables:
; VI_USERNAME - your vCenter username
; VI_PASSWORD - your vCenter password
; VI_SERVER - the vCenter hostname/IP
; Invoke with the command line:
; java -cp clojure-1.2.0/clojure.jar:vijava2120100715.jar:dom4j-1.6.1.jar clojure.main hosts.clj
(import '(com.vmware.vim25.mo ServiceInstance InventoryNavigator))
(def si (ServiceInstance.
(java.net.URL. (str "https://" (System/getenv "VI_SERVER") "/sdk"))
(System/getenv "VI_USERNAME")
(System/getenv "VI_PASSWORD")
true
))
(defn get-ips [vms]
"Return sequence of vectors of VM [ hostname IP ]"
(letfn
[(get-ip [vm] (.. vm getGuest ipAddress))
(get-name [vm] (.. vm getName))
(get-data [vm] (vector (get-name vm) (get-ip vm)))]
(map get-data (filter get-ip vms))))
(let [inv (new InventoryNavigator (. si getRootFolder))
vms (. inv searchManagedEntities "VirtualMachine")]
(get-ips vms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment