{"id":1652,"date":"2015-12-01T19:33:36","date_gmt":"2015-12-02T03:33:36","guid":{"rendered":"https:\/\/chriscarey.com\/?p=1652"},"modified":"2017-05-06T08:18:55","modified_gmt":"2017-05-06T16:18:55","slug":"raspberry-pi-phone-home-with-ip-address","status":"publish","type":"post","link":"https:\/\/chriscarey.com\/blog\/2015\/12\/01\/raspberry-pi-phone-home-with-ip-address\/","title":{"rendered":"Raspberry Pi phone home with IP address"},"content":{"rendered":"<p>Sometimes it&#8217;s a pain to keep track of your Raspberry Pi&#8217;s IP address. Maybe you want to plug it in on a work or friend&#8217;s network without having to plug in a keyboard and mouse. This technique is what I am using to know the IP address of my Raspberry Pi&#8217;s no matter how often they change or what the IP address is.<\/p>\n<p><!--more--><\/p>\n<p>This requires that you have a web server. I&#8217;m logging the IP addresses into the web server log files, Apache in my case.<\/p>\n<p>The script source code is here:<\/p>\n<p><strong>\/home\/pi\/bin\/send-ip-addr.sh<\/strong><\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:565px;\"><div class=\"text codecolorer\">#!\/bin\/bash<br \/>\nSERVER=&quot;http:\/\/example.com&quot;<br \/>\nHOST=`\/bin\/hostname`<br \/>\nETH0=`\/sbin\/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`<br \/>\nWLAN0=`\/sbin\/ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`<br \/>\nif [ -n &quot;$ETH0&quot; ]; then<br \/>\ncurl ${SERVER}\/locater\/${HOST}\/eth0\/${ETH0}<br \/>\nfi<br \/>\nif [ -n &quot;$WLAN0&quot; ]; then<br \/>\ncurl ${SERVER}\/locater\/${HOST}\/wlan0\/${WLAN0}<br \/>\nfi<\/div><\/div>\n<p><strong style=\"color:darkred\">Make sure to set &#8220;http:\/\/example.com&#8221; to a web server name that you own!<\/strong><\/p>\n<p>I set up a crontab to run this script every 5 minutes:<\/p>\n<p><strong>\/etc\/crontab<\/strong><\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:565px;\"><div class=\"text codecolorer\">*\/5 * * * * pi \/home\/pi\/bin\/send-ip-addr.sh<\/div><\/div>\n<p>This will drop an entry into your server log files every 5 minutes that looks like this:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:565px;\"><div class=\"text codecolorer\">[01\/Dec\/2015:22:25:01 -0500] &quot;GET \/locater\/pi2b\/eth0\/10.69.1.82 HTTP\/1.1&quot; 200 183 &quot;-&quot; &quot;curl\/7.26.0&quot;<\/div><\/div>\n<p>The useful bit is the location that is requested <strong>\/locater\/pi2b\/eth0\/10.69.1.82<\/strong><br \/>\nSplit the slashes and you get \/locater\/ then the Raspberry Pi hostname, interface (eth0 or wlan0), and IP address.<\/p>\n<p>I use this technique to keep tabs on the current IP address of nine of my Raspberry Pi computers so I can access them regardless of what IP address they get, or where I plug them in. This also lets me know how many of them are online at any given time since they will phone home every 5 minutes together.<\/p>\n<p>To see the logs, hop on your web server and tail your log file, filtering out the word &#8220;locater&#8221;. Your log file path may be different, so change \/var\/log\/apache2\/access.log to your log file name:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:565px;\"><div class=\"text codecolorer\">tail -f \/var\/log\/apache2\/access.log | grep &quot;locater&quot;<\/div><\/div>\n<hr>\n<p>If you want to make the log entry show up as a &#8220;<strong>HTTP 200 OK<\/strong>&#8221; instead of a &#8220;<strong>HTTP 404 Page Not Found<\/strong>&#8221; you need to put a hack on to your web server. This is useful if you run statistics and you do not want a bunch of ugly 404s to show up.<\/p>\n<p>I&#8217;m running Apache so I created this directive which will catch any URL request under \/locater (since the script fakes subdirectories):<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:565px;\"><div class=\"text codecolorer\">Alias \/locater \/opt\/locater.htm<\/div><\/div>\n<p>&#8230;with a file at \/opt\/locater.htm<\/p>\n<p>It doesn&#8217;t matter what is in the file, as long as it exists.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it&#8217;s a pain to keep track of your Raspberry Pi&#8217;s IP address. Maybe you want to plug it in on a work or friend&#8217;s network without having to plug in a keyboard and mouse. This technique is what I am using to know the IP address of my Raspberry Pi&#8217;s no matter how often [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[59],"tags":[],"class_list":["post-1652","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prpYG-qE","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":31,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"predecessor-version":[{"id":1813,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions\/1813"}],"wp:attachment":[{"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chriscarey.com\/blog\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}