Bosse_B
Posts: 1079
Joined: Thu Jan 30, 2014 9:53 am

How to read external IP address into a variable in a script?

Tue Apr 14, 2020 6:41 am

I would like to log my external IP, i.e. the WAN IP address of the router, on an RPi4 unit, which sits on a mobile broadband connection.
It seems like the external IP address changes frequently so I would like to create a log of the address.
I can do all items in a script except I don't know how to retrieve the address....
If I use this command in a browser I get a response in the browser window:
http://checkip.dyndns.com/

The displayed text in the browser is this:
Current IP Address: 158.174.yyy.xxx
(yyy.xxx are real numbers of course)

Can you advice some way I can use that URL inside a script and extract the IP address response into a variable?
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17963
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to read external IP address into a variable in a script?

Tue Apr 14, 2020 6:55 am

In bash, the following works for me

Code: Select all

myip="$(wget -q -O - "http://checkip.dyndns.com/" | sed "s/^.*: \([0-9.]*\)<.*$/\1/")"
echo "My IP address is: $myip"
There might be easier ways.
Unreadable squiggle

Bosse_B
Posts: 1079
Joined: Thu Jan 30, 2014 9:53 am

Re: How to read external IP address into a variable in a script?

Tue Apr 14, 2020 7:02 am

Thanks!
I think it is easy enough - it is working fine!
Bo Berglund
Sweden

Return to “General discussion”