How can I get my public IP address from the command line, if I am behind a router?



24 down vote accepted

Assuming your system has 2 ethernet devices, eth0 and eth1 and eth0 is connected to your LAN, say IPs 192.168.1.X and your eth1 device is connected to your ISP (WAN) you're going to want to use the following ifconfig command to get your IP for the WAN side.

NOTE: The 1st 2 ways assume that you're running them against a computer that has 2 ethernet devices and that one of them is connected to your ISP (cable modem and/or DSL modem). In this scenario the ethernet device (eth1) will be configured with your IP address on the internet (WAN IP).

1st way

                          +------------------------+
  +--------+    WAN IP    |   Computer that wants  |  LAN IP
  |Internet|--------------|     to know WAN IP     |------------
  +--------+  54.234.1.33 | +------+      +------+ | 192.168.1.1
                          +-| eth1 |------| eth0 |-+
                            +------+      +------+

% ifconfig eth1 | awk '/inet / { print $2 }' | sed -e s/addr://
54.234.1.33

You can also use the ip command.

% ip addr show eth1|grep inet|awk '{print $2}' | sed 's#/.*##'
54.234.1.33

2nd way

If you need to find this out from a system that sits only on the LAN you could setup a passphrase-less ssh key and add it to an account on your LAN machine so that it could remotely access the system with the WAN access like so.

                                                            +----------------+
  +--------+    WAN IP      +-------------+      LAN IP     | Computer that  |
  |Internet|----------------|remote-server|-----------------| wants to know  |
  +--------+  54.234.1.33  +----+-----+----+  192.168.1.x  +----+ WAN IP     |
                           |eth1|     |eth0|               |eth0|------------+
                           +----+     +----+               +----+

% ssh ruser1@remote-server "ifconfig eth1 | awk '/inet / { print \$2 }' | sed -e s/addr://"
54.234.1.33

3rd way

If you're unable to ssh into the box that has WAN access and you're using a home router/switch such as a Linksys or Netgear box. You may be able to get the IP from that device via a HTTP status page. I've done this in the past as well, something similar to what's described in this whatismyip.com forum post.

                                                               192.168.1.2
                                                            +----------------+
  +--------+    WAN IP      +-------------+      LAN IP     | Computer that  |
  |Internet|----------------|router/switch|-----------------| wants to know  |
  +--------+  54.234.1.33   +-------------+   192.168.1.x  +----+ WAN IP     |
                              192.168.1.1                  |eth0|------------+
                                                           +----+

# something like this....

% wget -q -O - http://<username>:<password>@192.168.1.1/Status_Router.asp | grep "ipaddress" | cut -d" " -f2

NOTE: This approach is highly dependent on which router/switch you have, whether it's a Linksys, Netgear, etc. brand. Each will have their own unique page with the WAN IP on it.

4th way

Sending a query against an external internet site which will report back your WAN IP address.

NOTE: I'm aware that the original question mentioned that they were looking for alternatives to this approach but I'm putting it in here so that this answer covers all the bases.

                                                        +---------------+
  +-------------+   +--------+   +------+     LAN IP    | Computer that |
  |whatsmyip.com|---|Internet|---|router|---------------| wants to know |
  +-------------+   +--------+   +------+  192.168.1.x +----+ WAN IP    |
you're 54.234.1.33                                     |eth0|-----------+
                                                       +----+

# 1st server
% wget -qO - http://cfaj.freeshell.org/ipaddr.cgi
54.234.1.33

# 2nd server
% wget -qO - icanhazip.com
54.234.1.33

# 3rd server
% curl -s checkip.dyndns.org | sed 's#.*Address: \(.*\)</b.*#\1#'
54.234.1.33

Additional info is available here: HOWTO: Check you external IP Address from the command line

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值