Skip to content

Instantly share code, notes, and snippets.

@sdorsett
Last active August 1, 2017 17:40
Show Gist options
  • Save sdorsett/a321d355e288b73302a2b352fc90f74b to your computer and use it in GitHub Desktop.
Save sdorsett/a321d355e288b73302a2b352fc90f74b to your computer and use it in GitHub Desktop.
rbvmomi - find vnic info for all ESXi hosts in all clusters in all datacenters
require 'rbvmomi';
credentials = { :host => "192.168.1.10", :user => "administrator@vsphere.local", :password => "vmware", :insecure => true };
vim = RbVmomi::VIM.connect(credentials);
datacenters = {};
vim.rootFolder.childEntity.each { |datacenter|
datacenter_info = {}
datacenter.hostFolder.childEntity.each { |cluster|
cluster_info = {}
cluster.host.each { |host|
host_info = {}
host.config.network.vnic.each { |vnic|
vnic_info = {}
vnic_info[:portgroup] = vnic.portgroup
vnic_info[:dhcp_enabled] = vnic.spec.ip.dhcp
vnic_info[:ip_address] = vnic.spec.ip.ipAddress
vnic_info[:subnet_mask] = vnic.spec.ip.subnetMask
host_info[vnic.device.to_sym] = vnic_info
}
cluster_info[host.name.to_sym] = host_info
}
datacenter_info[cluster.name.to_sym] = cluster_info
}
datacenters[datacenter.name.to_sym] = datacenter_info
};
datacenters
@sdorsett
Copy link
Author

sdorsett commented Aug 1, 2017

vnic_info.rb will generate a nested hash of datacenters, clusters, hosts & vnics like the following:

[root@vagrant ~] pry
[1] pry(main)> require 'rbvmomi'
=> true
[2] pry(main)> credentials = { :host => "192.168.1.10", :user => "administrator@vsphere.local", :password => "vmware", :insecure => true }
=> {:host=>"192.168.1.10", :user=>"administrator@vsphere.local", :password=>"vmware", :insecure=>true}
[3] pry(main)> vim = RbVmomi::VIM.connect(credentials)
=> VIM(192.168.1.100)
[4] pry(main)> datacenters = {}
=> {}
[5] pry(main)> vim.rootFolder.childEntity.each { |datacenter|
[5] pry(main)*   datacenter_info = {}
[5] pry(main)*   datacenter.hostFolder.childEntity.each { |cluster|
[5] pry(main)*     cluster_info = {}
[5] pry(main)*     cluster.host.each { |host|
[5] pry(main)*       host_info = {}
[5] pry(main)*       host.config.network.vnic.each { |vnic|
[5] pry(main)*         vnic_info = {}
[5] pry(main)*         vnic_info[:portgroup] = vnic.portgroup
[5] pry(main)*         vnic_info[:dhcp_enabled] = vnic.spec.ip.dhcp
[5] pry(main)*         vnic_info[:ip_address] = vnic.spec.ip.ipAddress
[5] pry(main)*         vnic_info[:subnet_mask] = vnic.spec.ip.subnetMask
[5] pry(main)*         host_info[vnic.device.to_sym] = vnic_info
[5] pry(main)*       }
[5] pry(main)*       cluster_info[host.name.to_sym] = host_info
[5] pry(main)*     }
[5] pry(main)*     datacenter_info[cluster.name.to_sym] = cluster_info
[5] pry(main)*   }
[5] pry(main)*   datacenters[datacenter.name.to_sym] = datacenter_info
[5] pry(main)* }
=> [Datacenter("datacenter-2")]
[6] pry(main)> datacenters
=> {:"datacenter-00"=>
  {:"cluster-00"=>
    {:"esx-00"=>{:vmk0=>{:portgroup=>"Management Network", :dhcp_enabled=>false, :ip_address=>"192.168.1.80", :subnet_mask=>"255.255.255.0"}}}}}
[7] pry(main)>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment