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,793
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,376
  • 5
  • 34
  • 52
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
  • 637