linux下如何获取当前能连上网的那个ip地址

linux下怎么获取当前能连上网的那个ip地址
就是在多网卡的情况下,多ip的情况下,怎么判断是哪个网卡ip连接着外网啦
------解决思路----------------------
把其他网卡关了,剩一个,哪个能ping通外网,那个就是外网的iplinux下如何获取当前能连上网的那个ip地址
------解决思路----------------------
要是route没配好,ping死都出不去....
------解决思路----------------------
http://hawknotes.blogspot.com/2010/06/finding-your-external-ip-address.html



#! /bin/bash

# Determine the public IP address for your DHCP system by bootstrapping

#  calls to your gateway/router

# Requires "route," available in Ubuntu package net-tools, and
# nslookup, available in Ubuntu package dnstools

# Disclaimers:
# 1) This works with Verizon FIOS using the Actiontec MI424WR Router
#    If you have another system YMMV.
#
# 2) And, as you'll no doubt note, this is set up for a Debian system,
#    specifically Ubuntu 10.04 .  Package names and installation
#    procedures for other systems WILL vary.
#

###
# Start Here:
###

#
# Get Router internal Gateway IP
# Information found on
# http://www.cyberciti.biz/faq/how-to-find-gateway-ip-address/
# If route is not installed, run
# sudo apt-get install net-tools
# and make sure /sbin is in your $PATH
# Note "U" = up, and "G" = gateway

internalip=`route -n 
------解决思路----------------------
 grep UG 
------解决思路----------------------
 awk '{print $2}'`

echo Your Gateway\'s Internal IP Address is: $internalip

# Now we need to get the name of the router.  If you don't
#  have nslookup installed, run
# sudo apt-get install dnsutils

echo -n "Your Gateways's name is: "

routername=`nslookup $internalip 
------解决思路----------------------
 grep "name =" 
------解决思路----------------------
 awk '{print $4}'`

# On the Actiontec, at least, the name has an annoying "." at the end.
# Remove it:

routername=${routername%\.}

echo $routername

echo -n "Your Public IP Address is: "

nslookup $routername 
------解决思路----------------------
 grep Address 
------解决思路----------------------
 tail -1 
------解决思路----------------------
 awk '{print $2}'
------解决思路----------------------
这个问题有点复杂。
首先,如果只有一个链路可以通到外网的话,那么要看路由表的设置。如果路由表设置不正确,就连不上外网。一般的,就看default路由了,如果这个路由跟某个网卡上面的IP同一个网段,那么这个IP就是通向外网的那个IP了。

还有一种可能,就是有多个链路可以通到外网。其实我们每个PC,从某种角度来说,都是一个路由器,我们尅设置某个IP范围走一个路由,其它走default路由。这样的话,有可能你好几个网卡的IP都可以通到外网。

结论:你可以通过netstat -r命令,然后找出路由表里面跟网卡IP同一个网段的所有IP,就是可能通到外网的IP,但不能保证路由表设置就是正确的,所以还要进一步逐一证实