Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to check a dynamic ip

Status
Not open for further replies.

wlf

Technical User
Sep 25, 2002
47
0
0
US
Hi, I need a shell script to check a dynamic ip(from cable company), it will send the ip to my email if the ip changes. The Solaris box is behind the router with a static ip (internal), the router connected with a cable modem for internet connection. The problem is the cable company assigned you a dynamic ip, and it changes sometime. When that happens, I can't not access my Solaris box outside my home. On the local box I can know my current external ip through the How can I write that in a script to send the ip to my email if it changes? I'm using Bourne shell.
 
I have used the following in the past:

#!/bin/sh

GETIP=`lynx -crawl -dump | grep "Your IP is"`
IP=`echo $GETIP | awk '{print $4}'|tail -1`
mail -s "my new ip is $IP" mailbox@hotmail.com

##########

could stand some cleaning up, then loop it, save your currentip and then test against your prev value and if it changes then send the mail. I used it as a heartbeat so just had cron run it every hour.
 
Thanks for your replies. I copied the script

#!/bin/sh

GETIP=`lynx -crawl -dump | grep "Your IP is"`
IP=`echo $GETIP | awk '{print $4}'|tail -1`
mail -s "my new ip is $IP" xxxx@hotmail.com

and saved as getip. I ran getip and following is the output.

# ./getip
./getip: lynx: not found
awk: syntax error near line 1
awk: bailing out near line 1
#

Did I miss something? Thanks. -wolf
 
use no-ip.com

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
sorry, yes you will need to have lynx installed (available for most *nix's). lynx is a character based http client that will return data to stdout that can be used with other utilities.
whatismyip.com is similar to checkip.dyndns.org, you could use either, just change the string grep is looking for.
 
Thanks for your reply. I installed lynx and ran the script and following is the output.

# ./ckip1
ld.so.1: lynx: fatal: libncurses.so.5: open failed: No such file or directory
#

Any more changes needed? Thanks.
 
stefanwagner:
ifconfig will not tell you the external ip that the isp has assigned to the router that is between his Solaris box and the internet. it will tell you what the ip is on his local network which won't help his problem.

wlf:
it looks like a dependency of lynx is not installed (ncurses?)
 
router <> (dsl or cable modem) ppp connection ... while the router itself may use ppp to connect to the isp ...

 
Thanks all. After installed ncurses I ran the script and no erro. Since my ip has not changed, and it won't send mail to me. I don't know how to test if it sends mail to me. I just typed

# mail "test" xxxx@hotmail.com

and it hangs.
 

It's not necessarily hanging, it could be waiting for you to type your email...
 
If you are interactively typing that command then yes it is waiting for input. ( you missed the -s parameter to make the subject of your email "test" ).

to end your email, at the beginning of a new line type "~." without the quotes.
 
use this command to test the mail:

echo "This is the body message...." | mailx -s "testing mail" xxx@mail.com

obviusly, use a valid email.

cheers.
 
I use a linux script called "ipcheck" to automatically update the IP address.. much easier with this

It will check your IP address either by logging onto your router and looking there, OR it will use the checkip website to obtain it..

a copy can be found here:
It works really well

Chris
 
I use dyndns.org to associate a free DNS name with my home system. Here's a script that I've used for the past couple of years to keep up IP address updated on my Solaris system. It's cron'ed up and runs hourly. You could modify it to mail instead of update a DNS name.


Code:
#!/usr/bin/ksh

# Used for updating the IP only when necessary.
INTERFACE=hme0
DNSNAME=ninjafodder.homeunix.org
USERNAME=bogus
PASSWORD=bogus
NEWIP=`netstat -rn | grep $INTERFACE | head -1 | awk '{ print $2 }'`
OLDIP=`cat /var/adm/OLD-IP.txt`

echo $NEWIP > /var/adm/NEW-IP.txt

if [ "$NEWIP" != "$OLDIP" ]; then
        # Update IP Address
        echo $NEWIP > /var/adm/OLD-IP.txt
        /usr/local/bin/ez-ipupdate -S dyndns -u $USERNAME:$PASSWORD -h $DNSNAME -i $INTERFACE > /dev/null 2>&1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top