Guest User

Untitled

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