LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2014, 06:17 PM   #1
mxmaniac
LQ Newbie
 
Registered: Feb 2014
Posts: 26

Rep: Reputation: Disabled
Help making this very simple bash script.


Hoping someone can help point me in the right direction. I want to make a cron job to run a bash script to e-mail my IP address when it changes.

I want to use a combination of curl and sendEmail. I know I can easily tunnel them to send an email like this.

Code:
curl ifconfig.me | sendEmail [rest of sendemail syntax]
But what I'd like to do is figure out the simplest most efficient way to make it check and only send when it changes. I'm guessing something like this might be the way to go, only in proper syntax.

First I would make sure FILE1 exists with a value, then set up something like this in a cron job.
Code:
 if     curl ifconfig.me    equals FILE1     do nothing
else if      curl ifconfig.me       does not equal FILE1     then sendEmail     and  write new results in file 1
Can anyone help with the syntax, or suggest a better way of doing this?
 
Old 02-26-2014, 07:40 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
Do you want to monitor the IP address of a local interface, or the public IP address assigned to a router or modem of some sort?

Edit: Never mind, the curl command gave the answer. See notKlaatu's post below for a straightforward solution.

Last edited by Ser Olmy; 02-26-2014 at 07:56 PM.
 
Old 02-26-2014, 07:43 PM   #3
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 897

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Without taking the cron job into account, this script would check the ip and email if different:

Code:
#!/bin/bash

CURIP=$(curl ipplz.org)
OLDIP=$(cat /path/to/some/file/containing/old/ip/address)

if [ $(echo $CURIP) != $(echo $OLDIP) ]
then 
  env MAILRC=/dev/null from="me@example.com" smtp=smtp.example.com mailx -n -s "IP Change Alert" you@destination.example.com < /path/to/message.contents.txt
  echo $CURIP > /path/to/some/file/containing/old/ip/address
fi
CURIP is the result of the command `curl ipplz.org` which i'm using instead of the overly verbose ifconfig.me
OLDIP is the contents of some file you must have on your system telling you of your old IP address

if these two values do not match,
then send the email.
and also place the new IP in the standard file keeping track of your public IP address so we can use it the next time this script runs.

Done.

So you need:
- the old ip address to be stored in some location on your harddrive. I dunno, maybe in /tmp or something.
- an email address to plug into your FROM field
- an email address to plug into your TO field
- a valid SMTP server through which you may send email

There are probably better ways to do it but that should work, I think.

Last edited by notKlaatu; 02-26-2014 at 07:44 PM.
 
Old 02-27-2014, 12:55 AM   #4
mxmaniac
LQ Newbie
 
Registered: Feb 2014
Posts: 26

Original Poster
Rep: Reputation: Disabled
Awesome, thanks so much for the help. Made one based off your code, its about 90% the same, although I used sendEmail for the mail sender. Just have to read up on Cron, and learn how to use it to run the script.

One question though. One time when I was running it, the ipplz.org site was unresponsive for a little over a minute (couldn't access it from my other PC either). Which caused the bash script to hang, before completing a minute later.

It made me worry about the possibility of what if the bash script were to get into an endless hang? Could it interfere with itself running in the future in the following cron jobs, which will probably will check every 20 minutes? Do bash scripts timeout automatically, or should I somehow specify a timeout? Or is this not really something to worry about?
 
Old 02-27-2014, 03:07 PM   #5
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 897

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Good point.

You can also try icanhazip.com to see if it is more responsive.

Either way, on curl, use

Code:
--connect-timeout [insert some number of seconds here]
Then I guess you could check to see if the new IP variable is empty; if empty, try again with icanhazip.com with timeout... and if it's still empty, then I guess email an alert to you or whatever else you want to do.
 
1 members found this post helpful.
Old 02-27-2014, 03:21 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,788
Blog Entries: 1

Rep: Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340Reputation: 1340
Quote:
Originally Posted by notKlaatu View Post
You can also try icanhazip.com to see if it is more responsive.
While this is a good tip, I think a lot of people like myself would have a problem using a service named in kitten-speak. It would embarass me for someone to see that in a script (Just kidding)

I use the more tech sounding and easy to remember ipecho:

Code:
wget -qO- http://ipecho.net/plain

Last edited by szboardstretcher; 02-27-2014 at 03:23 PM.
 
1 members found this post helpful.
Old 02-28-2014, 03:26 AM   #7
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 897

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Quote:
Originally Posted by szboardstretcher View Post
While this is a good tip, I think a lot of people like myself would have a problem using a service named in kitten-speak. It would embarass me for someone to see that in a script
Sorry, actually I didn't write that post. My cat has been at my keyboard again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
[SOLVED] making bash script more efficient The Gargantuan Programming 14 12-02-2011 10:16 AM
bash script: making options? johngreenwood Programming 4 01-04-2007 02:55 PM
Help making a simple script for mencoder. joney_bravo Programming 3 01-01-2007 07:28 PM
making a bash script student04 Linux - Software 5 01-11-2005 04:41 AM


All times are GMT -5. The time now is 06:21 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration