Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Reverse DNS in BASH script

  1. #1
    Join Date
    Mar 2006
    Beans
    80

    Reverse DNS in BASH script

    Hi guys,

    What's the best way to do a reverse dns lookup in a bash script?

    I know you can use host, dig and nslookup but they all seem to return extra information and I don't really want to have to grep/gawk/sed the output to get what I need.

    The best I've come up with so far is:

    HOST=$(host $IP | cut -d " " -f 5);


    There must be a nicer way!

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Reverse DNS in BASH script

    indeed there is, Python code:
    Code:
    >>> import socket
    >>> socket.gethostbyaddr('66.249.89.104')

  3. #3
    Join Date
    Mar 2006
    Beans
    80

    Re: Reverse DNS in BASH script

    I'm a bit confused, I'm writing a bash script not a python one.

  4. #4
    Join Date
    May 2008
    Beans
    5

    Post Re: Reverse DNS in BASH script

    If you're interested, I wrote a small python script to reverse the ip.
    (Maybe it already exists, and there is of course another way to do it, but this one works)

    Code:
    #!/usr/bin/python
    
    def ip_reversed(ip, separator='.'):
        "Transform a.b.c.d to d.c.b.a. you can even use a.b.c.d.e.f..., and change the separator (here: the dot)."
    
        ipListe = ip.split(separator)
        ipListeReversed = []
    
        n = len(ipListe)
        while n != 0:
            ipListeReversed.append(ipListe[n-1])
            n -= 1
            continue
    
        return separator.join(ipListeReversed)
    
    import sys
    print( ip_reversed(sys.argv[1]) )
    I advice you to 'chmod +x blabla.py'
    and then use it like this: ./blabla.py 91.189.94.12


    The code could have been simplificated, but the advantage here, is that you can use things like :
    a.b.c.d, a.b.c.d.e.f.g.h, or ff:gh:00, or even "reversed more not now is text this", just specify the second argument as the separator (" " in the last sentence. "." is taken by default)

  5. #5

    Re: Reverse DNS in BASH script

    Quote Originally Posted by freebourg View Post
    If you're interested, I wrote a small python script to reverse the ip.
    (Maybe it already exists, and there is of course another way to do it, but this one works)

    Code:
    #!/usr/bin/python
    
    def ip_reversed(ip, separator='.'):
        "Transform a.b.c.d to d.c.b.a. you can even use a.b.c.d.e.f..., and change the separator (here: the dot)."
    
        ipListe = ip.split(separator)
        ipListeReversed = []
    
        n = len(ipListe)
        while n != 0:
            ipListeReversed.append(ipListe[n-1])
            n -= 1
            continue
    
        return separator.join(ipListeReversed)
    
    import sys
    print( ip_reversed(sys.argv[1]) )
    I advice you to 'chmod +x blabla.py'
    and then use it like this: ./blabla.py 91.189.94.12


    The code could have been simplificated, but the advantage here, is that you can use things like :
    a.b.c.d, a.b.c.d.e.f.g.h, or ff:gh:00, or even "reversed more not now is text this", just specify the second argument as the separator (" " in the last sentence. "." is taken by default)

    .... The Fail is Strong with This One.

  6. #6

    Re: Reverse DNS in BASH script

    Anywho since this 4+ year old thread still shows up in google over this topic.

    apt-get install dnsutils

    then incorporate the dig command into a bash script, (since dig ip will have a reverse lookup in it).

  7. #7
    Join Date
    Mar 2007
    Beans
    8

    Re: Reverse DNS in BASH script

    This bash example should print all the hosts from 192.168.1.1 throught .254. (You could edit the IP address lines to suit your network, perhaps.)

    rdns.sh:
    Code:
    #!/bin/bash
    for i in {1..254}
    do
      myhostname=`host 192.168.1.$i`
      if [ "$?" -eq 0 ]; then
        echo -ne $i
        echo -ne "\t"
        echo -ne "IN"
        echo -ne "\t"
        echo -ne "PTR"
        echo -ne "\t"
        echo `echo $myhostname | cut -d " " -f 5`
      fi
    done
    I use this to generate rdns (reverse dns) files for my bind9 name server:
    Code:
    bash rdns.sh >> /etc/bind/db.192
    I still need to put the correct lines in at the top of the db.192 file (from db.empty), but it accomplishes most of what I am after.

    Normally, I would not post to an older thread, but it appears many people are landing here, and still looking for an answer.
    Last edited by damormino; September 27th, 2011 at 02:44 AM.

  8. #8
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Reverse DNS in BASH script

    Quote Originally Posted by paul.maddox View Post
    Hi guys,

    What's the best way to do a reverse dns lookup in a bash script?

    I know you can use host, dig and nslookup but they all seem to return extra information and I don't really want to have to grep/gawk/sed the output to get what I need.

    The best I've come up with so far is:

    HOST=$(host $IP | cut -d " " -f 5);


    There must be a nicer way!
    have you tried using:
    Code:
    $ getent hosts <host>
    Regards,

    Karlson

  9. #9
    Join Date
    May 2006
    Beans
    10

    Re: Reverse DNS in BASH script

    Just found this while I was searching for something else.

    How about
    Code:
    dig x +short domain.com

  10. #10

    Re: Reverse DNS in BASH script

    Code:
    host domain.com
    Windows assumes the user is an idiot.
    Linux demands proof.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •