LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
LinuxQuestions.org Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here.

Notices


Reply
  Search this Thread
Old 07-02-2017, 10:31 PM   #1
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Rep: Reputation: 190Reputation: 190
Simple bash script to check current ip and email new ip if changed


Hello all,
I've been creating various scripts to hone my scripting skills so I thought I'd share this here. It's just a simple bash script to check current ip address and, if it has changed, email the new current ip to specified address. You may need to create a couple empty files if they don't yet exist. (e.g. currentip.txt and oldip.txt) I may add in a test to check for existence and create if necessary at a later date.

Note that you will need to change /PATH/TO/SCRIPT to the proper location
Code:
#!/bin/bash

IP=$(curl ipinfo.io/ip)
DIFF=$(diff /PATH/TO/SCRIPT/currentip.txt <(echo "$IP"))
if [ "$IP" ]
then

    if [ "$DIFF" = "" ]
    then
        :
    elif [ "$DIFF" != "" ]
    then
        mv /PATH/TO/SCRIPT/currentip.txt /PATH/TO/SCRIPT/oldip.txt        
        echo "$IP" >/PATH/TO/SCRIPT/currentip.txt
        mail -s "SUBJECT" YOUREMAIL@EXAMPLE.COM < /PATH/TO/SCRIPT/currentip.txt
    fi
else    
    :   
fi
Any questions, edits/suggestions are welcome

Last edited by TheEzekielProject; 07-02-2017 at 10:37 PM.
 
  


Reply

Tags
bash, email, ip, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check status of a command in bash script and send email alert bmxakias Linux - Server 9 11-13-2016 06:29 AM
[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins Iyyappan Linux - Server 10 07-03-2013 05:19 AM
[SOLVED] Bash script to check inodes and Email eyanu Linux - Newbie 10 09-25-2012 03:45 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Need help with script for check for a file that has changed in the last minute EclipseAgent Programming 10 02-12-2009 07:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration