RESTFUL / API / Search Device with Perl script

Document created by Mgvergelin on Jun 30, 2015Last modified by SamCreek on Dec 17, 2016
Version 2Show Document
  • View in full screen mode

Leng: PERL

Environment: Windows

 

Español: Hola, debido a que no encontré información suficiente de como hacer una búsqueda de dispositivos con la REST me dedique unos días a estudiar la poderosa herramienta.

Tome algunos ejemplos y creé un script en perl que permite realizar una búsqueda con argumentos de IP de dispositivos, y así poder buscarlos automáticamente y listarlos en una tabla HTML.

 

English: Hi, As I didn't found any document with information of device search with REST. I opened some perl examples and made a new perl script with this task, the same use arguments (IP List) and he finds and gets information from Spectrum. Then the script makes a list in HTML format.

 

El código / The Code: (Yo no soy un programador avanzado por lo tanto seguro encuentran como optimizarlo / I'm not a advanced programmer. Therefore, I think you can optimize the code.

 

#CODE by MGvergelin #se deben poner las ip al lado del comando para que funcione use lib '.\lib'; use REST::Client; use Path::Class; use Cwd; use XML::Tidy; use MIME::Base64; use XML::XPath::XMLParser; use XML::XPath::Step; use XML::XPath::Expr; use XML::XPath::Function; use XML::XPath::LocationPath; use XML::XPath::Variable; use XML::XPath::Literal; use XML::XPath::Number; use XML::XPath::NodeSet;  my $OneClick_Host = 'IMPORTANT! ADD YOU ONE CLIC HOST IP'; my $username = 'IMPORTANT! ADD YOU USER'; my $password = 'IMPORTANT! ADD YOU PASS';  #se declara en la variable la ruta actual my $current_working_dir = getcwd(); $current_working_dir =~ s/\//\\/g;  #cuento la cantidad de argumentos para generar el for correspondiente my $totalArgumentos; $totalArgumentos = scalar(grep $_, @ARGV);  print "$totalArgumentos\n";  #declaro array para linea completa my @arraycrudoXMLOR;  #el for for (my $i=0; $i <= $totalArgumentos; $i++) {    @arraycrudoXMLOR[$i] = ("<equals><attribute id='0x12d7f'><value>$device@ARGV[$i]</value></attribute></equals>");   }    #printf @arraycrudoXMLOR[2]; #crudo del OR para armar el Crudo del XML completo my $crudoXML; $crudoXML = join("", @arraycrudoXMLOR);  #se ve el crudo temporal print $crudoXML;  #XML en RAW my @array =("<rs:model-request throttlesize='5000' xmlns:rs='http://www.ca.com/spectrum/restful/schema/request' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd '> <rs:target-models>   <rs:models-search><rs:search-criteria xmlns='http://www.ca.com/spectrum/restful/schema/filter'><devices-only-search /><filtered-models> <or>","$crudoXML","</or></filtered-models></rs:search-criteria></rs:models-search></rs:target-models><rs:requested-attribute id='0x1006e' /><rs:requested-attribute id='0x129fa' /><rs:requested-attribute id='0x10000' /><rs:requested-attribute id='0x12d7f' /></rs:model-request>");  #se ve el crudo completo my $crudoXMLcompleto;  $crudoXMLcompleto = join("", @array); print $crudoXMLcompleto; my $client = REST::Client->new(); $client->setHost('http://'.$OneClick_Host); $client->addHeader('Accept', 'application/xml'); $client->addHeader('Authorization', 'Basic ' . encode_base64($username . ':' . $password));   $client->POST("spectrum/restful/models?", "$crudoXMLcompleto");  my $tidy_obj = XML::Tidy->new( xml => $client->responseContent()); $tidy_obj->tidy(); $tidy_obj->write($current_working_dir.'\devices.xml');  system($current_working_dir.'\busquedaip.html');  exit 0;

 

Example:

 

Captura.JPG

 

Result:

 

Captura.JPG

 

Is All! Thanks!

 

Regards.

 

Marcos Gabriel Vergelin.

Outcomes