2 Replies Latest reply on Feb 1, 2013 1:23 PM by Leon Adato

    Simple query and loop through list operation (Perl)

    Leon Adato

      I've got to be missing something simple.

       

      My goal is to query SolarWinds, get a list of node names, and then loop through that list to perform various operations (here, we'll just print the node name)

       

      use SW::InformationService;

      use Data::Dumper;

      use XML::LibXML;

       

      my $hostname = "myhostname"; # fill in a hostname

      my $username = "myaccount";

      my $password = "mypassword";

       

      my $endpoint = "https://$hostname:17778/SolarWinds/InformationService/OrionBasic";

      my $swis = SW::InformationService->new();

      $swis->outputxml("true");

      $swis->proxy($endpoint);

      $swis->proxy->ssl_opts(verify_hostname => 0);

      $swis->proxy->http_request->authorization_basic($username, $password);

      open SWISOUT, ">swisout.xml" || die "can't open swisout.xml\n";

      print SWISOUT $swis->QueryXml('SELECT Caption FROM Orion.Nodes WHERE Caption like \'%blahblah%\'');

      close SWISOUT;

      my $filename = "swisout.xml";

       

      At this point, I have the XML in a file. But for the LIFE of me, I can't figure out how to parse through the list and simply print out a list of node names.

       

      Some things I've tried:

      my $parser = XML::LibXML->new();

      my $doc = $parser->parse_file($filename);

      foreach my $nodeinfo ($doc->findnodes('/QueryXmlResponse/QueryXmlResult/queryResult/data/Nodes')) {

          my($caption) = $nodeinfo->findnodes('./Caption');

          print $caption->to_literal, "\n"

        }

       

      print "try again \n";

      my $dom = XML::LibXML->new->parse_file($filename);

      for my $node ($dom->findnodes('/QueryXmlResponse/QueryXmlResult/queryResult/data/Nodes/@Caption')) {

          say $node->toString;

      }

       

      print "try again again\n";

       

      my $parser = XML::LibXML->new();

      my $xmldoc = $parser->parse_file($filename);

       

      for my $sample ($xmldoc->findnodes('/QueryXmlResponse/QueryXmlResult/queryResult/data/Nodes')) {

          for my $property ($sample->findnodes('./*')) {

              print $property->nodeName(), ": ", $property->textContent(), "\n";

          }

          print "\n";

       

      Any hints?