# aquire external IP address from router via command line?

## germaniac

I am looking for a nice way to get the external ip from my router.

Right now I solved this with a small quick'n dirty script I wrote:

-----------------------------------------

#! /bin/sh

# retrieve file max once an hour

VAR0=`ls -l | grep firewallip.txt | awk '{print $6,$7,$8}' | sed 's/...$//'`

VAR1=`date | sed 's/^....//' | sed 's/................$//'`

if [ ! "$VAR0" == "$VAR1" ]; then wget -O firewallip.txt http://www.mueloliva.es/cgi-bin

fi

wait

cat firewallip.txt | grep REMOTE_ADDR | sed 's/^............//'

-----------------------------------------

Does anybody have a nicer way to do this?

----------

## think4urs11

 :Shocked:  

```
wget -O - -q http://www.whatismyip.com | grep h1 | cut -f 4 -d " "
```

  :Question: 

----------

## bbgermany

Or this, very short:

```

wget http://ipinfo.io/ip -qO -

```

greets, bb

----------

## charles17

 *bbgermany wrote:*   

> Or this, very short:
> 
> ```
> 
> wget http://ipinfo.io/ip -qO -
> ...

 Even shorter:

```
curl http://ipinfo.io/ip
```

----------

## NathanZachary

Another option is:

```
curl https://api.ipify.org
```

----------

## UberLord

 *NathanZachary wrote:*   

> Another option is:
> 
> ```
> curl https://api.ipify.org
> ```
> ...

 

```
uberserver$ curl https://api.ipify.org

curl: (60) SSL certificate problem: self signed certificate in certificate chain

More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"

 of Certificate Authority (CA) public keys (CA certs). If the default

 bundle file isn't adequate, you can specify an alternate file

 using the --cacert option.

If this HTTPS server uses a certificate signed by a CA represented in

 the bundle, the certificate verification probably failed due to a

 problem with the certificate (it might be expired, or the name might

 not match the domain name in the URL).

If you'd like to turn off curl's verification of the certificate, use

 the -k (or --insecure) option.
```

I think you need to use the -k option.

----------

## NathanZachary

Strange.  I didn't get an SSL error when using ipify.org.  If you do, though, I guess you can pass the -k to curl.  :Smile: 

----------

## Ant P.

This might also work:

```
emerge net-libs/miniupnpc

/usr/bin/external-ip
```

----------

## wuzzerd

This should do it.

```
 traceroute google.com -n  | head -n 3

traceroute to google.com (216.58.217.46), 30 hops max, 60 byte packets

 1  10.10.10.1  0.423 ms  0.528 ms  0.658 ms

 2  7.42.7.42  25.544 ms  30.293 ms  30.325 ms

```

----------

## Hu

 *wuzzerd wrote:*   

> This should do it.
> 
> ```
>  traceroute google.com -n  | head -n 3
> 
> ...

 Not necessarily.  I ran your command from a system behind a NAT router and the only output it returned was the internal address of the NAT device:

```
$ traceroute google.com -n  | head -n 5

traceroute to google.com (216.58.217.46), 30 hops max, 60 byte packets

 1  192.168.0.1  0.342 ms  0.345 ms  0.338 ms

 2  192.168.0.1  0.594 ms !X  0.595 ms !X  0.803 ms !X

```

I even raised the number of lines that head was allowed to print, and it never produced my external IP.  Your technique relies on the behaviour of the NAT device managing the connection to the outside world.  For some, it may work.  For others, like mine, it does not.

----------

## Akkara

Wow. a 12-year necro with actual content posted...  That's got to be a record of some sort.

(Usually it's spambots that bumping old threads since they choose at random to deliver their payload)

----------

