I wanted get a list of port groups and their type (kernel, console, virtual machine) from a series of hosts, however the only thing I could find that was even close was a POSH script in the VMTN forums that was posted by LucD.
Using that script for inspiration, I essentially duplicated the functionality, but using the perl toolkit. This script gives me an easy to read (and parse…) list of portgroups, the vSwitch they belong to, and the type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#!/usr/bin/perl -w # # esx-portgroup-cfg.pl - written by Andrew Sullivan, 2009-04-07 # # Please report bugs and request improvements at http://get-admin.com/blog/?p=629 # # A perl replication of LucD's outstanding script to print the portgroups defined # on a host and their type. LucD's original can be found here: # http://communities.vmware.com/message/1029208 # # Examples: # Show portgroups for a host # ./esx-portgroup-cfg.pl --server your.esx.host # # Show portgroups for a host, connecting through vCenter # ./esx-portgroup-cfg.pl --server your.vCenter.server --vihost your.esx.host # use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../"; use VMware::VIRuntime; my %opts = ( 'vihost' => { type => "=s", help => "The name of the ESX host the vSwitch is on, not required if connecting " . "directly to the ESX host", required => 0 } ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my $vihost; my $host; if (Opts::option_is_set('vihost') && Opts::get_option('vihost') ne "") { $vihost = Opts::get_option('vihost'); } else { $vihost = Opts::get_option('server'); } eval { $host = Vim::find_entity_view( view_type => 'HostSystem', filter => { 'name' => qr/($vihost)/i } ); }; if ($@) { Util::trace(0, "Host not found or host name ambiguous"); Util::trace(0, $@); Util::disconnect; exit; } my $networkSystem = Vim::get_view(mo_ref => $host->configManager->networkSystem); # a holder for the kernel and console portgroup names my %portgroups = (); # gather the kernel and console portgroups so we can differentiate later foreach (@{$networkSystem->networkInfo->consoleVnic}) { $portgroups{ $_->portgroup } = "Console"; } foreach (@{$networkSystem->networkInfo->vnic}) { $portgroups{ $_->portgroup } = "Kernel"; } # loop through the full list of portgroups, but check for the console # and kernel groups so that they can be stated as suchi print pad_str("vSwitch Name", 20) . " " . pad_str("PG Name", 20) . " Type (Ports in Use) n"; print "-" x 20 . " " . "-" x 20 . " " . "-" x 20 . "n"; foreach (@{$networkSystem->networkInfo->portgroup}) { print pad_str($_->spec->vswitchName, 20) . " " . pad_str($_->spec->name, 20) . " "; if ($portgroups{ $_->spec->name }) { # Uncomment to show the "raw" type #print @{$_->port}[0]->type; print $portgroups{ $_->spec->name }; } else { print "Virtual Machine"; if ($_->port) { print " (" . scalar(@{$_->port}) . ")"; } } print "n"; } Util::disconnect(); sub pad_str { my $string = shift; my $length = shift; my $cur_len = length($string); if ($cur_len >= $length) { $string = substr($string, 0, $length - 3); $string = $string . "..."; } return $string . " " x ( $length - length($string) ); } |
Thanks to LucD for the original!