Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dump::Color;
use Getopt::Long;
use JSON;
my ($all,$pretty,$create,$remove,$ssid,$site_id,$pwd);
my $ok = GetOptions(
"pretty" => \$pretty,
"list" => \$all,
"create" => \$create,
"remove" => \$remove,
"ssid=s" => \$ssid,
"site_id=s" => \$site_id,
"password=s" => \$pwd,
);
my $client;
my $i = {};
if ($all) {
require MongoDB;
MongoDB->import();
$client = MongoDB->connect("mongodb://localhost:27117");
my $a = find_all_sites();
print "SiteKey\tSiteID\tSiteDesc\n";
foreach my $site_id(keys %$a) {
my $desc = $a->{$site_id}->{desc} // "";
my $key = $a->{$site_id}->{name} // "";
my $wlan = find_wlan($site_id);
print "$key\t$site_id\t$desc\n";
foreach my $w (@$wlan) {
my $wlan_id = $w->{'_id'};
my $ssid = $w->{name};
print "\t$ssid/$wlan_id\n";
}
}
} elsif ($create && $ssid && $site_id && $pwd) {
# function create_ssid($site_id,$ssid,$password,$sec_mode = "wpa2")
print "Create SSID $ssid (pwd: $pwd) on $site_id\n";
} elsif ($remove && $ssid && $site_id) {
# function remove_ssid($site_id,$wlan_id)
print "Remove SSID $ssid on $site_id\n";
} else {
print usage();
}
#####################################################################################
sub find {
my $site_id = shift();
my $ret = {};
my $member = find_member($site_id);
if (!$member) {
return $ret;
}
$site_id = $member->{_id}->to_string; # In case we search for a RAW OID
$ret->{member} = $member;
my $devices = find_devices($site_id);
$ret->{devices} = $devices;
my $wlan = find_wlan($site_id);
$ret->{wlan} = $wlan;
return $ret;
}
sub find_member {
my $str = shift();
my $site = $client->ns("ace.site");
my $ret = '';
if (length($str) > 12) {
my $id = MongoDB::OID->new($str);
my $cur = $site->find({"_id" => $id});
$ret = $cur->next;
} else {
my $cur = $site->find({"desc" => qr/$str/});
$ret = $cur->next;
}
return $ret;
}
sub find_devices {
my $site_id = shift();
my $device = $client->ns("ace.device");
my $cur = $device->find({site_id => $site_id});
my @ret;
while (my $i = $cur->next) {
push(@ret,$i);
}
return \@ret;
}
sub find_wlan {
my $site_id = shift();
my $device = $client->ns("ace.wlanconf");
my $cur = $device->find({site_id => $site_id});
my @ret = ();
while (my $i = $cur->next) {
push(@ret,$i);
}
return \@ret;
}
sub find_all_sites {
my $site = $client->ns("ace.site");
my $cur = $site->find({});
my $ret = {};
while (my $i = $cur->next) {
my $site_id = $i->{_id}->to_string;
$i->{_id} = $site_id;
$ret->{$site_id} = $i;
}
return $ret;
}
sub usage {
return "$0 [--list] [--create] [--remove] [--ssid SSID --password PASSWORD --site_id SITE_ID]\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment