Some useful perl scripts for interacting with the Neustar Enterprise RESTful API

If you manage a bunch of domains with Neustar, you’ve probably discovered their stupid interface has no manner in which you can export all of your domains.  The domain list will give you 25, 100, 200 domains per page, but if you click Export, you’ll soon discover you can only export 25 at a time.  Here’s a perl script that will spit all your domains out, however, their API limits you to 1000 domains max, so if you have more than that, you’re screwed and will have to contact their support to get a complete zone list:

#!/usr/bin/perl
use warnings;
use lib 'lib';
use JSON;
use LWP;
use HTTP::Request::Common;
use HTTP::Status qw( :constants );

my $base_rest_url = 'https://restapi.ultradns.com/v1';
my $debug=0;
my $json = JSON->new;
my $ua = LWP::UserAgent->new;

my $self = shift;
my $req_args = {
 grant_type => 'password',
 username => 'MyNeustarUserAccount',
 password => 'MyPassword'
};

# Proceed with logging in and store auth token (which is good for an hour)
my $response = $ua->post( "$base_rest_url/authorization/token", $req_args );

if ( $response->is_error ) {
 print "Login failure.\n";
 exit;
} else {
 my $content = $json->decode( $response->content );
 $self->{tokenType} = $content->{tokenType};
 $self->{accessToken} = $content->{accessToken};
}

# Change the following limit number to be something higher
# than your total number of domains.
my $request = GET "$base_rest_url/zones?limit=1000";

# Add auth token back to new request
$request->header( 'Authorization' => "$self->{tokenType} $self->{accessToken}" );

# Send request to get domain list
$response = $ua->request($request);

my %jsonDecode = %{ $json->decode( $response->content )};

for (keys $jsonDecode{zones}) {
 $zone = $jsonDecode{zones}[$_]{properties}{name};
 print "$zone\n";
}

Leave a Reply

Your email address will not be published. Required fields are marked *