我正在尝试使用目标
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 脚本需要它来确定网络连接是否已启动。
您可以使用
ip
命令获取默认网关,如下所示:
IP=$(/sbin/ip route | awk '/default/ { print $3 }')
echo $IP
包中的
ip route
命令可以选择路由,而不需要使用awk
/grep
等来进行选择。
选择默认路由(可能有多个):
$ 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
的 read
内置函数的示例:
$ read _ _ gateway _ < <(ip route list match 0/0); echo "$gateway"
172.28.206.254
适用于任何 Linux:
route -n|grep "UG"|grep -v "UGH"|cut -f 10 -d " "
这个简单的 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)
祝你好运。
我就是这样做的:
#!/bin/sh
GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p")
echo ${GATEWAY_DEFAULT}
有关所有默认网关的列表,请使用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}"
如评论中所述,这样做的优点是设置合理的退出代码,这在更广泛的编程环境中可能很有用。
traceroute www.google.com
第一跳是您的默认网关。
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)
如果您有多个默认网关,只要它们的指标不同(而且它们应该是..),这甚至应该有效。
$return = (split(" ", `ip route | grep default`))[2];<br>
注意:在
ip
之前和
default
之后使用这些反引号
#!/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
0.0.0.0
是您的预期输出,并且将位于行的开头,您可以在脚本中使用以下内容:
IP=`netstat -rn | grep -e '^0\.0\.0\.0' | cut -d' ' -f2`
然后引用变量
${IP}
。最好使用 awk 而不是在这里剪切......即:
IP=`netstat -rn | grep -e '^0\.0\.0\.0' | awk '{print $2}'`
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}'
netstat -rn | grep UG | awk '{print $8}'