|
|
@ -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}; |
|
|
|