Friday, April 26, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 4192  / 3 Years ago, sun, july 4, 2021, 7:00:16

I'm using DNSExit on my Ubuntu server (Natty 11.04) and have installed the ipUpdate rpm file and did an apt-get install for chkconfig to enable the ipUpdate.



I'd like to get an email whenever the IP address changes, so that I can SSH into the box using PuTTY (because I can't unless I know the current IP address).



The current IP address is stored in /tmp/dnsexit-ip.txt, and I'd like to, whenever that file changes, to mail the contents of that file and the contents of /var/log/dnsexit.log (which contains a history of the IP changes) to my email address.



How can I accomplish this task? I'm thinking that a cronjob will be the solution, but I'm not sure how to do this.


More From » cron

 Answers
3

Prerequisites



Install sendEmail. It's a lightweight, command line SMTP email client. We'll use it to send emails from a script, using a Gmail account.



sudo apt-get install sendemail libio-socket-ssl-perl libnet-ssleay-perl


Create the script



Create a file named "ip-notify.sh" somewhere, for example in a "Scripts" directory in your home folder; make it executable, and open it up for editing.



mkdir -p ~/Scripts && touch ~/Scripts/ip-notify.sh && chmod a+x ~/Scripts/ip-notify.sh && gedit ~/Scripts/ip-notify.sh


Insert the following text in the file:



#!/bin/bash

# Modify the following values!

SENDERNAME="Computer" # This is the name that will show in the 'From' field. Purely esthetic.
RECIPIENTNAME="Your Name" # This is the name that will show in the 'To' field. Also purely esthetic.
GMAILADDRESS="[email protected]" # This is your Gmail address.
GMAILUSER="someemail" # This is your Gmail username, without the '@gmail.com' part.
GMAILPASS="password" # This is your Gmail password.

# You can stop modifying here

DIR=/tmp/
CURIP=dnsexit-ip.txt
IPLOG=/var/log/dnsexit.log
SMTPSERVER="smtp.gmail.com:587"

if [[ $(find $DIR -mmin -2 -name $CURIP) ]];
then
echo "$CURIP has been modified in the last two minutes."
# Send an email
sendemail -u "IP Address" -m "IP address has changed!" -f "$SENDERNAME <$GMAILADDRESS>" -t "$RECIPIENTNAME <$GMAILADDRESS>" -s $SMTPSERVER -xu $GMAILUSER -xp $GMAILPASS -a $DIR$CURIP $IPLOG
fi


Once that's done, save and close the file.



Run the script periodically



We'll run this script every two minutes. Open up your crontab.



crontab -e


Add the following line to the bottom of the file:



*/2 * * * * bash ~/Scripts/ip-notify.sh


You're done!



If all goes well, you should now get email updates when the IP address of your machine changes.


[#38116] Monday, July 5, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
algicuade

Total Points: 317
Total Questions: 89
Total Answers: 106

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;