How do i get the default gateway in LINUX given the destination?

前端 未结 15 2131
清酒与你
清酒与你 2020-12-13 07:02

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

相关标签:
15条回答
  • 2020-12-13 07:40

    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.

    0 讨论(0)
  • 2020-12-13 07:40

    /sbin/route |egrep "^default" |cut -d' ' -f2-12 #and 'cut' to taste...

    0 讨论(0)
  • 2020-12-13 07:41

    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}
    
    0 讨论(0)
提交回复
热议问题