1 DNS Backup Script
2 =================
3 :author: Aaron Ball
4 :email: nullspoon@iohq.net
5
6
7 == {doctitle}
8
9 I would like to start this post with an admission of guilt.
10
11 Hello, my name is swarleyman and I'm an arstechnicaholic.
12
13 Please don't judge me.
14
15 Anyways, I was reading it a few days ago and read
16 http://arstechnica.com/business/news/2012/03/how-anonymous-plans-to-use-dns-as-a-weapon.ars[an
17 article] about how Anonymous plans to dos worldwide DNS. All the politics of
18 this topic aside, it got me thinking. We are so incredibly reliant on DNS that
19 if it went down, it could have catastrophic effects on society. Okay, so not
20 being able to access your Facebook page for a few days might be a good thing.
21 What about paying your bills (though if your bill pay system can't access their
22 payment processing service, it doesn't really matter anyways)? With that, I
23 decided to research a good way to back up DNS.
24
25 After some searching I was, of course, disappointed. There is apparently no way
26 to back up DNS. You would think that there should be some way to make a
27 third-party copy, especially since it's such a vital service that's supposed to
28 be relatively open. Either way, we still have a few tools to work with to make
29 at least a semi-thorough backup.
30
31 The tools I chose to use were perl and nslookup. Unless I'm missing something,
32 I think nslookup is really the only good way to get relatively complete DNS
33 data. I know you can dig stuff, but i'm not looking to back up people's cname,
34 aaa, a, srv, etc. records (perhaps I'll come back and write up a script for
35 that too). With that, to run this script you need a 'nix system with perl and
36 nslookup installed (in the dnsutils package).
37
38 What this script does is run nslookup on every host in a text file (for example
39 ./dnsbak.pl hostlist.txt), parse the text and format it in a hosts file format.
40 All you should need to do is take the output from this script and append it to
41 your hosts file and you should be back up and running.
42
43 Here's teh codez!
44
45 ----
46 #!/usr/bin/perl -w
47 sub main () {
48 open hosts_list, $ARGV[0] or die("\nNo file specified or file does not exist\n");
49 # ONE HOST PER LINE
50 my @hosts=<hosts_list>;
51 close(hosts_list);
52 for (my $i=0; $i<scalar(@hosts); $i++) {
53 my $nslookup=`nslookup $hosts[$i]`;
54 my $site = new Site($nslookup);
55 $site->parse();
56 sleep(1);
57 }
58 }
59 main();
60 print "\n\n";
61
62 package Site;
63 sub new {
64 my $class = shift;
65 my $self = { _nslookupData=>shift };
66 bless $self, $class;
67 return $self;
68 }
69
70 sub parse() {
71 my ( $self )=@_;
72 my $data=$self->{_nslookupData};
73 my @data=split("\n", $data);
74 my @addresses;
75 my $server;
76 for (my $i=0; $i<scalar(@data); $i++) {
77 if ($i>=3) {
78 # MATCH THE HOSTNAME
79 if ($data[$i]=~/Name:\s(\w+\.\w+)/) { $server=$1; }
80 # MATCH THE IP ADDRESSES
81 if ($data[$i]=~/Address:\s{1,3}(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) { push(@addresses, $1); }
82 }
83 }
84 if (scalar(@data) > 4) {
85 print join("\t".$server."\n", @addresses);
86 print "\t".$server;
87 print "\n\n";
88 }
89 }
90 ----
91
92 Please leave some comments on my script if you have any. I still consider
93 myself quite the perl noob as I am completely self taught and don't really have
94 all of the super pro +1 up perl one-liner guru experience that you perl
95 veterans http://icanhascheezburger.com/[can has]. I look forward to hearing
96 some feedback on my seemingly too long and verbose script.
97
98 Ensign, engage.
99
100 Category:Backups
101 Category:Perl
102 Category:Scripting
103
104
105 // vim: set syntax=asciidoc:
|