VMware {code} Community
rzubin
Contributor
Contributor

find_entity_view fails when I use a Host IP address in the name filter


Hi ,


 


I am using the perl SDK to create a VM


I get an errror that the host is not found when i filter out find_entity_view using the name filter and passing it the Host IP address like this


 my $host_view = Vim::find_entity_view(view_type => "HostSystem",
                                filter => {'name' => $hostIP });



 


However when i resolve the IP address to get the hostname and then pass it find_entity_view it works


$hostName = gethostbyaddr(inet_aton($hostIP), AF_INET) or die "Can't resolve $hostIP: $!\n";


my $host_view = Vim::find_entity_view(view_type => "HostSystem",
                                filter => {'name' => $hostName });


I have seen examples in which vmcreate.pl makes use of a HOST IP address .


Any ideas where I am going wrong ?


Thanks in advance


Zubin


 



 

4 Replies
stumpr
Virtuoso
Virtuoso

That's expected.  The name property matches the display name in vCenter.  To find a host by IP address, you'll have to use FindByIp().  It's a method in SearchIndex. 

The other option is to actually pull out the vnic information for the Host and iterate it's management interfaces.  For hosts, networking is a bit more complex as certain functions can be specified to various interfaces.  Though for a quick IP search probably not something to worry about.

I'll hack up an example if you need one, I don't have the cycles until later in the day.

Reuben Stump | http://www.virtuin.com | @ReubenStump
rzubin
Contributor
Contributor


Thanks stumpr !!


This is what I have got so far ..


 


my $service_content = Vim::get_service_content();
 


my $search_index = Vim::get_view(mo_ref => $service_content->searchIndex);
my $esxi = $search_index->FindByIp(ip => "XX.XX.XX.XX", vmSearch => "false");


so now can I use $esxi instead of $host_view I am using in the initial question ?


An example would be much helpful ! would appreciate that


Thanks


Zubin


 

0 Kudos
stumpr
Virtuoso
Virtuoso

That's basically it.  You just need to call get_view() on the reference returned by FindByIp().

Just add what specific HostSystem properties you want in the anonymous properties array parameter to get_view().  I also tried using other ip addresses than the management interface and it did not work.  So if you want to find a HostSystem by the vMotion ip address, you'll need to use a more advanced ContainerView or TraversalSpec.

use strict;

use warnings;

use VMware::VIRuntime;

my %opts = (

  ipaddr => {

  type => ":s",

  help => "ESXi HostSystem IP Address",

  required => 1,

  },

);

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

my ($host, $sc, $si, $mor);

$sc     = Vim::get_service_content();

$si     = Vim::get_view(mo_ref => $sc->{'searchIndex'});

$mor    = $si->FindByIp(ip => Opts::get_option('ipaddr'), vmSearch => 0);

unless ($mor)

{

    die "Failed to find HostSystem with IP address '" . Opts::get_option('ipaddr') . "'\n";

}

$host   = Vim::get_view(mo_ref => $mor, properties => ['name']);

print "Found HostSystem '" . $host->{'name'} .

    "' with IP address '" . Opts::get_option('ipaddr') . "'\n";

Util::disconnect();

Reuben Stump | http://www.virtuin.com | @ReubenStump
rzubin
Contributor
Contributor

Thanks a lot stumpr ! The example really cleared a lot of things for me !

0 Kudos