LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help writing shell script (https://www.linuxquestions.org/questions/linux-newbie-8/help-writing-shell-script-224489/)

np complete 08-30-2004 05:27 PM

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

michaelk 08-30-2004 05:34 PM

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.

Dark_Helmet 08-30-2004 05:45 PM

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.

np complete 08-30-2004 05:46 PM

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

Joubert79 08-30-2004 06:09 PM

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.

michaelk 08-30-2004 06:14 PM

If you are really interested in doing it yourself then search the website.
http://www.linuxquestions.org/questi...address+script

np complete 08-30-2004 09:43 PM

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


All times are GMT -5. The time now is 12:10 AM.