Advertisement
Nergalwaja

gethost.pl (modified)

Feb 11th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.22 KB | None | 0 0
  1.  5.010;
  2. use strict;
  3. use warnings;
  4. use JSON::RPC::Client;
  5. use Data::Dumper;
  6.  
  7. # Authenticate yourself
  8.  my $client = new JSON::RPC::Client;
  9.  my $url = 'http://10.10.10.108/zabbix/api_jsonrpc.php';
  10.  my $authID;
  11.  my $response;
  12.  
  13.  my $json = {
  14.  jsonrpc => "2.0",
  15.  method => "user.login",
  16.  params => {
  17.  user => "zapper",
  18.  password => "zapper"
  19.  },
  20.  id => 1
  21.  };
  22.  
  23.  $response = $client->call($url, $json);
  24.  
  25. # Check if response was successful
  26.  die "Authentication failed\n" unless $response->content->{'result'};
  27.  
  28.  $authID = $response->content->{'result'};
  29.  print "Authentication successful. Auth ID: " . $authID . "\n";
  30.  
  31. # # Get list of all hosts using authID
  32.  
  33.  $json = {
  34.  jsonrpc=> '2.0',
  35.  method => 'usergroup.get',
  36.  params =>
  37.  {
  38.  output => ['userids', 'name'],# get only host id and host name       # sort by host name
  39.  sortfield => 'name',
  40. },
  41.  id => 2,
  42.  auth => "$authID",
  43.  };
  44.  $response = $client->call($url, $json);
  45.  
  46. # Check if response was successful
  47.  die "usergroup.get failed\n" unless $response->content->{result};
  48.  
  49.  print "List of hosts\n-----------------------------\n";
  50.  foreach my $host (@{$response->content->{result}}) {
  51.  print "Host ID: ".$host->{userid}." Host: ".$host->{name}."\n";
  52.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement