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

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

    #!/bin/bash
    
    ##################################################################3
    # Alex Lucard
    # June 13 2013
    #
    # Get the gateway IP address from the router and store it in the variable $gatewayIP
    # Get the Router mac address and store it in the variable gatewayRouter
    # Store your routers mac address in the variable homeRouterMacAddress
    #
    
    # If you need the gateway IP uncomment the next line to get the gateway address and store it in the variable gateWayIP
    # gatewayIP=`sudo route -n | awk '/^0.0.0.0/ {print $2}'` 
    
    homeRouterMacAddress="20:aa:4b:8d:cb:7e" # Store my home routers mac address in homeRouterMac.
    gatewayRouter=`/usr/sbin/arp -a`
    
    # This if statement will search your gateway router for the mac address you have in the variable homeRouterMacAddress
    if `echo ${gatewayRouter} | grep "${homeRouterMacAddress}" 1>/dev/null 2>&1`
    then
      echo "You are home"
    else
      echo "You are away"
    fi
    

提交回复
热议问题