1

I own a server at my Parents house and recently there has been a lot of power cuts and when our Router comes back online it changes its Public/Global IP address which makes me unable to connect to it (SSH, FTP, HTTP) ,is there any way that I could make a script that when the power goes out on my server and it reboots it send an email with my Global/Public IP to me.

Jakuje
  • 6,605
  • 7
  • 30
  • 37
Joiie
  • 11

2 Answers2

2

You could try using sendmail. install

sudo apt install -y postfix

choose internet side and enter your server's hostname

Write a script e.g. in sudo nano /usr/local/bin/sendIPmail:

#!/bin/bash

# 1. gets the primary IP
#from https://stackoverflow.com/a/25851186/7111561
IP=$( ip route get 1 | awk '{print $NF;exit}' )


# 2. send it using sendmail
#from https://stackoverflow.com/a/13390926/7111561

#--- adjust those ---
recipients="your.mail@address,another@mail.address"
subject="some subject"
from="info@your.server"
#--------------------

/usr/sbin/sendmail "$recipients" <<EOF
subject:$subject
from:$from

New IP is $IP
EOF

exit 0

make it executable

sudo chmod +x /usr/local/bin/sendIPmail

and call it on every reboot run

sudo crontab -e

(select your editor e.g. 2 for nano) Add the line

@reboot /usr/local/bin/sendIPmail

This does not require any external webpages or complex scripts/setups and uses only packages from the official Ubuntu repos.

NOTE: Alternatively you really should consider using a static IP address for a server!

derHugo
  • 3,356
  • 5
  • 31
  • 51
1

use my script.

  1. Clone the script from my github repo:

git clone https://github.com/el-beth/sendExternalIPAddress.git

  1. then move the sendexternalip.bash file from the directory sendExternalIPAddress to /usr/lib - use the following command to do so.

sudo cp sendExternalIPAddress/sendexternalip.bash /usr/lib

  1. now make the script executable:

sudo chmod +x /usr/lib/sendexternalip.bash

  1. for the first time, to install all the necessary packages and dependencies: sudo /usr/lib/sendexternalip.bash

  2. now to make the script run automatically on startup run the following command:

sudo printf "start on startup\ntask\nexec /us/lib/sendexternalip.bash\n" > ~/.config/upstart/sendexternalip.conf

This will make your Linux box email it's current external IP address to the email address specified in the script.

N.B. The script - by default - sends the External IP address to the email address "receiver@grr.la", however, you can change the customReceiverEmail variable on line 9 of the script to any @grr.la address of you choosing and the IP address will be sent to that address instead.

endrias
  • 597
  • I think one of the issues here is if they don't have GoeillaMail - the entire thing seems to rely on that based on your answer. – Thomas Ward Jul 04 '17 at 15:37
  • I have edited the answer and fixed a bit. would you be so kind as to go through the answer - from the top - once more and up-vote the reply when it works .. lol .. btw, really sorry for the hassle - but the improved answer will work. Also gorilla mail is a free and anonymous messaging service. google it. no sign up or subscription is needed. in your case you just insert the email address "receiver@grr.la" in the box at the homepage and voila, you can see all the emails that are sent to that account. these are disposable email addresses. – endrias Jul 04 '17 at 15:43