Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If we have this string (IP address):

How can I derive the (DNS reverse record form) from this string, so it will be shown like using a shell script?

share|improve this question
It has to be with the shell? – Braiam May 30 '14 at 22:07
Stop doing that – Michael Mrozek Jun 8 '14 at 8:30
With this amount of answers this should better be in codegolf ;) – tkausl Dec 6 '14 at 12:59
@tkausl, share it there if you want to :) – Networker Dec 6 '14 at 23:58

14 Answers 14

up vote 26 down vote accepted

You can do it with AWK. There are nicer ways to do it, but this is the simplest, I think.

echo '' | awk 'BEGIN{FS="."}{print $4"."$3"."$2"."$1".in-addr.arpa"}'

This will reverse the order of the IP address.

Just to save a few keystrokes, as Mikel suggested, we can further shorten the upper statement:

echo '' | awk -F . '{print $4"."$3"."$2"."$1".in-addr.arpa"}'


echo '' | awk -F. '{print $4"."$3"."$2"."$1".in-addr.arpa"}'


echo '' | awk -F. -vOFS=. '{print $4,$3,$2,$1,"in-addr.arpa"}'

AWK is pretty flexible. :)

share|improve this answer
If you want to save a few keystrokes, -F . should be equivalent to BEGIN{FS="."}. – Mikel May 30 '14 at 22:03
Thanks, it worked I will mark the question as answered – Networker May 30 '14 at 22:04

Just for curiosity value... using tac from GNU coreutils: given a variable ip in the form then

$(printf %s "$ip." | tac -s.)in-addr.arpa


$ ip=
$ rr=$(printf %s "$ip." | tac -s.)in-addr.arpa
$ echo "$rr"
share|improve this answer
+1 This is definitely my favorite answer here. Thanks for introducing me to tac! – Jonathon Reinhart May 31 '14 at 17:41
Thanks are also due to @StéphaneChazelas for the elegant printf edit (I originally posted an ugly echo -n) – steeldriver Jun 1 '14 at 17:07

If you want to use only shell (zsh, ksh93, bash), here's another way:

IFS=. read w x y z <<<''
printf '%d.%d.%d.%d.in-addr.arpa.' "$z" "$y" "$x" "$w"

Or in plain old shell:

echo '' | { IFS=. read w x y z; echo "$z.$y.$w.$x.in-addr.arpa."; }
share|improve this answer

Easily with Perl, thusly:

$ echo|perl -nle 'print join ".",reverse(split /\./,$_)'
share|improve this answer
Can be made even more compact: perl -F'\.' -lane '$,=".";print reverse @F' – Joseph R. May 30 '14 at 22:15

To round it out, Ruby:

ruby -r ipaddr -e 'puts IPAddr.new(ARGV.first).reverse'

Which also supports IPv6

share|improve this answer
you could take the "require" out of the script: ruby -r ipaddr -e 'puts ...' – glenn jackman Dec 5 '14 at 19:22

Through GNU sed,

sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\1.in-addr.arpa/g' file

It reverses any IPv4-address format.


$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\1.in-addr.arpa/g'

$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\1.in-addr.arpa/g'

$ echo '' | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\1.in-addr.arpa/g'

$ sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/\4.\3.\2.\1.in-addr.arpa/g' <<< ''
share|improve this answer

With zsh:

$ ip=
$ echo ${(j:.:)${(s:.:Oa)ip}}.in-addr.arpa

Those are variable expansion flags:

  • s:.:: split on .
  • Oa: reverse order the array
  • j:.:: join on .
share|improve this answer

Another possibility is to use the "dig" command line tool with the "-x" switch.

It actually does a request on the PTR entry, but if you filter on "PTR" it will show you one commented line (the request) and maybe some replies.

Using "dig" can be handy for a quick writing of the PTR name, without having to write a small script. Particularly if you need it interactively (to cut and paste the result). It works on IPv6 too.

share|improve this answer

Using Python’s standard library:

>>> ipaddress.ip_address('').reverse_pointer
share|improve this answer
This is in the 3.5 standard library and scheduled to be released September 2015. From a script you can do: python3.5 -c "import ipaddress; ipaddress.ip_address('').reverse_pointer" (all on one line) – Anthon Nov 24 '14 at 20:40

If you want it working with IPv6 as well, you can use dig -x.

For example:

$ dig -x | egrep '^;.*PTR$' | cut -c 2- | awk '{print $1}'

$ dig -x 2001:db8:dc61:2a61::1 | egrep '^;.*PTR$' | cut -c 2- | awk '{print $1}'
share|improve this answer

In Python

 a = ""
 import re
 m = re.search(r'(\d+)\.(\d+)\.(\d+)\.(\d+)',a)
 ip = m.group(4),m.group(3),m.group(2),m.group(1)
 '.'.join(ip) + ".in-addr.arpa"
share|improve this answer
Or more idiomatically, "{}.in-addr.arpa".format(".".join(reversed(a.split(".")))) in Python2.7 – iruvar Jun 8 '14 at 11:42
$ while read ip
while> do
while> n=( $(echo $ip) ) && echo "${n[4]}"'.'"${n[3]}"'.'"${n[2]}"'.'"${n[1]}"'.'"in-addr.arpa"
while> done

This way you can type in an address and hit return for your result.

share|improve this answer

With host command from dnsutils:

$ host -t ptr | cut -d' ' -f 2
share|improve this answer
IFS=. ; set -- ${0+}
printf %b. $4 $3 $2 $1 in-addr.arpa\\c

IFS=. ; printf %s\\n \
    in-addr.arpa ${0+} |    
sed '1!G;$s/\n/./gp;h;d'

IFS=. ; printf '[%b.] ' \
    ${0+]PPPPP\\c} |dc
echo in-addr.arpa
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.