Send Email when IP Address Changes

Posted: 6 Μαρτίου 2018 in linux
Ετικέτες: , , , ,

Εγκατάσταση msmtp σε  GNU/Linux για να στέλνουμε  emails από το  gmail

Βήμα 1: Κάνουμε εγκατάσταση μέσω τερματικού τα απαραίτητα πακέτα.

sudo apt-get install msmtp msmtp-mta

Βήμα 2: Δημιουργούμε ένα app password στο  Google λογαριασμό μας για το  msmtp

This (Step 2.* in this tutorial) was the missing piece of the puzzle which weren’t in any of the msmtp tutorials I did through. I found out about it in one of the Postfix tutorials where it was included after many hours of trial and error. Because at first, I thought the msmtp config file where was password field had to be filled with the same password with what you log in as a person to your account.

Go to: https://myaccount.google.com and log into the email account you want to start sending the emails.

Step 2.1: Select Security from the side menu and turn 2-Step Verification ON.

Step 2.2: After that click on App passwords

Step 2.3: Next up select Other (Custom name)

Step 2.4: Fill in the name as msmtp for example

Step 2.5: Get the password for your msmtp configuration file

Save this password to some notepad temporarily so you can copy & paste it later to a configuration file because once you click DONE you cannot see the password again. At least I have not found a way to re-open this popup.

Βήμα 3: Μπαίνουμε στο msmtprc

sudo nano /etc/msmtprc

Βήμα 4: Διαμορφώστε το msmtprc αρχείο

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
account        gmail
host           smtp.gmail.com
port           587
from           username@gmail.com
user           username
password       password
account default : gmail

To  username@gmail.com θα πρέπει να αλλαχτεί στο  config file με το email σας

To password με  τον κωδικό του app του google

Αλλάζουμε αν θέλουμε και το username

Βήμα 5:  Κάνουμε τεστ το email

echo "Message" | mail -s "Title" youremailaddress@gmail.com

Μετά την εντολή θα πρέπει να έχεις παραλάβει το email όπου έστειλες

Βήμα 6:  Κάνουμε εγκατάσταση το curl εάν δεν το έχουμε 

apt-get install curl

Βήμα 7:  Δημιουργούμε τα αρχεία 

1. Δημιουργούμε τα παρακάτω κενά αρχεία

erxeteip.txt  

newip.txt

oldip.txt

 

2. The Script

#!/bin/bash
 
#Script to report public IP address change
#By: Soultidis D. Christos
 

curl ipinfo.io/ip > /home/username/emailscript/erxeteip.txt


oldip=`cat /home/username/emailscript/oldip.txt`
erxeteip=`cat /home/username/emailscript/erxeteip.txt`

if [[ $erxeteip = $oldip ]]
then

    #echo $oldip > /home/username/emailscript/oldip.txt
    echo "Kamia allagi"

else
    echo $erxeteip > /home/username/emailscript/newip.txt
echo $erxeteip > /home/username/emailscript/oldip.txt


    cat /home/username/emailscript/newip.txt | mailx -s "Dynamic Public IP Address" youremail@gmail.com

fi
 

Αποθήκευσε το script  σαν  ip.sh εκτελέσιμο και βάλτο σε έναν φάκελο μαζί με τα άλλα αρχεία

π.χ.

/home/username/emailscript/

Βήμα 8:  Cron και χρονική διάρκεια εκτέλεσης του script μας 

3. Cron
Πατάμε την παρακάτω εντολή στο τερματικό

 
crontab -e

Προσθέτουμε  αυτό ώστε να μπορεί να τρέχει το  script  ανα 5 λεπτά

*/5 * * * * /home/username/emailscript/ip.sh >/dev/null 2>&1

(Το 5 δηλώνει τα λεπτά , βάζουμε όσο θέλουμε εμείς)

Αποθήκευση

Γράφουμε στο τερματικό :

pgrep cron 

Άμα δεν βλέπουμε αριθμό ο  cron δεν τρέχει. Έτσι πατάμε  sudo /etc/init.d/cron start  για να ξεκινήσει

Τέλος

Advertisement

Τα σχόλια είναι απενεργοποιημένα.