{"id":43,"date":"2011-03-30T07:57:00","date_gmt":"2011-03-29T21:57:00","guid":{"rendered":"https:\/\/www.serenux.com\/?p=43"},"modified":"2021-01-14T08:02:04","modified_gmt":"2021-01-13T22:02:04","slug":"howto-automatically-determine-your-public-ip-address-and-email-it-periodically","status":"publish","type":"post","link":"https:\/\/www.serenux.com\/index.php\/2011\/03\/30\/howto-automatically-determine-your-public-ip-address-and-email-it-periodically\/","title":{"rendered":"HowTo: Automatically determine your public IP address and email it periodically"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let\u2019s say you are running a poor man\u2019s website where you are just testing stuff but have no real intention of buying a domain name or paying your ISP to give you a static IP address.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to access your test site remotely, you need to know your public IP address, however your home ISP gives you a dynamic public IP address and every time you have a power failure, or reboot your router, you are assigned a brand new public IP address. This makes it very annoying if you are testing your site remotely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sure, you could use a Dynamic DNS service to keep track of when your public IP address changes, but what if you have a paranoid client who does not want to use even Dynamic DNS? How do you keep track of your new public IP without having to get to your internal network to read it each time?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What you need is a way to be able to have the system send you an email with your current public IP address so that there is no guess work involved. But how do we do this?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To determine your public IP, you need to have an external site to refer to which will record your public IP as connecting to it. You could use services such as What Is My IP or similar, but we don\u2019t want to rely on an external&nbsp;<em>service<\/em>, nor do we want to hammer a third-party service with requests. Basically we just want to replicate the functionality of such a site as locally as possible and then use an email tool to send the information to you in a message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pre-requisites:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access to a web space outside of your working space with an IP or address that does not change. Generally every ISP out there will grant you some small quantity of complimentary web space to store some files. If you are working for clients like I am, you can use your own server if you like (after all, this blog is served from a fixed IP with a domain name attached to it).<\/li><li>A PHP interpreter in your web space\u2019s host. If you are using your own static host on Ubuntu, Apache 2 from a normal Ubuntu install be it Desktop or Server, then you should have the PHP interpreter installed by default. If not, then install the&nbsp;<em>php5<\/em>&nbsp;package.<\/li><li>If using your own static server, you will need a web server such as Apache installed on it to serve the PHP we\u2019ll be creating.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now let\u2019s look at using a simple bit of PHP to obtain the address of the connecting host.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>On your external static web space (not your local dynamic site), create a new text file somewhere readily available called\u00a0<em>getmypublicip.php<\/em>\u00a0or similar.<br><\/li><li>In your favourite text editor, copy and paste in the following PHP code:<br><br><code>&lt;?php<br>$ip = getenv(\"REMOTE_ADDR\");<br>echo $ip;<br>?><\/code><br><\/li><li>Save your changes and exit the text editor.<br><\/li><li>Test your new PHP file by referring to it from your ISP\u2019s webspace, eg: If your ISP\u2019s website was called\u00a0<em>myisp.com<\/em>\u00a0and you had a login called \u201cnoddy\u201d, then the address to your webspace might be\u00a0<em>http:\/\/www.myisp.com\/~noddy\/getmypublicip.php<\/em>, so type that in your web browser, which is usually the path most ISP\u2019s use.<br><\/li><li>When you go to that URL, the PHP on the remote side should parse the referring public IP address and echo it to the page. In this case, because you are referring yourself, you should see your own public IP address (let\u2019s say it\u2019s\u00a0<em>74.125.237.84<\/em>\u00a0for the purposes of this guide) printed at the top-left of the web browser page.<br><br>If you ran this PHP locally, you would get the internal IP of the connecting host instead, eg:\u00a0<em>192.168.0.100<\/em>\u00a0or similar instead.<br><\/li><li>Awesome, so now how do we email this newly obtained information to ourselves? Simple. We will make use of a neat little command line tool called\u00a0<em>send<strong>e<\/strong>mail<\/em>\u00a0(not to be confused with the venerable\u00a0<em>sendmail<\/em>). It\u2019s not installed in Ubuntu by default, so install it with:<br><br><code>$ sudo apt-get install sendemail<\/code><br><\/li><li>Let\u2019s create the script that will use sendemail. On your local test website, create a new text file outside of your web-accessible directories, eg:\u00a0<em>\/home\/noddy\/getpublicip.sh<\/em>\u00a0using your favourite text editor.<br><br><code>$ nano \/home\/noddy\/getpublicip.sh<\/code><br><\/li><li>In the text editor, copy and paste the following in:<br><br><code>#!\/bin\/bash<br>wget -O- http:\/\/www.myisp.com\/~noddy\/getmypublicip.php 2>\/dev\/null | sendemail -u \"Client's public IP address\" -f test@blah.com -t noddy@mycompany.com -s mail.mycompany.com<\/code><br><\/li><li>Save your changes and exit your text editor.<br><\/li><li>Now test your script my manually running it using the following command:<br><br><code>$ sh \/home\/noddy\/getpublicip.sh<\/code><br><\/li><li>Check your personal email. You should have received an email sent by\u00a0<em>test@blah.com<\/em>\u00a0containing a single line showing your public IP address!<br><\/li><li>Now we need to setup the script to be executed periodically. We will setup a cron schedule to run our new bash script as follows:<br><br><code>$ crontab -e<\/code><br><\/li><li>Your text editor will appear. In this, add the following line on the end:<br><br><code>0 * * * * sh \/home\/noddy\/getpublicip.sh<\/code><br><\/li><li>Save your changes and exit your text editor.<br><\/li><li>Now wait. On the next whole hour, you should receive an email in your mailbox containing a single line with an IP address \u2013 your public IP address! Now you need never know what your private server\u2019s public IP address is if it changes in the middle of the day \u2013 on the hour you will be told what it is!<br><\/li><li>If you\u2019d like to receive notification on a more regular basis, modify the cron schedule. For example, the following change to step 13 will make your system run the script every 15 minutes:<br><br><code><strong>*\/15<\/strong> * * * * sh \/home\/noddy\/getpublicip.sh<\/code><br><\/li><li>That\u2019s it! Pat yourself on the back. You\u2019re done.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">NOTE: Some ISP\u2019s do not allow connections on dynamic IP\u2019s to send periodic email on their mail server and may block it on the belief that your emails may be spam. Should that be the case, you should install a local mail daemon such as Postfix and change the sendemail arguments to direct their mail to&nbsp;<em>localhost<\/em>&nbsp;instead, and allow Postfix to send the notification email. In Ubuntu, you can install Postfix by simply installing the&nbsp;<em>postfix<\/em>&nbsp;package and electing the server to be an \u201cInternet Site\u201d. The default configuration is fine, but not secure. Ensure that no-one externally can access port 25 on your client\u2019s server if you choose to go down this route.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you are running a poor man\u2019s website where you are just testing stuff but have no real intention of buying a domain name or paying your ISP to give you a static IP address. If you want to access your test site remotely, you need to know your public IP address, however your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":1,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}