Linode DNS server automation script
Posted: Thu May 11, 2017 2:31 pm
Hi,
I've been doing a lot of work moving over one of my old servers to a new setup with VestaCP. The most of it has been easy, but I've found it a total PITA getting the DNS stuff done on Linode (as you have to setup each domain individually!). So, I decided to write a script to make it easier!
This is ONLY for Linode users, so please be aware of that. This script will:
1) Create the DNS records for you at Linode's end
2) Setup DKIM and SPF for you for the domain in question
The instructions are pretty simple:
1) Run the following commands to setup the linode-cli:
2) Log into your account at Linode, and then go to: https://manager.linode.com/profile/api . From there, request the access key. Keep this key safe for the moment - you will need it in a second.
3) Copy the code below, and save as add-to-dns.cgi. Upload this somewhere on your server (that you will remember where it is ;))
4) Now you need to update your settings to match your server:
The email is the point of reference you want to use on the SOA record.
$key is the value you got in the last step
$ip_4 and $ip_6 are the IPv4/6 IP's, so make sure to set those to your own IP's!
5) Now just give it a run :)
So an example:
This is still a work in progress, but feel free to give me any feedback :) Hopefully someone else gets some use out of it.
Cheers
Andy
I've been doing a lot of work moving over one of my old servers to a new setup with VestaCP. The most of it has been easy, but I've found it a total PITA getting the DNS stuff done on Linode (as you have to setup each domain individually!). So, I decided to write a script to make it easier!
This is ONLY for Linode users, so please be aware of that. This script will:
1) Create the DNS records for you at Linode's end
2) Setup DKIM and SPF for you for the domain in question
The instructions are pretty simple:
1) Run the following commands to setup the linode-cli:
Code: Select all
wget -O- https://apt.linode.com/linode.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install linode-cli
3) Copy the code below, and save as add-to-dns.cgi. Upload this somewhere on your server (that you will remember where it is ;))
Code: Select all
#!/usr/bin/perl
use WebService::Linode;
my $ip_4 = "123.123.123.123"; # the IPv4 address of your server
my $ip_6 = "xx::xxx:xxx::xxx"; # the IPv6 address of your server
my $soa_email = '[email protected]'; # this is the SOA email you want to use
my $key = 'xxxx'; # Get this from your Linode.com account online (https://manager.linode.com/profile/api)
handle();
sub handle {
if (!$ARGV[0]) {
helper();
return;
}
my $domain = $ARGV[0]; # the domain in question
my $username = $ARGV[1]; # who owns the account
my $type = $ARGV[2]; # perl, modperl or none
if (!-d "/home/$username/web/$domain") {
print qq|Sorry, /home/$username/web/$domain doesn't exist. Please add the domain in VestaCP first!\n|;
helper();
exit;
}
my $api = WebService::Linode->new( apikey => $key );
my $list = $api->domain_list();
# Check if this domain already exits in the DNS.. if it does we need to remove it first before we try and re-add!
foreach (@$list) {
if ($_->{domain} eq $domain) {
print qq|Removing from DNS..\n|;
$api->domain_delete(domainid => $_->{domainid});
last;
}
}
my $domain_rec = $api->domain_create( domain => $domain, type => "master", soa_email => $soa_email, master_ips => "$ip_4" );
print qq|Got domain ID: $domain_rec->{domainid} \n|;
# create the MX record
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "MX", target => "mail.$domain" , priority => 10, name => "" );
my $result_dkim = `v-list-mail-domain-dkim-dns $username $domain`;
my @tmp = split /\n/, $result_dkim;
my @values = split /\s{2,20}/, $tmp[3];
my $dkim = $values[4];
$dkim =~ s/\n//g;
$dkim =~ s/^"|"$//g;
# dkim, spf etc records
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "TXT", name => "", target => "v=spf1 a mx ~all" );
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "TXT", name => "mail._domainkey", target => $dkim );
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "TXT", name => "_domainkey", target => "t=y; o=~;" );
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "A", target => $ip_4, name => "" );
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "AAAA", target => $ip_6, name => "" );
foreach (qw/cdn www/) {
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "A", target => $ip_4, name => $_ );
$api->domain_resource_create( domainid => $domain_rec->{domainid}, type => "AAAA", target => $ip_6, name => $_ );
}
}
sub helper {
print qq|Instructions:
perl add-to-dns.cgi %domain% %username%
%domain% - the domain you want to add
%username% - the username of who OWNS the domain
|;
}
Code: Select all
my $ip_4 = "123.123.123.123"; # the IPv4 address of your server
my $ip_6 = "xx::xxx:xxx::xxx"; # the IPv6 address of your server
my $soa_email = '[email protected]'; # this is the SOA email you want to use
my $key = 'xxxx'; # Get this from your Linode.com account online (https://manager.linode.com/profile/api)
$key is the value you got in the last step
$ip_4 and $ip_6 are the IPv4/6 IP's, so make sure to set those to your own IP's!
5) Now just give it a run :)
Code: Select all
perl add.cgi %domain% %username%
Code: Select all
perl add.cgi sometest.com myusername
Cheers
Andy