【发布时间】:2012-05-15 03:32:54
【问题描述】:
我找到了一个帖子,可以满足我的需要,但在 Windows 上:
Discovering public IP programmatically
> tracert -d www.yahoo.com
`Tracing route to www-real.wa1.b.yahoo.com [69.147.76.15]
over a maximum of 30 hops:`
1 <1 ms <1 ms <1 ms 192.168.14.203
2 * * * Request timed out.
3 8 ms 8 ms 9 ms 68.85.228.121
4 8 ms 8 ms 9 ms 68.86.165.234
5 10 ms 9 ms 9 ms 68.86.165.237
6 11 ms 10 ms 10 ms 68.86.165.242
The 68.85.228.121 is a Comcast (my provider) router. We can ping that:
ping -r 9 68.85.228.121 -n 1
Pinging 68.85.228.121 with 32 bytes of data:
Reply from 68.85.228.121: bytes=32 time=10ms TTL=253
Route: 66.176.38.51 ->
68.85.228.121 ->
68.85.228.121 ->
192.168.14.203
Voila! The 66.176.38.51 is my public IP.
这个(第三个)答案显示了一种获取我的 ISP 的 IP,然后使用 ping 来获取我的 IP 的方法。
它在 Linux 上未经修改就无法工作。 Traceroute 可以代替 tracert 工作,但是因为它的输出是不可预测的,所以我不知道如何解析它。
我做到了
IP="$(traceroute -d www.yahoo.com | grep ' 2 ' | sed -e 's/.*(\(.*\)).*/\1/')"
但是 grep 是(很差)硬编码的。我没有看到如何让 ping 像示例中那样工作。
我们将不胜感激。
【问题讨论】:
-
这将是一种非常不可靠的发现您的公共 IP 地址的方法。无论如何,你需要它做什么?
-
我想让它成为一个 bash 函数。我最初的用途是在一个脚本中,当指定端口在打开和关闭之间更改状态时通知我,我需要的不仅仅是本地 ip 才能获得真正的答案。
-
@nikhil ifconfig 在连接到电缆调制解调器的 WRT54GL 上为 wlan0 返回 inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0。它无法让我通过我的路由器。
标签: bash networking ip