Search code examples
bashgpslocationmailx

Changing IP log with GPS information and mail. I need robustness


I've created a script in order to receive a mail with wan ip information and GPS location of my macbookpro. The content of the script is this:


#!/bin/bash
# -*- ENCODING: UTF-8 -*-

if [ ! -e /tmp/ip ]; then
  curl -s icanhazip.com > /tmp/ip
fi
curl -s icanhazip.com > /tmp/ip2
newip=$(diff /tmp/ip /tmp/ip2 | wc -l)
if [ $newip -ne 0 ]; then 
  mv -f /tmp/ip2 /tmp/ip
  date > IPlog.txt
  curl -s icanhazip.com >> IPlog.txt
  sudo ./Downloads/whereami >> IPlog.txt
  mailx [email protected] < IPlog.txt
  rm IPlog.txt  
else 
  rm /tmp/ip2 
fi

Every minute the sistem executes this script that verifies if the wan ip has changed. If it has changed, the script send me a mail with the new information. The problems are:

1.- The mail is not always correctly sent. Sometimes I don't reveive it.

2.- The mail isn't contain all the info. Sometimes it includes only the new wan ip adress.

3.- Sometimes the mail is qualified as spam and I don't know why because the sender is always the same adress.


Solution

  • I have some suggestions to debug your problems.

    First you should use a different location to store the ip than tmp. If your system wipes your tmp folder on boot and your system gets a new WAN ip after boot you would loose the previous recorded ip.

    Check the exit code of mailx when sending using $?. 0 is ok. You could do a while loop and keep trying to send it until you get exit code 0.

    You could add the info for the mail to a local variable instead of a file.

    IPLog=`date`
    IPLog+=`curl -s icanhazip.com`
    

    The spam problem might be due to the IP address in the mail. Or whatever ./Downloads/whereami is adding to the file. Adding the sending email address as a trusted sender might do it.

    Check the email header for information about spam score.