LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-30-2004, 05:27 PM   #1
np complete
Member
 
Registered: May 2004
Distribution: Fedora Core 2/3
Posts: 51

Rep: Reputation: 15
help writing shell script


Okay,

I am hosting a webserver and ssh from my house for a project a friend and I are working on. Being that this is from my house, I have a dynamically assigned IP address. I was trying to find a way to write a shell script that would determine my external IP address, and mail it to him every few hours.

I've been messing around with ifconfig but can't figure out how to get the external address. I'm also aware that I will need to use cron for scheduling this script to run.

thanks in advance for any advice/pointer you can give

-NPC
 
Old 08-30-2004, 05:34 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,969

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
What type of internet connection are you using, cable, ADSL etc..? Are you using a router?

Use a free dynamic IP DNS service like www.noip.com. No need to worry if your IP changes.
 
Old 08-30-2004, 05:45 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Assuming that the computer is directly connected to the ISP (no intervening routers), and that eth0 is the interface being used, you can execute this command:
Code:
/sbin/ifconfig eth0 | grep "inet addr" | cut -f 2 -d ':' | cut -f 1 -d ' '
There's probably a more elegant way using sed, awk, or some other such utilitiy, but this works.
 
Old 08-30-2004, 05:46 PM   #4
np complete
Member
 
Registered: May 2004
Distribution: Fedora Core 2/3
Posts: 51

Original Poster
Rep: Reputation: 15
hmm interesting. thanks.

i am using dsl, with a linksys wireless router. i do have the internal ip's static though for port forwarding purposes
 
Old 08-30-2004, 06:09 PM   #5
Joubert79
Member
 
Registered: Jul 2004
Location: Manchester, UK
Distribution: Gentoo
Posts: 46

Rep: Reputation: 15
If you don't want to register a name with a DNS and update your changing ip with something ddclient, you could resolve your ip from whatismyipaddress.com by

wget -O - http://www.whatismyipaddress.com 2> /dev/null | grep IP | awk '{print $6}'

So, you might try something like the following
Code:
#!/bin/bash

WORKING_DIR=/a/working/directory
cd ${WORKING_DIR}

if [ -e ip.last ] ; then
    mv ip.last ip.old
fi

wget -O - http://www.whatismyipaddress.com 2> /dev/null | grep IP | awk '{print $6}'  > ip.last

if [ ! -e ip.old ] || [ -n "`diff ip.last ip.old`" ] ; then
  (Your Mailing Code)
fi
You could try mailx to mail the ip to your friend. You could set up a cron job, or add the executable script to your cron.hourly directory, say.
 
Old 08-30-2004, 06:14 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,969

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
If you are really interested in doing it yourself then search the website.
http://www.linuxquestions.org/questi...address+script
 
Old 08-30-2004, 09:43 PM   #7
np complete
Member
 
Registered: May 2004
Distribution: Fedora Core 2/3
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Joubert79
If you don't want to register a name with a DNS and update your changing ip with something ddclient, you could resolve your ip from whatismyipaddress.com by

wget -O - http://www.whatismyipaddress.com 2> /dev/null | grep IP | awk '{print $6}'

So, you might try something like the following
Code:
#!/bin/bash

WORKING_DIR=/a/working/directory
cd ${WORKING_DIR}

if [ -e ip.last ] ; then
    mv ip.last ip.old
fi

wget -O - http://www.whatismyipaddress.com 2> /dev/null | grep IP | awk '{print $6}'  > ip.last

if [ ! -e ip.old ] || [ -n "`diff ip.last ip.old`" ] ; then
  (Your Mailing Code)
fi
You could try mailx to mail the ip to your friend. You could set up a cron job, or add the executable script to your cron.hourly directory, say.
thanks alot man! i appreciate it greatly
 
  


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
[SOLVED] Writing a shell script mattz40 Debian 5 04-13-2005 06:56 PM
writing a shell script tchoua Linux - General 8 02-24-2005 06:18 PM
shell script writing ust Linux - General 6 01-24-2005 01:06 PM
writing my first shell script speedhead34 Linux - Newbie 4 12-20-2004 12:50 AM
Need Help in Writing a Shell Script. KptnKrill Linux - General 2 05-19-2003 05:44 AM


All times are GMT -5. The time now is 03:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration