The IPv4 address for the default route:
ip address show $(ip route | grep "^default " | head -n1 | grep -Po "(?<=dev )[^ ]+") | grep -Po "(?<=inet )[^ /]+"
The IPv6 address for the default route:
ip address show $(ip route | grep "^default " | head -n1 | grep -Po "(?<=dev )[^ ]+") | grep -Po "(?<=inet6 )[^ /]+"
These only require commands ip
and grep
with support for -P
and -o
. The head -1
is required because ip route
may show multiple default routes when system has complex enough network setup.
If you don't mind which IP is which, you can just do
ip route | grep -Po '(?<=src )[^ ]+'
or
hostname --all-ip-addresses