Certainly! Below is an IPv6 version of the command that you can use to add a static route to a destination IPv6 address using the gateway IPv6 address obtained from the existing routing table.
ip -6 route add $(ip -6 route get "$IPV6" | \
sed -r '/ via [0-9a-fA-F:]\{1,48\}/{s/^\(.* via [0-9a-fA-F:]\{1,48\}\).*/\1/}' | \
head -n 1) 2>/dev/null
Let's break down the IPv6 version:
-
ip -6 route get "$IPV6": This command prints the routing table entry for the specified destination IPv6 address. The -6 option is used to specify IPv6.
-
| sed -r '/ via [0-9a-fA-F:]\{1,48\}/{s/^\(.* via [0-9a-fA-F:]\{1,48\}\).*/\1/}': This is a piped command that uses sed with the -r (extended regex) flag to extract the gateway IPv6 address from the output of the ip -6 route get command. The regular expression via [0-9a-fA-F:]\{1,48\} matches the "via" part and the variable-length sets of numbers and colons that follow it (the gateway IPv6 address). The s/// substitution command is used to replace the entire line with just the gateway IPv6 address.
-
| head -n 1: This command takes the first line of the output from the sed command, which is the gateway IPv6 address.
-
ip -6 route add $(...) 2>/dev/null: Finally, the output of the entire pipeline is passed to the ip -6 route add command, which adds a static route to the routing table. The 2>/dev/null part discards any error messages that might be printed.
Please note that the regular expression used in the sed command may need to be adjusted depending on the output of your specific ip command. Also, make sure to replace "$IPV6" with the actual destination IPv6 address you want to route to.