如何在给定目的地的 Linux 中获取默认网关?
我正在尝试使用目标 0.0.0.0
获取默认网关。
我使用了命令netstat -rn | grep 0.0.0.0 并返回此列表:
Destination Gateway Genmask Flags MSS Window irtt Iface
10.9.9.17 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
133.88.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 133.88.31.70 0.0.0.0 UG 0 0 0 eth0
我的目标是使用目标 0.0.0.0
(即 133.88.31.70
) ping 默认网关,但由于使用了grep
,这个返回一个列表。
如何仅获取默认网关? 我的 Bash 脚本需要它来确定网络连接是否已启动。
I'm trying to get the default gateway, using the destination 0.0.0.0
.
I used the command netstat -rn | grep 0.0.0.0
and it returned this list:
Destination Gateway Genmask Flags MSS Window irtt Iface
10.9.9.17 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
133.88.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 133.88.31.70 0.0.0.0 UG 0 0 0 eth0
My goal here is to ping the default gateway using the destination 0.0.0.0
, which is 133.88.31.70
, but this one returns a list because of using grep
.
How do I get the default gateway only? I need it for my Bash script to determine whether the network connection is up or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
另一个 Perl 的事情:
注意:在
ip
之前和default
之后使用这些反引号Another perl thing:
Note: use these backticks before
ip
and afterdefault
netstat -rn | 网络统计 0.0.0.0 | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0"
netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0"
如果您知道
0.0.0.0
是您的预期输出,并且位于行的开头,则可以在脚本中使用以下内容:然后引用变量
${IP}< /代码>。
最好使用 awk 而不是在这里剪切......即:
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:then reference the variable
${IP}
.It would be better to use awk instead of cut here... i.e.:
使用下面的命令:
use command below:
/sbin/route |egrep "^default" |cut -d' ' -f2-12 #and 'cut' 来品尝...
/sbin/route |egrep "^default" |cut -d' ' -f2-12 #and 'cut' to taste...
要获取默认网关的 NIC 名称,请使用以下命令:
To get the NIC name that it's the default gateway use this command:
ip 路由
iproute2 包中的命令可以选择路由,而无需使用awk
/grep
等进行选择。选择默认路由(可能从多个):
选择特定接口的下一跃点:
如果有多个默认网关,您可以选择选择哪一个作为到特定目标地址的下一跃点:
然后,您可以提取使用
sed
/awk
/grep
等读取值。这是一个使用bash
的read 的示例内置:
The
ip route
command from the iproute2 package can select routes without needing to useawk
/grep
, etc to do the selection.To select the default route (from possibly many):
To select the next hop for a particular interface:
In case of multiple default gateways, you can select which one gets chosen as the next hop to a particular destination address:
You can then extract the value using
sed
/awk
/grep
, etc. Here is one example usingbash
'sread
builtin:您可以使用
ip
命令获取默认网关,如下所示:You can get the default gateway using
ip
command like this:适用于任何 Linux:
works on any linux:
这个简单的 perl 脚本将为您完成。
基本上,我们运行 netstat,将其保存到 $ns。 然后找到以 0.0.0.0 开头的行。 然后正则表达式中的括号将其中的所有内容保存到 $1 中。 之后,只需打印出来即可。
如果它被称为 null-gw.pl,只需在以下命令上运行它:
或者如果您需要在 bash 表达式中使用它:
祝你好运。
This simple perl script will do it for you.
Basically, we run netstat, save it to $ns. Then find the line that starts off with 0.0.0.0. Then the parentheses in the regex saves everything inside it into $1. After that, simply print it out.
If it was called null-gw.pl, just run it on the command like:
or if you need it inside a bash expression:
Good luck.
我就是这样做的:
This is how I do it:
有关所有默认网关的列表,请使用mezgani的答案,此处重复(并稍作简化) :
如果您同时配置了多个网络接口,这将打印多个网关。 如果您想按名称选择单个已知网络接口(例如
eth1
),除了过滤^default
之外,只需搜索该接口即可。行:您可以制作一个脚本,将单个网络接口名称作为参数并打印关联的网关:
如注释中所述,这具有设置合理的退出代码的优点,这在更广泛的编程中可能很有用语境。
For a list of all default gateways, use mezgani's answer, duplicated (and slightly simplified) here:
If you have multiple network interfaces configured simultaneously, this will print multiple gateways. If you want to select a single known network interface by name (e.g.
eth1
), simply search for that in addition to filtering for the^default
lines:You can make a script that takes a single network-interface name as an argument and prints the associated gateway:
As noted in the comments, this has the advantage of setting a sensible exit-code, which may be useful in a broader programmatic context.
以下命令仅使用 bash 和 awk 返回 Linux 主机上的默认路由网关 IP:
如果您有多个默认网关,只要它们的指标不同(而且它们应该是......),这甚至应该可以工作。
The following command returns the default route gateway IP on a Linux host using only bash and awk:
This should even work if you have more than one default gateway as long as their metrics are different (and they should be..).
这里已经有很多答案了。 其中一些是非常特定于发行版的。 对于那些发现这篇文章寻找找到网关的方法,但不需要在代码/批量利用中使用它的人(就像我所做的那样)...尝试:
第一跳是您的默认网关。
There are a lot of answers here already. Some of these are pretty distro specific. For those who found this post looking for a way to find the gateway, but not needing to use it in code/batch utilization (as I did)... try:
the first hop is your default gateway.