Generating Layer 3 Interface DNS records for cisco devices

Traceroute looks up the PTR dns records for each hop. A lot of providers create ptr records that describe the layer 3 interface of a given router:

akonkol@use:~$ traceroute
traceroute to (, 30 hops max, 60 byte packets
 1 (  0.451 ms  0.568 ms  0.698 ms
 2 (  0.184 ms  0.215 ms  24.803 ms
 3 (  0.667 ms  0.726 ms  1.026 ms
 4 (  0.793 ms  0.860 ms  0.911 ms
 5 (  0.470 ms  0.451 ms  0.456 ms
 6 (  0.440 ms  0.437 ms  0.417 ms
 7 (  0.486 ms  0.512 ms  0.491 ms
 8 (  0.555 ms (  10.054 ms  10.044 ms
 9 (  7.835 ms (  3.690 ms  3.676 ms
10 (  7.489 ms (  7.450 ms (  7.409 ms
11 (  7.366 ms  7.332 ms (  7.357 ms
12  * * *
13 (  7.410 ms  7.436 ms  7.418 ms

Depending on how much equipment you manage you could have hundreds or even thousands of layer 3 interfaces. This problem intrigued me so I wrote a script that leverages tratto; a framework that I built back in 2012 to ssh/telnet to devices. The script is called which gets layer 3 interface information and constructs a fqdn.

Connection Code

The following snippet shows a few things: setting up the ssh session, issuing a "show ip interface brief" storing the results in "ip_ints."

#connection setup
import Connectiviy, Systems

device = args['device']
username = args['username']
os_type = Systems.OperatingSystems['IOS']

session = Connectivity.Session(device,port,transport,os_type)
session.login(username, password)
ip_ints = session.sendcommand("sho ip int br")

Parsing Code

We take ip_ints and for each line that contains an ip address, we split the line by "whitespace" which allows us to access the interface name and number separately allowing us to assign these values to int_name and int_ip. Based on the interface name we come up with an abbreviation: GE for gigabit interfaces, FE for fast ethernet interfaces,etc... Since DNS records do now allow for slashes, we replace those with hyphens and assign it to clean_suffix.

#for each line of sho ip int brief

for line in ip_ints.split('\r\n'):
   contains_ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line )
   if contains_ip:
        line_chunks = line.split()

        #[0]= gigabitethernet1/2
        int_name = line_chunks[0]

        #[1] = 10.a.b.c
        int_ip = line_chunks[1]

        if "GigabitEthernet" in int_name:
           prefix = "GE"
           suffix = int_name[15:]

        if "FastEthernet" in int_name:
           prefix = "FE"
           suffix = int_name[12:]

        if "Loopback" in int_name:
           prefix ="LO"
           suffix = int_name[8:]

        if "Vlan" in int_name:

        if "Tunnel" in int_name:
           suffix= int_name[6:]

        #ignore NVIs
        if "NVI" in int_name:

        #replace interface number slash with hyphen
        clean_suffix = re.sub('\/','-',suffix)

Schema and Return Code

The remaining code checks for any user defined schema. There are three variables you can specify in this "schema" (or format string): interface_name, interface_number, hostname, and ip_address. You would specify something like this to use a custom fqdn format: ./ -u akonkol -d -s "" and interface_name and hostname would be replaced with the dynamically learned information for device living at

if args['schema']:
           schema = args['schema']
           dns_line = schema.replace('interface_name',prefix)
           dns_line = dns_line.replace('interface_number',clean_suffix)
           dns_line = dns_line.replace('hostname',hostname)
           dns_line = dns_line.replace('ip_address',int_ip)
           print dns_line
           print prefix + clean_suffix  + "." + hostname +  domainname + "," +  int_ip

Some Examples:

akonkol@echo:~/Code$ ./ -u akonkol -d

akonkol@echo:~/Code$ ./ -u akonkol -d -dn

akonkol@echo:~/Code$ ./ -u akonkol -d -s ",ip_address"

Wait a second, this isn't actually creating the DNS entries! You would be correct, and I'm willing to bet that if you've read this far that you can figure how to do that... There are a few ways to do this and it all depends on what you are using for your DNS server (bind, windows, etc). The above script generates a CSV output which you could loop through and use to create dns records. Here is a great article on creating ptr records in bulk on a windows dns server DNS Bulk PTR records creation.

Tagged as cisco , dns ios , ptr python , tratto
Written by Andrew Konkol on July 22nd, 2014


Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.