Child pages
  • Пример скрипта на perl для работы с IPAM REST
Skip to end of metadata
Go to start of metadata

Это скрипт который берет информацию из IPAM REST и формирует список доступа для nginx

#!/usr/bin/perl

use HTTP::Request;
use LWP::UserAgent;
use JSON;
use Data::Dumper;
use URI;

$num_args = $#ARGV + 1;
if ($num_args != 1) {
  print "\nUsage: name.pl tags\n";
  exit;
}
my $tags=$ARGV[0];


my $noc_url="http://stage.noc/ip/prefix/";
my $noc_user="admin";
my $noc_password="admin";


my $url = URI->new($noc_url);

#
# Will get info from IPAM based on tags 
#
$url->query_form(tags__contains => $tags);

my $req = HTTP::Request->new('GET', $url );
$req->authorization_basic($noc_user, $noc_password);
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

my $lwp = LWP::UserAgent->new;
$lwp->request( $req );

$ua = LWP::UserAgent->new;
my @result= @{from_json($ua->request($req)->content)};

for (my $i = 0; $i < @result; $i++ ) 
{
    print "allow " . $result[$i]{prefix} . "; #tags " . join(",", @{$result[$i]{tags}}) . "\n" ;
}

print "deny all;" . "\n" ;

Запускаем как и получаем:

perl ipam_get.pl "nsk"

allow 192.168.99.0/27; #tags nsk,help
allow 192.168.99.0/26; #tags nsk,help
allow 192.168.99.0/25; #tags nsk,help
allow 192.168.99.0/24; #tags nsk,help2
allow 192.168.99.128/27; #tags help,help3,nsk
allow 192.168.100.0/24; #tags nsk
allow 192.168.102.0/24; #tags nsk
allow 192.168.102.0/25; #tags nsk
allow 192.168.102.0/26; #tags nsk,eee
allow 192.168.77.0/24; #tags nsk,test
allow 192.168.77.0/25; #tags nsk,test
allow 192.168.127.8/29; #tags nsk,help
allow 91.144.128.56/29; #tags nsk
deny all;