Skip to content

Instantly share code, notes, and snippets.

@lsowen
Last active March 16, 2020 17:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsowen/4447d916fd19cbb7fce4 to your computer and use it in GitHub Desktop.
Save lsowen/4447d916fd19cbb7fce4 to your computer and use it in GitHub Desktop.
Calculate Reverse ipv6 PTR Record in Bash (using awk and sed)
function reverseIp6 {
echo "$1" | awk -F: 'BEGIN {OFS=""; }{addCount = 9 - NF; for(i=1; i<=NF;i++){if(length($i) == 0){ for(j=1;j<=addCount;j++){$i = ($i "0000");} } else { $i = substr(("0000" $i), length($i)+5-4);}}; print}' | rev | sed -e "s/./&./g"
}
$(reverseIp6 "2001:db8:85a3::8a2e:370:7334")"ip6.arpa."
#Result: 4.3.3.7.0.7.3.0.e.2.a.8.0.0.0.0.0.0.0.0.3.a.5.8.8.b.d.0.1.0.0.2.ip6.arpa.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment