How to send new url address of localtunnel to email on startup

Last updated on January 14, 2013 @ No comments yet

For you who doesn’t know idea of localtunnel kindly read here for more information, Please note that current post was tested on Centos 6.3.

Basically localtunnel create a dynamic tunnel from your local webserver to their server so it can be world wide accessible with a random localtunnel dot com subdomain name every time we start the tunnel.

Localtunnel : The easiest way to share localhost web servers to the rest of the world

However it’s a great service especially if we have a local server behind nat/firewall and we need to work on it away from server (i.e. home).

Cacti trough LocalTunnel

We have an unstable power source on our office, sometime the power down for a few minutes and sometime more than 6 hours, unscheduled and totally random. We put the localtunnel on our startup script, so it can keep world wide accessible so we can continue to work on. The problem is that every time localtunnel connected to server, new address is also generated, which mean our server now is on a new address.

So we have this idea, to send newly generated address to our email from our local server everytime the server resumed from power failure. But somehow is not that simple, because localtunnel doesn’t write a log file (not sure, because i can’t find it anywhere in my computer) and doesn’t write anything if we force to write the output to a file or mail until CTRL+C is pressed.

Finally we’ve found within localtununel library called tunnel.rb where it write the result to stdout. We need this library also write a log so we can use the log for later. So, here what we’ve done (assume localtunnel already installed and you know how to use it).

Finding tunnel.rb

find /usr -name tunnel.rb

Backup and edit tunnel.rb

cd /usr/lib/ruby/gems/1.8/gems/localtunnel-0.3/lib/localtunnel/
cp tunnel.rb tunnel.rb.back
nano tunnel.rb

nanto tunnel.rb
find  “is now publicly accessible from” sentence within the file and add the following line just bellow it.

open("/tmp/tunnel_#{port}_.log", "w") {|f| f.puts "New address http://#{tunnel['host']} for port #{port}"}

Edited tunnel.rb
This line will create /tmp/tunnel_(port number)_.log (a log file on tmp folder) everytime localtunnel connected to the server and write a log contain “New address for port yyy“, where :

xxxx = random localtunnel subdomain created
yyyy = our local webserver port

Next task is sending the log created to our email address.

Configuring local mail

Sendmail was not working on our server, so we have to relay our local mail trough Gmail in order to send any notification from server to our email using SSMTP.

Installing SSMTP

yum install ssmtp

Backup and create new config file

mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.back
nano /etc/ssmtp/ssmtp.conf

Write the following config on it (change AuthUser and AuthPass to match your account)


Next, backup and replace sendmail with ssmtp

which sendmail
mv /usr/sbin/sendmail /usr/sbin/sendmail.backup
which ssmtp
ln -s /usr/sbin/ssmtp /usr/sbin/sendmail

Test your new mail

mail < a_text_file_or_log_for_test.log

Check your target email, when you got mail contain texts from the file then your done with your mail setting.
Mail received
Creating startup script
Now we need to create a stratup script which is contain command for:

  1. Delete previous localtunnel log file
  2. Check internet connection, if not connect wait for 2s
  3. Run localtunnel with provided port
  4. Check locatunnel file log, if exist send mail, if not exist sleep for 2s
nano /root/bin/localtunnelstartup

Place the script anywhere you want, in my case /root/bin/localtunnelstartup.
write the following codes on it.

rm -rf $LOG #delete previous log file
while [ `/bin/ping -c 1 | grep 'packets transmitted' | awk {'print $6'}` = "100%" ] #checking connection to
        sleep 2 #if not, wait until connected
/usr/bin/nohup /usr/bin/localtunnel $1 > /dev/null 2&>&1&; #run the localtunnel
while [ ! -f $LOG ] #checking log file
        sleep 2 #if not, wait until log created
mail < $LOG  #send the log to owner's email

Using the script
make it executable

chmod u+x /root/bin/localtunnelstartup

Add the following command into /etc/rc.local (change the port to suit you need, 81 just an example port).

/root/bin/localtunnelstartup 81

This will check connection to before creating a localtunnel for port 81 (to make sure server connected to internet) and then send the url address generated from log file to email you’ve provided on the startup script.

M Riza

Article by M Riza

Is a blogger since 2007, founder and editor of Oa Ultimate. Working as a network administrator, computer technician, database and web maintainer | Twitter | G+

Leave a Reply


Loading Google+ Comments ...
Loading Facebook Comments ...