Linux:获取网关
今天在微博(Linux命令行精选)上看到了一条关于获取网关的命令,很有意思。
所以,在自己的环境上试了下:
1 2 3 4 5 6 7 8 9 |
[root@oracle-db ~]# /sbin/route -n | grep "^0\.0\.0\.0" | awk '{ print $2 }' 192.168.56.2 [root@oracle-db ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.56.2 0.0.0.0 UG 0 0 0 eth0 [root@oracle-db ~]# |
如上:
首先,通过“route -n”获取路由表的全表。
然后,通过grep的正则,定位到以“0.0.0.0”起头的行,即网关“192.168.56.2”所在的那一行。
最后,通过awk的范围截取,取得第二列的数据,也就是“192.168.56.2”。
——————————————
Done。