External IP script

Got any great tips or tricks you're dying to share with the rest of the world? Then please post them here.
They don't have to be specifically related to building a media server but please do try to keep them suitably "geeky".
User avatar
Posts: 49
Joined: March 17th, 2011, 10:19 pm

External IP script

Postby DunkB » January 15th, 2012, 11:29 am

Hi all

Just a quick tip/trick I thought I'd pass on.

As most of us don't have an ISP kind enough to supply us with a static IP the question of how to SSH to our server from outside the home network poses a problem. While there are services that provide DDNS like DynDNS.org they charge money and I'm tight! Besides I don't need to log on remotely that often.

I have written the following script which serves me well, basically it checks to see if my external IP has changed and sends me an e-mail when it does.

Code: Select all

# PURPOSE: e-Mail me my external IP on change. 
new=$(wget -qO - http://cfaj.freeshell.org/ipaddr.cgi)

if grep -q $new /home/myhomedir/MyScripts/MyIP.log
echo "No change"
echo $new > /home/myhomedir/MyScripts/MyIP.log

/usr/sbin/ssmtp my@email.co.uk </home/myhomedir/MyScripts/MyIP.log


Make the script executable

chmod a+x MyIP.sh

Create a blank log file, I've called mine MyIP.log. Imaginative I know. It will populate the first time the script is run.

Set the script as a cron job to execute at an interval of your choice.

Job done.

In order to SSH in from the outside world you will need to make sure your router is forwarding the appropriate port (22 in my case) to your server.
Then just tell putty your newly discovered IP.

Hope someone finds this of use.

Dunk :ugeek:

Posts: 40
Joined: October 18th, 2011, 1:00 am

Re: External IP script

Postby n2o2diver » January 19th, 2012, 3:55 am

Should go in the Tips and Tricks section

User avatar
Posts: 751
Joined: January 1st, 2011, 8:00 am

Re: External IP script

Postby Ian » January 19th, 2012, 8:20 pm

n2o2diver wrote:Should go in the Tips and Tricks section

Good idea!

Done :thumbup:

User avatar
Posts: 2
Joined: December 17th, 2013, 2:58 am
Location: netherlands

Re: External IP script

Postby gwynhfar » December 17th, 2013, 4:28 am

Before you start anything network related, building servers, subdomains, VM's, you start with a blank A4 white paper and write down some things you have to know.
Dynamic or Static IP?: Is your provider giving you a static IP? So it won't change? Write it down. Otherwise look here in the forum about dynamic IP.
What Router do you use?: assume you have a static IP address (f.e., what router is between your providers system and your computers?
Routers can be administered, at least most of them, by typing or in your browser.... admin, admin, for login and password if it is your first time.
In your routers setup you'll find the static (or dynamic) IP number (f.e., now consider that as your "phone number" for the outside world to reach you. You cannot call yourself is it? (That's where proxy's come in, use another IP to "call" yourself).
But we keep it simple, we build a server for ourselves, inside the house, wireless, or cabled.
So we are not going to talk about PORTS? (yes we will later in this story).
Your Router will tell you, that it is deviding devices the way he wants, DHCP, the router will give any PC or laptop, or any device, who hooks on, a IP number (range f.e. to So your device can be, or, etc, depends on the session.

We don't want that!

So in our home environment, we tell our machines to have a static IP within the range of the router. (fe. It takes some figuring out on how to do so, but Ian showed the way on his website.
Now here is where most people get confused, what is that all about opening ports?

Nothing, as long as you keep your server for yourself (your indoor network), ports are not important, they just exist, for your at home network and you have to remember them if you use several computers indoors.

Port forwarding: If your network wants to go out to the big cruel world :crazy: , you need to know your IP address the provider gave you and tell your router to open the gates, port, for the big cruel world.
So what you can see inside your house on ip addresses (like you can throw to the big world by port forwarding.... just tell your router that your port 9000 is open for your static IP address. :geek: In a way your provider looks if your system is open for that port (the door).

I hope this will clear some things for those who get confused. There is more, off course, but for now, understand the static or dynamic IP your provider is giving you, the router that's inbetween and DHCP'ing everything for 98% of the internet users at home, and the possibilities for you to re-arrange if you knowhow to do it.

Posts: 6
Joined: April 17th, 2014, 7:47 am

Re: External IP script

Postby DaveyB » April 19th, 2014, 10:43 am

This is an old post, but I thought it better to update it rather than post a new topic.

You can use the API (Application Programming Interface) described at http://whatismyipaddress.com/api to get your external IP address for a script.

The address to use is http://bot.whatismyipaddress.com/ and this will return your external IP address, nothing more nor less. They do ask that you keep the queries to less than one every 5 minutes, and tbh, if your IP changes more frequently than that, you need to get off AOL and find a real ISP! ;)

Hope that helps!

Return to “Tips 'n' Tricks”