如何在给定目的地的情况下在LINUX中获取默认网关? [英] How do i get the default gateway in LINUX given the destination?

查看:114
本文介绍了如何在给定目的地的情况下在LINUX中获取默认网关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用目标0.0.0.0

我使用了以下命令:netstat -rn | grep 0.0.0.0

它返回了此列表:

**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>

我的目标是使用目标0.0.0.0对默认网关执行ping操作; 因此,这就是133.88.31.70;但这是由于使用grep而返回的列表.

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.

如何仅获取默认网关?我的bash脚本需要它来识别网络连接是否正常.

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.

推荐答案

您可以使用ip命令获取默认网关,如下所示:

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

IP=$(/sbin/ip route | awk '/default/ { print $3 }')
echo $IP

这篇关于如何在给定目的地的情况下在LINUX中获取默认网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