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

前端 未结 15 2132
清酒与你
清酒与你 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: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}
    

提交回复
热议问题