I\'m trying to get the default gateway, using the destination 0.0.0.0
I used this command: netstat -rn | grep 0.0.0.0
And it return
There are a lot of answers here already. Some of these are pretty distro specific. For those who found this post looking for a way to find the gateway, but not needing to use it in code/batch utilization (as I did)... try:
traceroute www.google.com
the first hop is your default gateway.
/sbin/route |egrep "^default" |cut -d' ' -f2-12 #and 'cut' to taste...
This is how I do it:
#!/bin/sh
GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p")
echo ${GATEWAY_DEFAULT}