Search code examples
linuxwindowsmacosgateway

How to get system default gateway address in Linux + windows + MAC


I have found a way to get default gateway in Linux with the following command:

grep -i "gateway" /etc/network/interfaces | awk print $2

Output:

192.168.14.1

This will not work if the system is in DHCP mode.

I need a way to get only 'gateway' which works on all platforms (Linux / Windows / Mac).


Solution

  • This will work for Linx(your host may be dhcp or static)

    route -n | cut -d" " -f10

    Output : x.x.x.x(gateway address)