2a01:488:66:1000:523:f116:0:1
指定されたIPv6アドレス(またはなど)に基づいてdjbdns(dbndns)構成ファイルを更新したいと思います::1
。
dbndnsには、拡張IPv6アドレスが必要2a010488006610000523f11600000001
です2a01:488:66:1000:523:f116:0:1
。
このようなIPv6アドレスを拡張する最も簡単な方法は何ですか?
を使用sipcalc
すると、それができる場合があります。それはあなたが必要とするよりも多くの情報を提供しますが、それを少しgrep
解決cut
することができます:-)
$ EXPANDED=`sipcalc 2001::1 | fgrep Expanded | cut -d '-' -f 2`
$ echo $EXPAND
2001:0000:0000:0000:0000:0000:0000:0001
参考までに、これは次の完全な出力ですsipcalc
。
$ sipcalc 2001::1
-[ipv6 : 2001::1] - 0
[IPV6 INFO]
Expanded Address - 2001:0000:0000:0000:0000:0000:0000:0001
Compressed address - 2001::1
Subnet prefix (masked) - 2001:0:0:0:0:0:0:1/128
Address ID (masked) - 0:0:0:0:0:0:0:0/128
Prefix address - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
Prefix length - 128
Address type - Aggregatable Global Unicast Addresses
Network range - 2001:0000:0000:0000:0000:0000:0000:0001 -
2001:0000:0000:0000:0000:0000:0000:0001
私は最近、シェル間で移植可能で、openwrt などのプラットフォームで動作する依存関係のないソリューションが必要でした。次のスニペットを思いつきました:
# helper to convert hex to dec (portable version)
hex2dec(){
[ "$1" != "" ] && printf "%d" "$(( 0x$1 ))"
}
# expand an ipv6 address
expand_ipv6() {
ip=$1
# prepend 0 if we start with :
echo $ip | grep -qs "^:" && ip="0${ip}"
# expand ::
if echo $ip | grep -qs "::"; then
colons=$(echo $ip | sed 's/[^:]//g')
missing=$(echo ":::::::::" | sed "s/$colons//")
expanded=$(echo $missing | sed 's/:/:0/g')
ip=$(echo $ip | sed "s/::/$expanded/")
fi
blocks=$(echo $ip | grep -o "[0-9a-f]\+")
set $blocks
printf "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n" \
$(hex2dec $1) \
$(hex2dec $2) \
$(hex2dec $3) \
$(hex2dec $4) \
$(hex2dec $5) \
$(hex2dec $6) \
$(hex2dec $7) \
$(hex2dec $8)
}
圧縮するこの機能もあります
# returns a compressed ipv6 address under the form recommended by RFC5952
compress_ipv6() {
ip=$1
blocks=$(echo $ip | grep -o "[0-9a-f]\+")
set $blocks
# compress leading zeros
ip=$(printf "%x:%x:%x:%x:%x:%x:%x:%x\n" \
$(hex2dec $1) \
$(hex2dec $2) \
$(hex2dec $3) \
$(hex2dec $4) \
$(hex2dec $5) \
$(hex2dec $6) \
$(hex2dec $7) \
$(hex2dec $8)
)
# prepend : for easier matching
ip=:$ip
# :: must compress the longest chain
for pattern in :0:0:0:0:0:0:0:0 \
:0:0:0:0:0:0:0 \
:0:0:0:0:0:0 \
:0:0:0:0:0 \
:0:0:0:0 \
:0:0; do
if echo $ip | grep -qs $pattern; then
ip=$(echo $ip | sed "s/$pattern/::/")
# if the substitution occured before the end, we have :::
ip=$(echo $ip | sed 's/:::/::/')
break # only one substitution
fi
done
# remove prepending : if necessary
echo $ip | grep -qs "^:[^:]" && ip=$(echo $ip | sed 's/://')
echo $ip
}
それらを組み合わせて、特定の入力が ipv6 であるかどうかをテストできます
# a valid ipv6 is either the expanded form or the compressed one
is_ipv6(){
expanded="$(expand_ipv6 $1)"
[ "$1" = "$expanded" ] && return 0
compressed="$(compress_ipv6 $expanded)"
[ "$1" = "$compressed" ] && return 0
return 1
}
これが役立つことを願っています!これらのスニペットはhttps://github.com/chmduquesne/wg-ipから取得されます。バグを見つけたら、貢献してください!
これで大丈夫ですか?
kent$ echo "2a01:488:66:1000:523:f116:0:1"|awk -F: '{for(i=1;i<=NF;i++)x=x""sprintf ("%4s", $i);gsub(/ /,"0",x);print x}'
2a010488006610000523f11600000001