Another itch to scratch: which vSwitch is a pNIC connected to? To solve this simple problem I created a quick perl script…
This script also lets me see the driver in use, connection speed and duplex setting, and the MAC address of the pNIC.
1 2 3 4 5 6 7 |
# Sample output: Adaptor (Driver) Speed (Duplex) MAC vSwitch ---------------- -------------- --- ------- vmnic1 (bnx2) 1000 (Full) 00:00:00:00:00:00 vSwitch0 vmnic0 (tg3) 1000 (Full) 00:00:00:00:00:00 vSwitch0 vmnic3 (tg3) 1000 (Full) 00:00:00:00:00:00 vSwitch1 vmnic2 (bnx2) 1000 (Full) 00:00:00:00:00:00 vSwitch1 |
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 |
#!/usr/bin/perl -w # # esx-nic-cfg.pl - written by Andrew Sullivan, 2009-04-08 # # Please report bugs and request improvements at http://get-admin.com/blog/?p=634 # # Simple perl script to display some basic info about physical nics on an esx host # # Examples: # Connecting directly to the esx host # ./esx-nic-cfg.pl --server your.esx.host # # Connecting via vCenter # ./esx-nic-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 to interrogate, 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); my $vswitches = $networkSystem->networkInfo->vswitch; my $pnics = $networkSystem->networkInfo->pnic; # get the map of pnics to vswitches, we use this later to display the vswitch a pnic # is connected to my %pnic_to_vswitch = (); foreach my $vswitch (@$vswitches) { if (scalar(@{$vswitch->pnic}) > 0) { foreach (@{$vswitch->pnic}) { $pnic_to_vswitch{$_} = $vswitch->name; } } } # a header, to make it more readable print "Adaptor (Driver)tSpeed (Duplex)ttMACtttvSwitchn"; print "----------------t--------------tt---ttt-------n"; foreach (@$pnics) { print $_->device . " (" . $_->driver . ")" . "tt" . $_->linkSpeed->speedMb . " (" . ($_->linkSpeed->duplex eq "1" ? "Full" : "Half") . ")tt" . $_->mac . "t" . $pnic_to_vswitch{$_->key} . "n"; } Util::disconnect(); |
Would it be possible to get your script to also output the PCI slot location of each vmnic? I forget what the field is titled at the moment however I usually access that information via the “esxcfg-nics -l” command.
Just a though as would be nice to get the output in one go in your script.
Sure, just print the pci attribute…
There is quite a bit of information available, so I may adjust this script (when I can test it, which wouldn’t be until Monday) so that it shows a couple of fields by default, but providing some options will display additional information.
Would it be possible to extend this script and get the virtual machine info (like mac address…)
connected to vswitch. So one script will display the virtual machine, vswitch and physical adapter info.
While running this script I am hitting this error
Can’t call method “networkSystem” on an undefined value at ASHISH.pl line 63, line 2.
ashish,
Yes, it should be possible to add the virtual machines to the output. I’ll work on that when I get some spare time and post about it.
As for the error you’re getting, it looks like the host object is not being created. Make sure that name is correct and ensure that the host object is being populated with the correct data. You can use Data::Dumper to print the object out.
At line 55, add the following:
Which will show you the contents of that variable.
Hi Andrew.
I have the same problem as ashish.
when I dump the content of the variable, I can see pnic, proxySwitch, consoleVnic, dnsConfig, etc but not vswitch.
Do you have any idea about this situation?
Thanks in advance…