Script to check external ip and send to mail

Maybe it's useful for somebody.
Checks ip and sends to your email., if it has changed. Save as ipcheck.sh and put on /home/USER/
To check every hour, execute crontab -e and add this to the end:

0 * * * * sh ipcheck.sh

Here it goes:

#!/bin/sh

#########################
## uncomment and edit as needed
#########################
#mail_to="[email protected]"
#mail_from="[email protected]"
#user="[email protected]"
#pass="password123"
#host_params="--host=smtp.sapo.pt --port=587 --tls=on --tls-starttls=on --auth=on"

#########################
## do not edit lines bellow
#########################
ip_old=""
ip_new=""
if [ -f ip.txt ]; then ip_old="$(cat ip.txt)";  fi
ip_new="$(curl --connect-timeout 10 --max-time 10 --retry 3 --retry-delay 0 -s icanhazip.com)"

if [ "$ip_new" != "" ] && [ "$ip_new" != "$ip_old" ]
then
echo $ip_new > ip.txt
printf "To:$mail_tonSubject: IPnnOld ip = $ip_oldnNew ip = $ip_new" | msmtp --timeout=10 -t --from=$mail_from --user=$user --passwordeval="echo '$pass'" $host_params
fi

Kajika,

I am using a python script that push in a private git repository (so I even have history). I am using dig +short myip.opendns.com @resolver1.opendns.com command to check ip.

rambos,

Thx, this is interesting. Im using duckdns atm, but would be nice to replace it with some local scrip if possible

murkom,

I also use duckdns.
But as i like doing little scripts and wanted to try this, i remembered to share here.

Fryboyter,

I would recommend always testing shell scripts with github.com/koalaman/shellcheck. Often there is room for improvement that you don’t think of yourself.

murkom,

Nice one. Did not know about it.

MoriGM,
@MoriGM@feddit.de avatar

If you test it let us now how it's worked with the script.

Fubarberry,
@Fubarberry@lemmy.fmhy.ml avatar

Pretty useful, definitely going to save this.

My home IP almost never changes, so I’d hate to pay for a static IP.

unwantedpamphlet,
@unwantedpamphlet@mastodon.social avatar

@Fubarberry @murkom noip.com has a free service I’ve used a bunch. Just run an app at home and select a domain. Some routers support the service too.

Fubarberry,
@Fubarberry@lemmy.fmhy.ml avatar

Thanks!

manned_meatball,
@manned_meatball@lemmy.ml avatar

cool, I’ve been using curl -s ifconfig.me after dig +short myip.opendns.com stopped working

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • [email protected]
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • oklahoma
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • SuperSentai
  • feritale
  • KamenRider
  • All magazines