Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

gateway

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 returned this list:

**Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface<br> 10.9.9.17       0.0.0.0         255.255.255.255 UH        0 0          0 tun0<br> 133.88.0.0      0.0.0.0         255.255.0.0     U         0 0          0 eth0<br> 0.0.0.0         133.88.31.70    0.0.0.0         UG        0 0          0 eth0**<br> 

My goal here is to ping the default gateway using destination 0.0.0.0; thus, that is 133.88.31.70; but this one returns a list because of using grep.

How do i get the default gateway only? I will need it for my bash script to identify if net connection is up or not.

like image 957
Suezy Avatar asked Jul 30 '09 05:07

Suezy


People also ask

How do I find my default gateway Linux?

When Command Prompt is open, type the following command: ipconfig | findstr /i "Gateway" (You can copy & paste it in the command prompt; just right-click anywhere in the command prompt window and select Paste.) In this example, your default gateway (router) IP address is 192.168. 1.1.

How do I find the default gateway route?

In the "Open:" field, type cmd , and then click OK. This will open the command prompt. At the prompt, enter ipconfig . This will display your network information, including your default gateway.

What is default gateway IP Linux?

0.1 and 192.168. 2.254 are the default gateway IP address. The gateway with lowest Metric is the first to be searched and used as the default gateway.


1 Answers

You can get the default gateway using ip command like this:

IP=$(/sbin/ip route | awk '/default/ { print $3 }') echo $IP 
like image 134
Ali Mezgani Avatar answered Sep 22 '22 05:09

Ali Mezgani