Men & Mice Men & Mice Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Scripting mass DNS deletions  XML
Forum Index -> Domain Name System (DNS)
Author Message
joelj
User

Joined: 20/05/2008 20:00:20
Messages: 10
Offline

Under a DNS cleanup project we will be receiving a csv file with a list of records (hundres or thousands) including IP, name and domain name. Pretty much we are looking for recommendation to script the following:

1. Read records from the csv file and verify if they are safe be deleted (ping and nslookup)
2. Create a verified file to be read by mmcmd
3. Have mmcmd reading the verified file and deleting records.

Thank you
Carsten Strotmann
Men & Mice Staff
[Avatar]

Joined: 26/07/2007 13:08:39
Messages: 159
Location: Germany
Offline

joelj wrote:
Under a DNS cleanup project we will be receiving a csv file with a list of records (hundres or thousands) including IP, name and domain name. Pretty much we are looking for recommendation to script the following:

1. Read records from the csv file and verify if they are safe be deleted (ping and nslookup)
2. Create a verified file to be read by mmcmd
3. Have mmcmd reading the verified file and deleting records.

Thank you 


Hello JoelJ,

can you specify step 1. in more detail. my guess is that the data will still be in the DNS at this moment, so "nslookup" will be successful but "ping" will not. What is the criteria for an entry to be save for deletion?

Also, can you give an example of the CSV file structure?

-- Carsten

----
Men & Mice Support Team
support@menandmice.com
joelj
User

Joined: 20/05/2008 20:00:20
Messages: 10
Offline

The format it is like this

IP Address,DNS Name,DNS Domain Name
172.22.238.31,PROSITE03,domain.com

you are right, if there is no response to ping the record should be removed, the data is coming from a system that supposedly had checked that but we want to add an extra check to safely remove the records.

Once we perform the check, the info about records being removed will be piped to a file so Men and Mice could read it and execute deletion.

Thank you,
Carsten Strotmann
Men & Mice Staff
[Avatar]

Joined: 26/07/2007 13:08:39
Messages: 159
Location: Germany
Offline

Hello JoelJ,

