2. Sort
3. Convert back
python3 -c 'import ipaddress, sys; print("\n".join(sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
It takes the IP addresses to be sorted on the command line.Or, re-abbreviating them by removing zeroes and attempting to use :: where possible:
python3 -c 'import ipaddress, sys; print("\n".join(str(ipaddress.IPv6Address(y)) for y in sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
Both of these versions will crash if given input that isn't syntactically valid as an IPv6 address.Probably better in this case would be to have a small program that expands IPv6 addresses that you could then pipe into sort. That would be more in-keeping with the Unix Way™.
I went and wrote this and then realized you'd need two utilities. One to expand the addresses, and one to minify the addresses, so your pipeline would be:
cat ipv6-addrs.txt | ipv6expand | sort -V | ipv6minify
vs cat ipv6-addrs.txt | sort -6
The unix philosphy is do one thing and do it well. Ideally sort does sorting very well, including sorting of ipv6 addresses. Ergo, sort should handle ipv6 address sorting. 1) convert to a non-space-zeros-compressing hex string
2) sort on the hex string
3) convert back through inet_ntop()
Only a minor variant needed to deal with prefix/len sort order.This post also highlights a major thing I discovered when deploying and using IPv6, which is that you don't "Lift and shift" IPv4 to IPv6.
This is one of the reason its hard to deploy, because people cannot use the same IPv4 concepts to IPv6. For unknown reason they do, they will find the same problem they had with v4.
Which some also do not sort nicely. Specially when combined.
0177.1 (octal, 2 stripped 0s, decimal one)
017700000001 (octal)
2130706433 (decimal)
127.0.0x1
0x7f.0x0.0x0.0x1
127.000.000.001
"sorting by ip" for example is eschewed in favor of router advertisements where hosts pick their own subnet delegations.
one of the ways you track all of this is using a dhcp6 server with dns updates turned on. the subnet delegation is picked up by the client, which in turn pings dhcp6 and reports its IP for update in the DNS server.
If only it were that easy.
I'm in a moderate-to-high technology area in the grand scheme. They laid fiber here roughly two years ago, and lit it about a year ago, whereupon I immediately subscribed.
They don't offer IPv6. At all. That should tell you just how unimportant IPv6 is perceived as outside of its core cadre of proponents. Note that this is not a commentary on how important it actually is; just on perception.
In short, nobody cares.
https://www.google.com/intl/en/ipv6/statistics.html#tab=per-...
Cloudflare reports slightly lower 40% IPv6 adoption globally https://radar.cloudflare.com/adoption-and-usage
A lot of the holdouts on IPv4-only traffic are corporate traffic. A lot of corporate traffic acts like it wants a separate internet anyway, and a lot of companies hoarded IPv4 spaces for long enough that they don't see the increasing costs of IPv4 addresses hit their bottom lines yet enough to care. It's a fascinating dynamic. Maybe corporations will just buy the remaining IPv4 internet entirely and keep it as their own separate but not equal network.
Ip version is not a selling point, unless you are on the server.
This was a few years ago, but the majority of Danish ISP wasn't offering IPv6, because "there's is no demand from customers". Well, the customers also aren't demanding IPv4, they are demanding internet access. How you deliver it is not interesting to anyone but a small niche segment of the market. If you could somehow make it work over IPX, then 95% of customers would be fine with that, it's not something they care about.
(Ultimately UUID v1 was full of mistakes that we all will keep paying for.)
For the record it is Java with the worst possible UUID sort algorithm, sorting parts of it as signed numbers: https://devblogs.microsoft.com/oldnewthing/20190913-00/?p=10...
(Friends don't let friends use Java. /s)
$ echo "2a00:1450:400e:80c::200e
2606:4700::6810:85e5
2a03:2880:f145:82:face:b00c:0:25de
2603:1030:c02:8::14
2603:1030:20e:3::23c
2603:1030:b:3::152
2603:1020:201:10::10f
2603:1010:3:3::5b" | ch --structure 'ip IPv6' --query "SELECT ip FROM table ORDER BY ip"
2603:1010:3:3::5b
2603:1020:201:10::10f
2603:1030:b:3::152
2603:1030:20e:3::23c
2603:1030:c02:8::14
2606:4700::6810:85e5
2a00:1450:400e:80c::200e
2a03:2880:f145:82:face:b00c:0:25de
prompt: create a bash command to sort a text file of ipv6 adresses per line. sort numerically (128 bit representation) but keep the full line as originally formatted
result:
while read -r ip; do
printf '%032x %s\n' \
"$(python3 -c 'import ipaddress,sys; print(int(ipaddress.IPv6Address(sys.argv[1])))' "$ip")" \
"$ip"
done < ipv6.txt | sort | cut -d' ' -f2-
It's just to demonstrate how easy it is to solve those problems without spending an unnecessary amount of time on them.
sargstuff•1mo ago
1) Regular expression / seamingly lost art of sprintf formatting are some methods to normalize 001:db8::2:1 to something usable for sorting aka 2001:db8:0000:0000:0000:0000:0002:0001. Perhaps restoring to rfc 5952 format when printing sorted results.
2) Modify hex to 'sortable utf-16 characters', modify back post sort[1]
3) avoid utf-8 / utf-16 issues, use relevant python libraries to handle ipv4/ipv6[2][3],[4]: ip2n < file | sort -n | n2ip
----------------------------
[0] : https://datatracker.ietf.org/doc/html/rfc5952
[1] : https://stackoverflow.com/questions/5797369/unix-sort-utilit...
[2] : https://stackoverflow.com/questions/75522231/how-to-sort-ipv...
[3] : https://www.geeksforgeeks.org/convert-ip-address-to-integer-...
[4] : https://ipfind.com/blog/how-to-convert-ip-address-to-integer...