Save cookies to a file so we don't have to login at every invocation

GLPI #34449
tags/zabbix-agent-addons-0.2.61-1
Daniel Berteaud 6 years ago
parent 0862bfa4bc
commit 54f98a5f27
  1. 19
      zabbix_scripts/check_unifi
  2. 18
      zabbix_scripts/disco_unifi

@ -5,8 +5,11 @@ use warnings;
use JSON; use JSON;
use Getopt::Long; use Getopt::Long;
use LWP::UserAgent; use LWP::UserAgent;
use HTTP::Cookies;
use Data::Dumper; use Data::Dumper;
umask 077;
my $user = 'zabbix'; my $user = 'zabbix';
my $pass = 'secret'; my $pass = 'secret';
my $site = 'default'; my $site = 'default';
@ -36,11 +39,20 @@ GetOptions (
my @radio_proto = qw/a b g na ng ac/; my @radio_proto = qw/a b g na ng ac/;
my $resp; my $resp;
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
my $cj = HTTP::Cookies->new(
file => "/tmp/.unifi_$username.txt",
autosave => 1,
ignore_discard => 1
);
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 }, ssl_opts => { verify_hostname => 0 },
cookie_jar => {} cookie_jar => $cj
); );
# Check if we need to login
$resp = $ua->get($url . '/api/self/sites');
if ($resp->is_error){
# Log into the API # Log into the API
$resp = $ua->post( $resp = $ua->post(
$url . '/api/login', $url . '/api/login',
@ -48,10 +60,11 @@ $resp = $ua->post(
Content_Type => 'application/json;charset=UTF-8' Content_Type => 'application/json;charset=UTF-8'
); );
die "Login failed: " . $resp->message . "\n" if $resp->is_error; die "Login failed: " . $resp->message . "\n" if $resp->is_error;
# Now, we need to get the site ID
$resp = $ua->get($url . '/api/self/sites'); $resp = $ua->get($url . '/api/self/sites');
die $resp->message . "\n" if $resp->is_error; die $resp->message . "\n" if $resp->is_error;
}
# Now, we need to get the site ID
foreach (@{from_json($resp->decoded_content)->{data}}){ foreach (@{from_json($resp->decoded_content)->{data}}){
if ($_->{name} eq $site || $_->{desc} eq $site){ if ($_->{name} eq $site || $_->{desc} eq $site){
$site_id = $_->{_id}; $site_id = $_->{_id};

@ -5,8 +5,11 @@ use warnings;
use JSON; use JSON;
use Getopt::Long; use Getopt::Long;
use LWP::UserAgent; use LWP::UserAgent;
use HTTP::Cookies;
use Data::Dumper; use Data::Dumper;
umask 077;
my $user = 'zabbix'; my $user = 'zabbix';
my $pass = 'secret'; my $pass = 'secret';
my $site = 'default'; my $site = 'default';
@ -32,11 +35,21 @@ GetOptions (
$type = 'all' if ($type eq ''); $type = 'all' if ($type eq '');
my $site_id; my $site_id;
my $resp; my $resp;
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
my $cj = HTTP::Cookies->new(
file => "/tmp/.unifi_$username.txt",
autosave => 1,
ignore_discard => 1
);
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 }, ssl_opts => { verify_hostname => 0 },
cookie_jar => {} cookie_jar => $cj
); );
# Check if we need to login
$resp = $ua->get($url . '/api/self/sites');
if ($resp->is_error){
# Login on the API # Login on the API
$resp = $ua->post( $resp = $ua->post(
$url . '/api/login', $url . '/api/login',
@ -44,9 +57,10 @@ $resp = $ua->post(
Content_Type => 'application/json;charset=UTF-8' Content_Type => 'application/json;charset=UTF-8'
); );
die "Login failed: " . $resp->message . "\n" if $resp->is_error; die "Login failed: " . $resp->message . "\n" if $resp->is_error;
# Now, we need to get the site ID
$resp = $ua->get($url . '/api/self/sites'); $resp = $ua->get($url . '/api/self/sites');
die $resp->message . "\n" if $resp->is_error; die $resp->message . "\n" if $resp->is_error;
}
foreach (@{from_json($resp->decoded_content)->{data}}){ foreach (@{from_json($resp->decoded_content)->{data}}){
if ($_->{name} eq $site || $_->{desc} eq $site){ if ($_->{name} eq $site || $_->{desc} eq $site){
$site_id = $_->{_id}; $site_id = $_->{_id};

Loading…
Cancel
Save