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. 33
      zabbix_scripts/check_unifi
  2. 36
      zabbix_scripts/disco_unifi

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

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

Loading…
Cancel
Save