macos 如何在 Mac OSX 中获取默认网关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6782658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get default gateway in Mac OSX
提问by SThomas
I need to retrieve the default gateway on a Mac machine. I know that in Linux route -n will give an output from which I can easily retrieve this information. However this is not working in Mac OSX(Snow Leopard).
我需要在 Mac 机器上检索默认网关。我知道在 Linux 中 route -n 会给出一个输出,我可以从中轻松检索这些信息。但是,这在 Mac OSX(雪豹)中不起作用。
I also tried netstat -nr | grep 'default'
, but I was hoping for a cleaner output like that produced by route -n
in Linux/Unix. netstat -nr
lists all the interfaces and the default gateway for them.
我也尝试过netstat -nr | grep 'default'
,但我希望有一个像route -n
Linux/Unix 中那样更干净的输出。netstat -nr
列出所有接口和它们的默认网关。
Any kind of suggestion or a hint in the right direction will be appreciated.
任何类型的建议或正确方向的提示将不胜感激。
回答by Humber
You can try with:
您可以尝试:
route -n get default
It is not the same as GNU/Linux's route -n
(or even ip route show
) but is useful for checking the default route information.
Also, you can check the route that packages will take to a particular host. E.g.
它与 GNU/Linux route -n
(甚至ip route show
)不同,但对于检查默认路由信息很有用。此外,您还可以检查包裹到达特定主机的路线。例如
route -n get www.yahoo.com
The output would be similar to:
输出将类似于:
route to: 98.137.149.56
destination: default
mask: 128.0.0.0
gateway: 5.5.0.1
interface: tun0
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
0 0 0 0 0 0 1500 0
IMHO netstat -nr
is what you need. Even MacOSX's Network utility app(*) uses the output of netstat to show routing information.
恕我直言netstat -nr
,这就是你所需要的。甚至 MacOSX 的网络实用程序应用程序 (*) 也使用 netstat 的输出来显示路由信息。
I hope this helps :)
我希望这有帮助 :)
(*) You can start Network utility with open /Applications/Utilities/Network\ Utility.app
(*) 您可以使用以下命令启动网络实用程序 open /Applications/Utilities/Network\ Utility.app
回答by Prabhu
For getting the list of ip addresses associated, you can use netstatcommand
要获取关联的 ip 地址列表,您可以使用netstat命令
netstat -rn
This gives a long list of ip addresses and it is not easy to find the required field. The sample result is as following:
这给出了一长串 ip 地址,并且不容易找到所需的字段。示例结果如下:
Routing tables
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 192.168.195.1 UGSc 17 0 en2
127 127.0.0.1 UCS 0 0 lo0
127.0.0.1 127.0.0.1 UH 1 254107 lo0
169.254 link#7 UCS 0 0 en2
192.168.195 link#7 UCS 3 0 en2
192.168.195.1 0:27:22:67:35:ee UHLWIi 22 397 en2 1193
192.168.195.5 127.0.0.1 UHS 0 0 lo0
More result is truncated.......
The ip address of gateway is in the first line; one with default at its first column.
网关的ip地址在第一行;第一列有默认值。
To display only the selected lines of result, we can use grepcommand along with netstat
要仅显示选定的结果行,我们可以使用grep命令和netstat
netstat -rn | grep 'default'
This command filters and displays those lines of result having default. In this case, you can see result like following:
此命令过滤并显示那些具有默认值的结果行。在这种情况下,您可以看到如下结果:
default 192.168.195.1 UGSc 14 0 en2
If you are interested in finding only the ip address of gateway and nothing else you can further filter the result using awk. The awk command matches pattern in the input result and displays the output. This can be useful when you are using your result directly in some program or batch job.
如果您只想查找网关的 ip 地址而没有其他内容,您可以使用awk进一步过滤结果。awk 命令匹配输入结果中的模式并显示输出。当您在某些程序或批处理作业中直接使用结果时,这会很有用。
netstat -rn | grep 'default' | awk '{print }'
The awk command tells to match and print the second columnof the result in the text. The final result thus looks like this:
awk 命令告诉匹配并打印文本中结果的第二列。因此,最终结果如下所示:
192.168.195.1
In this case, netstat displays all result, grep only selects the line with 'default' in it, and awk further matches the pattern to display the second column in the text.
在这种情况下,netstat 显示所有结果,grep 只选择其中包含 'default' 的行,而 awk 进一步匹配模式以显示文本中的第二列。
You can similarly use route -n get default command to get the required result. The full command is
您可以类似地使用 route -n get default 命令来获取所需的结果。完整的命令是
route -n get default | grep 'gateway' | awk '{print }'
These commands work well in linux as well as unix systems and MAC OS.
这些命令在 linux 以及 unix 系统和 MAC OS 中运行良好。
回答by Concertogr
The grep utility is not needed. Awk can do it all:
不需要 grep 实用程序。awk 可以做到这一切:
netstat -rn | awk '/default/ {print }'
192.168.128.1
Note that if you have something like Parallels (or a VPN, or both) running, you may see two or more default routing entries - it will be true if you use the 'grep' suggestion above, too.
请注意,如果您正在运行 Parallels(或 VPN,或两者)之类的东西,您可能会看到两个或多个默认路由条目 - 如果您也使用上面的“grep”建议,这将是正确的。
netstat -rn | awk '/default/ {print }'
192.168.128.1
link#12
and
和
netstat -rn | awk '/default/ {print }'
utun1
192.168.128.1
link#12
To set a variable (_default) for further use (assuming only one entry for 'default') .....
设置一个变量 (_default) 以供进一步使用(假设只有一个条目用于“默认”).....
_default=$( netstat -rn inet | awk '/default/ {print }' ) # I prefer $( ... ) over back-ticks
In the case of multiple default routes use:
在多个默认路由的情况下使用:
netstat -rn | awk '/default/ {if ( index(, "en") > 0 ){print } }'
192.168.128.1
These examples tested in Mavericks Terminal.app and are specific to OSX only. For example, other *nix versions frequently use 'eth' for ethernet/wireless connections, not 'en'. This is also only tested with ksh. Other shells may need a slightly different syntax.
这些示例在 Mavericks Terminal.app 中进行了测试,并且仅适用于 OSX。例如,其他 *nix 版本经常使用 'eth' 进行以太网/无线连接,而不是 'en'。这也仅使用 ksh 进行了测试。其他 shell 可能需要稍微不同的语法。
回答by Robert Northard
I would use something along these lines...
我会沿着这些路线使用一些东西......
netstat -rn | grep "default" | awk '{print }'
回答by James Nick
Using System Preferences:
使用系统偏好设置:
Step 1: Click the Apple icon (at the top left of the screen) and select System Preferences.
步骤 1:单击 Apple 图标(位于屏幕左上角)并选择系统偏好设置。
Step 2: Click Network.
第二步:点击网络。
Step 3: Select your network connection and then click Advanced.
步骤 3:选择您的网络连接,然后单击高级。
Step 4: Select the TCP/IP tab and find your gateway IP address listed next to Router.
步骤 4:选择 TCP/IP 选项卡并找到路由器旁边列出的网关 IP 地址。