erebus^
Posts: 12
Joined: Sat Oct 04, 2014 11:22 pm

Best way to make RPi report it's public IP

Tue Sep 24, 2019 12:46 pm

Heya!

I have a RPi with your usual home setup. ATM I rent a DNS name from a provider, but I'm not sure if it's something I really need.

So I've been looking for a way to make the RPi regularly send me it's public IP, by mail or something.

I only need it so I personally can log on to it via SSH.

Is there a way to do this? Is there a command line to find your public IP address at all?

Thank you :)

Andyroo

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 12:57 pm

Have a look at https://www.ipify.org/

Returns IPv4 and IPv6 in json for you.

tpyo kingg
Posts: 626
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 12:59 pm

Another way is to subscribe to a dynamic DNS service. It might be part of your current DNS subscription, or it may be an add on, o ryou might need to do it completely separately.

Once you have dynamic DNS, you can install and configure the package ddclient. That will phone home to the service and update your address in their register. The end result is that you can then SSH to a host name and it will automatically go to your current address.

erebus^
Posts: 12
Joined: Sat Oct 04, 2014 11:22 pm

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:04 pm

Andyroo wrote:
Tue Sep 24, 2019 12:57 pm
Have a look at https://www.ipify.org/

Returns IPv4 and IPv6 in json for you.
Oh, very nice, thanks! This helps me a lot.

I can't really think of any other way of reporting it back than email. Or maybe a chat-bot of some kind...

erebus^
Posts: 12
Joined: Sat Oct 04, 2014 11:22 pm

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:06 pm

tpyo kingg wrote:
Tue Sep 24, 2019 12:59 pm
Another way is to subscribe to a dynamic DNS service. It might be part of your current DNS subscription, or it may be an add on, o ryou might need to do it completely separately.

Once you have dynamic DNS, you can install and configure the package ddclient. That will phone home to the service and update your address in their register. The end result is that you can then SSH to a host name and it will automatically go to your current address.
Thaaaat is what I have right now, I believe. And yeah, it works great, but it also costs money. I might keep it anyway, but I'd thought it be fun to dive in to some alternative ways of solving this...

User avatar
B.Goode
Posts: 8820
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:12 pm

I can't really think of any other way of reporting it back than email.
Let me be your imagination..

Twitter

MQTT


A GUI app that displays the information in a window on your Desktop.


An additional display (maybe just a 16-character LCD) to present that IP address.

tpyo kingg
Posts: 626
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:14 pm

There are also free-of-charge dynamic DNS options. The service noip.com has one. There are lists of them around. You'll have to read the terms of service to see if that one or anything like that is acceptable. At best you have to renew the free trial every month. It might be least work to keep paying like you have.

If you are just looking to find the IP address before leaving the Raspberry Pi, then you might just have it look up its external address and show that on an LCD or e-paper display. I've done that with an Inky PHat. It's ok as long as you remember to check before leaving and, depending on the ISP, it might not change too often.

erebus^
Posts: 12
Joined: Sat Oct 04, 2014 11:22 pm

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:24 pm

tpyo kingg wrote:
Tue Sep 24, 2019 1:14 pm
There are also free-of-charge dynamic DNS options. The service noip.com has one. There are lists of them around. You'll have to read the terms of service to see if that one or anything like that is acceptable. At best you have to renew the free trial every month. It might be least work to keep paying like you have.

If you are just looking to find the IP address before leaving the Raspberry Pi, then you might just have it look up its external address and show that on an LCD or e-paper display. I've done that with an Inky PHat. It's ok as long as you remember to check before leaving and, depending on the ISP, it might not change too often.
Hmmmm, a free DDNS might be all I need actually. Any recommendations? Is https://duckdns.org reliable?

Andyroo

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:48 pm

I’ve been using https://www.dynu.com for a long time.

No issues with response time from outside and it’s simple enough to update.

I have one minor issue where it reports back that the IP address has changed but it has not maybe once a day but that’s not an issue as it still works.

This is the basic script I use (well it’s the latest one I can get hold of on my iPad :lol: )

Code: Select all

#!/usr/bin/python3

import logging
import sys
from subprocess import PIPE, run

if len(sys.argv) > 1:
    if sys.argv[1].upper() == 'DEBUG':
        log_level = logging.DEBUG
    elif sys.argv[1].upper() == 'INFO':
        log_level = logging.INFO
    else:
        log_level = logging.INFO
else:
    log_level = logging.INFO

logging.basicConfig(filename='/home/pi/IP-change.log',level=log_level, datefmt='%Y/%m/%d %H:%M:%S', format='%(asctime)s %(message)s')

pwd = "https://api.dynu.com/nic/update?hostname=example.com&password=longcomplexstringgoeshere"

update = run(['curl', pwd], stdout=PIPE, stderr=PIPE)

logging.debug('Program executed under debug logging')
logging.debug('Return code = ' + str(update.returncode))
logging.debug('Return Data = ' + update.stdout.decode())

if update.returncode > 0:
    logging.warning('Curl failed with code ' + str(update.returncode))

if update.stdout.decode() != 'nochg':
    logging.info('IP address change - ' + update.stdout.decode())
This runs from a cron job with info or debug as the parameter depending on the amount of logging I’m interested in.

Edit: code tags now corrected...

Kendek
Posts: 127
Joined: Thu Jul 25, 2019 4:39 pm
Location: Kaposvár, Hungary

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 1:59 pm

Code: Select all

wget -qO- ifconfig.co
curl ifconfig.co
or if you have an IPv6 WAN address too, but you need the IPv4:

Code: Select all

wget -qO- v4.ifconfig.co
curl v4.ifconfig.co


spock
Posts: 208
Joined: Sun Oct 02, 2011 10:33 am

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 4:22 pm

if you buy a domain you could also use cloudflare. they have an api that can be used to update your ip.

jbudd
Posts: 1016
Joined: Mon Dec 16, 2013 10:23 am

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 5:11 pm

erebus^ wrote:
Tue Sep 24, 2019 1:24 pm
Hmmmm, a free DDNS might be all I need actually. Any recommendations? Is https://duckdns.org reliable?
I use duckdns. It's very simple to set up and not too hard to update it with an HTTP request when your public IP changes. (I use a Node-Red flow to update it automatically)
My only slight niggle with it is you can't login directly to duckdns, you have to use your google, reddit, twitter etc ID.

User avatar
DougieLawson
Posts: 36302
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 5:43 pm

I use

Code: Select all

#!/bin/bash
IPv4=$(dig +short myip.opendns.com \@resolver1.opendns.com | grep -v -e '^$' || echo "0.0.0.0")
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Roken
Posts: 306
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

Re: Best way to make RPi report it's public IP

Tue Sep 24, 2019 6:52 pm

Code: Select all

curl ifconfig.me
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

Return to “General discussion”