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

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

    If you know that 0.0.0.0 is your expected output, and will be at the beginning of the line, you could use the following in your script:

    IP=`netstat -rn | grep -e '^0\.0\.0\.0' | cut -d' ' -f2`
    

    then reference the variable ${IP}.

    It would be better to use awk instead of cut here... i.e.:

    IP=`netstat -rn | grep -e '^0\.0\.0\.0' | awk '{print $2}'`
    

提交回复
热议问题