here is my script (in Ruby -> http://ruby-lang.org)

Code:
 require 'rubygems'
 require 'net/dns/resolver'
 require 'ping'
 require 'csv'  
 
 unless ARGV.length > 0
   puts "Usage: test-for-ipdevice-deletion.rb <csv-file>"
   exit
 end
  
 csvfile   = ARGV[0]
 filename  = csvfile.slice(0,csvfile.index(".csv"))
 ipdevices = CSV.read(csvfile) 
 
 mmcfile   = "#{filename}.mmc"
 puts "writing #{mmcfile} ..."
 cmdf = File.open(mmcfile, "w")
 
 # iterate over each line in the CSV
 ipdevices.each do | device |
   ipaddress = device[0]
   hostname  = device[1]
   dnszone   = device[2]
 
   pingtest = false
   dnstest  = false
 
   # test if IP Address is really an IP Address
   ip_pat = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
   if ipaddress =~ ip_pat
     puts "Testing #{ipaddress} with Ping (TCP-Echo)"
     # test IP Address with TCP-Echo ping
     if Ping.pingecho(ipaddress.to_s, 10, 80)
       puts "#{ipaddress} is alive!"
       pingtest = true
     end
 
     # test if the domain name exists in DNS
     domainname = "#{hostname}.#{dnszone}"
     puts "Testing #{domainname} with DNS A-Record lookup"
     packet = Net::DNS::Resolver.start("#{domainname}")
     header = packet.header
     answer = packet.answer
 
     # do we have an DNS query answer?
     if answer.any?
       dnstest = true
       puts "DNS answer contains #{header.anCount} entries"
       # test each IP address returned
       packet.each_address do |ip|
         if Ping.pingecho(ip.to_s, 10, 80)
           pingtest = true
         end
       end
     end
 
     if pingtest = false && dnstest = true
       # open DNS zone, remove A-Record and save zone"
       cmdf.puts "open #{dnszone}"
       cmdf.puts "delRec - #{domainname} A #{ipaddress}"
       cmdf.puts "save -"
       # remove IP device from IPAM Module
       cmdf.puts "removeDevice #{ipaddress}"
     end
   end
 end
 
 cmdf.puts "quit"
 cmdf.close
 


This is the example CSV I've used for testing:

Code:
 cas@sun11:~/dnsruby$ more testdata.csv
 "IP Address","Hostname","DNS Zone"
 "192.168.1.1","test1","example.com."
 "192.168.1.2","test4","example.org"
 "192.168.1.10","somehost","test.example"
 "192.168.1.20","server","example.com."
 "192.168.1.40","bigmachine","example.com."
 


The script writes a command file for the Men & Mice CLI with the same base-filename as the CSV file but with extension "mmc". This mmc file can then be called with the Men & Mice CLI:

Code:
 mmcmd -s <server> -u <username> -p <password> -f <mmc-file>
 

----
Men & Mice Support Team
support@menandmice.com
joelj
User

Joined: 20/05/2008 20:00:20
Messages: 10
Offline

Thanks so much Carsten, I'll give it a try and post results.

Greatly appreciated!
joelj
User

Joined: 20/05/2008 20:00:20
Messages: 10
Offline

Carsten, please excuse my ignorance I really tried hard to run the script, but this is the error I am getting,

C:\Ruby191>ruby test-for-ipdevice-deletion.rb mmtest1.csv
test-for-ipdevice-deletion.rb:2:in `require': no such file to load -- net/dns/resolver (LoadError)
from test-for-ipdevice-deletion.rb:2:in `<main>'

I've have installed ruby, rubygems and net-dns 0.6.1 on Windows
Carsten Strotmann
Men & Mice Staff
[Avatar]

Joined: 26/07/2007 13:08:39
Messages: 159
Location: Germany
Offline

Hello Joel,

on windows I had to do a

Code:
 gem install net-dns
 


to install the DNS Resolver code.

Also, "net-ping" must be installed

Code:
 gem install net-ping
 


It seems that on Windows, Ruby 1.9.x does not support "ping" yet, so I recommend to downgrade to Ruby 1.8.x, or to use a windows specific implementation of "ping" (like wrapping the "ping" command inside a IO.popen statement).

----
Men & Mice Support Team
support@menandmice.com
joelj
User

Joined: 20/05/2008 20:00:20
Messages: 10
Offline

Thanks much! I was able to properly install ruby and required gems, the script is running but the only thing I am getting in the mmc file is the word "quit".

Playing around because I discovered that some host names still are in use with a different IP I added:

# test if the host responds pinging by name
domainname = "#{hostname}.#{dnszone}"
puts "Testing #{domainname} with ping"
packet = Net:NS::Resolver.start("#{domainname}")
header = packet.header
answer = packet.answer

The screen output is (for instance)

Testing 172.22.238.202 with Ping (TCP-Echo)
Testing PROSITE07.mydomain.com with ping
DNS answer contains 1 entries
Testing 172.31.19.54 with Ping (TCP-Echo)
Testing host02.mydomain.com with ping
host02 is alive!
DNS answer contains 1 entries
Testing 172.31.16.190 with Ping (TCP-Echo)
Testing host03.mydomain.com with ping
DNS answer contains 1 entries
Testing 172.28.179.203 with Ping (TCP-Echo)
Testing host04.mydomain.com with ping
DNS answer contains 3 entries

But nothing is being written to the mmc file.
Carsten Strotmann
Men & Mice Staff
[Avatar]

Joined: 26/07/2007 13:08:39
Messages: 159
Location: Germany
Offline

Hello Joel,

joelj wrote:

Playing around because I discovered that some host names still are in use with a different IP I added:

# test if the host responds pinging by name
domainname = "#{hostname}.#{dnszone}"
puts "Testing #{domainname} with ping"
packet = Net::DNS::Resolver.start("#{domainname}")
header = packet.header
answer = packet.answer
 


That change should not be necessary, as the script already tests all A-Records existing for a given name using the ping.

The snipped above will only test one out of a group of IP Addresses for a name, as the "ping" command will just use the first entry of the DNS resource record set returned.

In the original script, please add

Code:
 
  puts "Ping: #{pingtest}"
  puts "DNS: #{dnstest}"
 
  if pingtest = false && dnstest = true
 


an then send me the whole screen output (can be redirected to a file) for inspection to support@menandmice.com

----
Men & Mice Support Team
support@menandmice.com
 
Forum Index -> Domain Name System (DNS)
Go to:   
Powered by JForum 2.1.7 © JForum Team