38

我正在尝试使用目标获取默认网关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>

我的目标是使用 destination ping 默认网关0.0.0.0;因此,即133.88.31.70; 但是这个返回一个列表,因为使用grep.

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

4

15 回答 15

60

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

IP=$(/sbin/ip route | awk '/default/ { print $3 }')
echo $IP
于 2009-08-04T08:59:23.523 回答
55

iproute2包中的ip route命令可以选择路由,而无需使用/等来进行选择。awkgrep

选择默认路由(可能有很多)

$ ip -4 route show default  # use -6 instead of -4 for ipv6 selection.
default via 172.28.206.254 dev wlp0s20f3 proto dhcp metric 600

为特定接口选择下一跳

$ ip -4 route list type unicast dev eth0 exact 0/0  # Exact specificity
default via 172.29.19.1 dev eth0

在多个默认网关的情况下,您可以选择将哪个网关选为到特定目标地址的下一跳。

$ ip route get $(dig +short google.com | tail -1)
173.194.34.134 via 172.28.206.254 dev wlan0  src 172.28.206.66 
    cache

然后,您可以使用sed/ awk/grep等提取值。这是使用bash'sread内置函数的一个示例。

$ read _ _ gateway _ < <(ip route list match 0/0); echo "$gateway"
172.28.206.254
于 2013-04-12T13:52:59.060 回答
8

适用于任何Linux:

route -n|grep "UG"|grep -v "UGH"|cut -f 10 -d " "
于 2014-02-23T12:57:24.570 回答
4

这个简单的 perl 脚本将为您完成。

#!/usr/bin/perl

$ns = `netstat -nr`;

$ns =~ m/0.0.0.0\s+([0-9]+.[0-9]+.[0-9]+.[0-9]+)/g;

print $1

基本上,我们运行 netstat,将其保存到 $ns。然后找到以 0.0.0.0 开头的行。然后正则表达式中的括号将其中的所有内容保存到 $1 中。之后,只需将其打印出来。

如果它被称为 null-gw.pl,只需在以下命令上运行它:

perl null-gw.pl

或者如果您在 bash 表达式中需要它:

echo $(perl null-gw.pl)

祝你好运。

于 2009-07-30T06:00:07.547 回答
3

我就是这样做的:

#!/bin/sh
GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p")
echo ${GATEWAY_DEFAULT}
于 2012-07-23T23:50:08.083 回答
3

有关所有默认网关的列表,请使用mezgani 的答案,在此处重复(并略微简化):

/sbin/ip route | awk '/^default/ { print $3 }'

如果您同时配置了多个网络接口,这将打印多个网关。如果您想按名称(例如 )选择单个已知网络接口,除了过滤行之外eth1,只需搜索它:^default

/sbin/ip route |grep '^default' | awk '/eth1/ {print $3}'

您可以制作一个脚本,将单个网络接口名称作为参数并打印关联的网关:

#!/bin/bash
if [[ $# -ne 1 ]]; then
    echo "ERROR: must specify network interface name!" >&2
    exit 1
fi
# The third argument of the 'default' line associated with the specified
# network interface is the Gateway.
# By default, awk does not set the exit-code to a nonzero status if a
# specified search string is not found, so we must do so manually.
/sbin/ip route | grep '^default' | awk "/$1/ {print \$3; found=1} END{exit !found}"

正如评论中所指出的,这具有设置合理退出代码的优势,这在更广泛的编程环境中可能很有用。

于 2016-04-25T21:53:13.840 回答
2

这里已经有很多答案了。其中一些是非常特定于发行版的。对于那些发现这篇文章正在寻找一种找到网关的方法,但不需要在代码/批处理利用中使用它(就像我一样)的人......尝试:

traceroute www.google.com

第一跳是您的默认网关。

于 2014-12-26T22:12:24.460 回答
2

以下命令仅使用 bash 和 awk 返回 Linux 主机上的默认路由网关 IP:

printf "%d.%d.%d.%d" $(awk '$2 == 00000000 && $7 == 00000000 { for (i = 8; i >= 2; i=i-2) { print "0x" substr($3, i-1, 2) } }' /proc/net/route)

如果您有多个默认网关,只要它们的指标不同(它们应该是..),这甚至应该有效。

于 2018-09-27T05:17:29.847 回答
1
#!/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
于 2013-06-14T04:38:40.387 回答
1

另一个 perl 事情:

$return = (split(" ", `ip route | grep default`))[2];<br>

注意:在前后使用这些反ip引号default

于 2011-01-02T00:33:58.817 回答
1

网络统计-rn | grep 0.0.0.0 | awk '{打印 $2}' | grep -v "0.0.0.0"

于 2013-01-08T05:36:10.170 回答
0

使用以下命令:

route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*[ \t]\+0\.0\.0\.0[ \t]\+[^ \t]*G[^ \t]*[ \t]' | awk '{print $2}'
于 2012-02-08T04:51:38.973 回答
0

/sbin/route |egrep "^default" |cut -d' ' -f2-12 #and 'cut' to taste...

于 2013-02-01T18:08:35.590 回答
0

如果您知道这0.0.0.0是您的预期输出,并且将在行首,您可以在脚本中使用以下内容:

IP=`netstat -rn | grep -e '^0\.0\.0\.0' | cut -d' ' -f2`

然后引用变量${IP}

在这里使用 awk 而不是 cut 会更好......即:

IP=`netstat -rn | grep -e '^0\.0\.0\.0' | awk '{print $2}'`
于 2009-08-04T07:40:19.563 回答
0

要获取作为默认网关的 NIC 名称,请使用以下命令:

netstat -rn | grep UG | awk '{print $8}'
于 2020-06-16T17:35:34.623 回答