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

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


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

$num_args = $#ARGV + 1;
if ($num_args != 1) {
  print "\nUsage: tags\n";
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 "nsk"

allow; #tags nsk,help
allow; #tags nsk,help
allow; #tags nsk,help
allow; #tags nsk,help2
allow; #tags help,help3,nsk
allow; #tags nsk
allow; #tags nsk
allow; #tags nsk
allow; #tags nsk,eee
allow; #tags nsk,test
allow; #tags nsk,test
allow; #tags nsk,help
allow; #tags nsk
deny all;