Comments on: IPv6 address normalization https://backreference.org/2013/03/01/ipv6-address-normalization/ Proudly uncool and out of fashion Thu, 14 Aug 2014 12:28:16 +0000 hourly 1 https://wordpress.org/?v=5.8.2 By: waldner https://backreference.org/2013/03/01/ipv6-address-normalization/#comment-25116 Thu, 14 Aug 2014 12:28:16 +0000 http://backreference.org/?p=3710#comment-25116 In reply to Imran.

I think we have a terminology misunderstanding here. The address you're trying to "normalize" (fd12:ba74:4ba8:2e::3) is already normalized according to the article and the RFC (see http://tools.ietf.org/html/rfc5952#page-10 for the details), thus it is correct that python outputs it unchanged. It's java that applies some other, different, formatting. If that's the one you need, then by all means go with java.

]]>
By: Imran https://backreference.org/2013/03/01/ipv6-address-normalization/#comment-25115 Thu, 14 Aug 2014 11:38:59 +0000 http://backreference.org/?p=3710#comment-25115 Thank you for reply.

Python approach is not normalizing all the formats, where Java approach does.
Example:
If I ask to normalize IP fd12:ba74:4ba8:2e::3, outputs are

Python: fd12:ba74:4ba8:2e::3
Java: fd12:ba74:4ba8:2e:0:0:0:3

Output by Java APIs seems following RFC.

normIP = java.net.InetAddress.getByName(IP).toString().substring(1)

]]>
By: waldner https://backreference.org/2013/03/01/ipv6-address-normalization/#comment-25100 Thu, 12 Jun 2014 18:45:59 +0000 http://backreference.org/?p=3710#comment-25100 In reply to Imran.

Your problem is one of bash syntax. For unknown/untold reasons, you're trying to put a complex command in a variable for later execution, something that looks completely unnecessary to me here. If you insist on doing that, please read this page: http://mywiki.wooledge.org/BashFAQ/050 where the topic is covered in detail and at least use one of the methods described there. You should also get into the habit of using quotes around your variables (http://mywiki.wooledge.org/Quotes), and $() instead of backticks for command substitution.

I'd rewrite your code as follows:

#!/bin/bash
fill(){
 python2 -c "import socket; print socket.inet_ntop(socket.AF_INET6, socket.inet_pton(socket.AF_INET6, '${1}'))"
}

IP="0:0:0:0:0:FFFF:204.152.189.116"
norm_ip=$(fill "$IP")
echo "$norm_ip"
]]>
By: Imran https://backreference.org/2013/03/01/ipv6-address-normalization/#comment-25099 Thu, 12 Jun 2014 16:05:46 +0000 http://backreference.org/?p=3710#comment-25099 Very informative and helping. Facing another issue now.
In a existing shell program I have to normalize IPs, doing something like below

#!/bin/bash
fill(){
convert="python -c \"import socket; print socket.inet_ntop(socket.AF_INET6, socket.inet_pton(socket.AF_INET6, '${IP}'))\"`
}

IP="0:0:0:0:0:FFFF:204.152.189.116"
fill
echo "command: $convert"
norm_ip=`$convert`
echo $norm_ip

Execution of this gives:

command: python -c "import socket; print socket.inet_ntop(socket.AF_INET6, socket.inet_pton(socket.AF_INET6, '0:0:0:0:0:FFFF:204.152.189.116'))"

SyntaxError: EOL while scanning string literal

]]>