|
|
@ -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,22 +39,32 @@ 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 |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
# Log into the API |
|
|
|
# Check if we need to login |
|
|
|
$resp = $ua->post( |
|
|
|
$resp = $ua->get($url . '/api/self/sites'); |
|
|
|
$url . '/api/login', |
|
|
|
if ($resp->is_error){ |
|
|
|
Content => to_json({ username => $user, password => $pass }), |
|
|
|
# Log into the API |
|
|
|
Content_Type => 'application/json;charset=UTF-8' |
|
|
|
$resp = $ua->post( |
|
|
|
); |
|
|
|
$url . '/api/login', |
|
|
|
die "Login failed: " . $resp->message . "\n" if $resp->is_error; |
|
|
|
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 |
|
|
|
# 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}}){ |
|
|
|
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}; |
|
|
|