Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Also know what your external ip is, wherever you are
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Sun Nov 23, 2003 7:18 pm    Post subject: Also know what your external ip is, wherever you are Reply with quote

My isp used dynamic dhcp, so my ip changes all the time. I wrote this script, with help through the gentoo forum(sorry I forgot who wrote the part to find out your ip)
anyways here is the script.
--------------------------------
Code:
ip=$(wget -O - -q http://www.whatismyip.com | grep -i title | awk '{print $4}')
echo $ip > /worktemp/ipb
diff --normal /worktemp/ipa /worktemp/ipb
if [ $? -eq 1 ]
then
cp /worktemp/ipb /worktemp/ipa
sendEmail -f linux@home -t yourcell@pcs.rogers.com -m $ip -s ip of email server
fi

--------------------------------

I forgot where I got the program sendEmail but just do a search in google, it was fairly easy.
before this script will work, you will have to create /worktemp/ipa just needs the file.

Basicly it emails a my rogers cellphone with my external ip(I'm behind a router)

I also set up a cron schedule for this script.

The script only emails the ip, if the ip changes.

anyways, hope this is of use to some of you(this was my first linux project, and successful)
_________________
In Nomine AntiX
Distort!


Last edited by AntiX on Mon Nov 24, 2003 4:33 am; edited 2 times in total
Back to top
View user's profile Send private message
s3ntient
Guru
Guru


Joined: 13 Apr 2003
Posts: 304
Location: Lyon, France

PostPosted: Sun Nov 23, 2003 8:19 pm    Post subject: Reply with quote

Nice! That's just what I needed :D
_________________
http://blog.chaostrophy.org
Back to top
View user's profile Send private message
NamShub
n00b
n00b


Joined: 24 Feb 2003
Posts: 28

PostPosted: Sun Nov 23, 2003 10:17 pm    Post subject: Reply with quote

I wrote a very similar script with a couple of more functionality, I will post it tonight when I get home...

EDIT: Here is the script. :D

First you need to download sendEmail if you want to use Email, sendSNPP if you want to send page message, and sendSMS if you want to send phone-SMS. Those are found at http://caspian.dotconf.net/menu/Software/ This script is really just an utility around those scripts; those are the tools that do all the job.

Direct download links:
sendEmail: http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.42.tar.gz
sendSMS: http://caspian.dotconf.net/menu/Software/SendSMS/sendSMS.pl.gz
sendSNPP: http://caspian.dotconf.net/menu/Software/SendSNPP/sendSNPP-v1.02.tar.gz

Note: If you have a sendmail or postfix server, you might want to change the mail-sending stuff in the script to use those instead, I guess, and drop the sendEmail script entirely. Post all your modifications here...

I suggest putting those in /usr/local/bin/

Now put the following in a script (I choose /bin/myip) and chmod +x the file...

Code:

#!/bin/bash


#########################
# Configuration Options #
#########################

#TODO: Read command line arguments instead?

IP_FILE=/tmp/ip.current

EMAIL="email@work.com email2@cellphone.com email3@hotmail.com"
EMAIL_SENDER="myemail@myisp.com"
EMAIL_HOST="localhost:25"

SMS_NUMBER="9995556789"
SMS_PROVIDER="ATTWS"

SNPP_NUMBER="9995551234"
SNPP_PROVIDER="BellMobility"
SNPP_SUPPORT_TEXT="NO"

RECONNECT="YES"

SEND_EMAIL="YES"
SEND_SMS="NO"
SEND_SNPP="NO"

ADSL_PATH="/usr/sbin"

# Download the 3 sendScripts from http://caspian.dotconf.net/menu/Software/
# and change the following for the actual installation folder
SCRIPTS_PATH="/usr/local/bin"


#########################
#    IP-Email Script    #
#########################

# Create IP file if it doesn't exist
if [[ ! -e $IP_FILE ]]
then
  touch $IP_FILE
fi

# First and foremost, test net connection
netstatus=$($ADSL_PATH/adsl-status | grep adsl-status | awk '{print $4}')
if [[ "$netstatus" = "down" ]]
then
  if [[ "$RECONNECT" = "YES" ]]
  then
    # Will fail if script isn't run as root...
    $ADSL_PATH/adsl-start
  else
    echo "Net connection seems down, exiting..."
    exit
  fi
fi

# Get IP stored in file and new IP from web
oldip=$(cat $IP_FILE)
newip=$(wget -O - -q http://www.whatismyip.com | grep -i title | awk '{print $4}')

# TODO: Use regexp to make sure $newip is valid IP address
# For now, just make sure it's not empty
if [[ "$newip" = "" ]]
then
  exit
fi

if [[ "$newip" != "$oldip" ]]
then

  # Update the file containing the IP address
  echo $newip > $IP_FILE


  if [[ "$SEND_EMAIL" = "YES" ]]
  then
    # Send an email to every addresses listed in $EMAIL
    for addr in $EMAIL; do

      # TODO: Check if valid email address
      echo "EMAILING $addr..."
      perl $SCRIPTS_PATH/sendEmail -f $EMAIL_SENDER -t $addr -m "New IP: $newip" -u "New IP: $newip" -s $EMAIL_HOST
    done
  fi

  if [[ "$SEND_SMS" = "YES" ]]
  then
    echo "SENDING SMS to $SMS_NUMBER (using provider $SMS_PROVIDER)..."
    perl $SCRIPTS_PATH/sendSMS.pl -r $SMS_NUMBER -p $SMS_PROVIDER -s $EMAIL_SENDER -m "Current IP: $newip"
  fi

  if [[ "$SEND_SNPP" = "YES" ]]
  then
    if [[ "SNPP_SUPPORT_TEXT" = "YES" ]]
    then
      msg="New IP: $newip"
    else
      # Convert dots (.) to asterisks (*)
      msg=$(sed s/[.]/*/g $IP_FILE)
    fi
    echo "PAGING $SNPP_NUMBER with message: $msg (using provider $SNPP_PROVIDER)..."
    perl $SCRIPTS_PATH/sendSNPP.pl -r $SNPP_NUMBER -p $SNPP_PROVIDER -m $msg
  fi

fi


Of course, you must edit the first part of the script your actual email address, SMS numbers & providers etc...

I couldn't test the SMS stuff because my cellphone is old, but if someone could try it and post experience here I could correct the script if anything doesn't work...

Also if you don't use adsl-start/adsl-status well I guess you need to change that part to, or remove it altogether... I guess it would be best to have another script that makes sure the connection is always up and have that script in your crontab...

If anyone feels like doing the TODOs, feel free! :lol:


Last edited by NamShub on Mon Nov 24, 2003 5:07 am; edited 4 times in total
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Sun Nov 23, 2003 11:02 pm    Post subject: Reply with quote

cool, thx man, I plan on making one that will message a pager with the ip.
you know when you page a numeric pager and use numerials, and * 's on the keypad, it puts spaces/dashes in the numeric message.
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
NamShub
n00b
n00b


Joined: 24 Feb 2003
Posts: 28

PostPosted: Mon Nov 24, 2003 3:02 am    Post subject: Reply with quote

Next step, reading the IP through speech and saving it to a wav file, converting the wav file to a voicemail file (.rmd?) and using a voice-capable modem to actually call the cellphone and read the IP out loud :lol: :lol: :lol:

Or if you have a vgetty answering machine/voicemail server, add the IP to your voicemail so you can phone home and always know your current IP... Ultimate geek-ness, but cool in a way I guess hehe :lol: :roll:
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Mon Nov 24, 2003 3:08 am    Post subject: Reply with quote

that does sound cool, heh, I just like making cool things in linux.
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
razamatan
Apprentice
Apprentice


Joined: 28 Feb 2003
Posts: 160

PostPosted: Mon Nov 24, 2003 5:56 am    Post subject: Re: Also know what your external ip is, wherever you are Reply with quote

Code:
wget -O - -q http://www.whatismyip.com | grep h1 | cut -f 4 -d " "


i'd suggest this because `cut` is less resource intensive than `awk` and it's guaranteed to be on nearly all systems... it's a VERY minor change...

i actually use this line as an alias command. thanks dude.
_________________
a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity"
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Mon Nov 24, 2003 6:06 am    Post subject: Reply with quote

oh right on, man. Thanx, since I'm still new to linux scripting, and linux in general, its a real help with the suggestions.


I can not make the script more effiencent with the 'cut' command. Any's thanx for the input 8)
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
dub.wav
Tux's lil' helper
Tux's lil' helper


Joined: 09 Apr 2003
Posts: 149
Location: Norway

PostPosted: Mon Nov 24, 2003 9:10 am    Post subject: Reply with quote

Although there's no real point in it, I just want to point out that the emailing can be done in "pure" bash :)
Code:

{
echo "HELO ip"
echo "MAIL FROM:example@example.com"
echo "RCPT TO:example@example.com"
echo "DATA"
echo "From: example"
echo "To: example"
echo "Subject: IP"

echo "$ip"
echo
echo
echo .
} > /dev/tcp/smtp.example.com/25

Btw...Does someone know if bash can be used to download web pages as well?
Back to top
View user's profile Send private message
noganex
n00b
n00b


Joined: 20 Aug 2003
Posts: 57
Location: Germany

PostPosted: Mon Nov 24, 2003 3:55 pm    Post subject: Reply with quote

Goto dyndns.org and register an free account.

Quote:
emerge ez-ipupdate


Create a /etc/dyndns.conf
Quote:
#!/usr/bin/ez-ipupdate -c
service-type=dyndns
user=username:password
host=yourhost.dyndns.org
interface=ppp0
max-interval=2073600
cache-file=/tmp/ez-ipupdate.cache


Add a cronjob:
Quote:
crontab -e

0,10,20,30,40,50 * * * * /usr/bin/ez-ipupdate -c /etc/dyndns.conf >/dev/null 2>&1


So.. yourhost.dyndns.org will be checked every 10 Minutes. If an update is necessary, it will be updatet. ;)


mfg
noganex
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Mon Nov 24, 2003 4:14 pm    Post subject: Reply with quote

you can also go to www.no-ip.com & download the utility for linux, to update your domain, with your current external ip. I discovered this aswell, but I still wanted to make the cellphone ip script anyways, for the fun of it.
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
X
Apprentice
Apprentice


Joined: 30 Apr 2002
Posts: 192
Location: Lexington KY

PostPosted: Mon Nov 24, 2003 10:20 pm    Post subject: Reply with quote

AntiX wrote:
you can also go to www.no-ip.com & download the utility for linux, to update your domain, with your current external ip. I discovered this aswell, but I still wanted to make the cellphone ip script anyways, for the fun of it.


there's an ebuild... emerge noip-updater
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Tue Nov 25, 2003 7:29 am    Post subject: Reply with quote

oh, right on, I didn't even look
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
tecknojunky
Veteran
Veteran


Joined: 19 Oct 2002
Posts: 1937
Location: Montréal

PostPosted: Fri Jan 02, 2004 2:32 am    Post subject: Reply with quote

Want the ip of your default route? How about a simple one-liner?

Code:

echo $(ifconfig `sed '/^[^\t]*\t00000000/!d;s/\t.*//' /proc/net/route` | sed -n 's/\ *inet addr://p' | sed 's/\ .*//')


Enjoy.

PS. Remember, this is a one-liner. Don't be fooled by your browser wrapping the line.
_________________
(7 of 9) Installing star-trek/species-8.4.7.2::talax.
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Fri Jan 02, 2004 2:56 am    Post subject: Reply with quote

I've been working on a script that emails my cell when one of my game servers goes offline. By using wget and grep, but I need to learn more about awk, I want to make it so that it tells me which server went offline. Anyways check out www.distortgaming.com under the server status

or www.distortgaming.com/modules.php?name=servers

If any of you have and ideas or help, that would be cool. Um so far it takes 2 scripts, one that detects the server going down, and writing a variable(we will call it A) to HD, and one that detects when the server goes back up, if the variable A is 1, then if server goes up, set variable A to 0. The scripts are in a cron tab aswell.
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
ecatmur
Advocate
Advocate


Joined: 20 Oct 2003
Posts: 3595
Location: Edinburgh

PostPosted: Fri Jan 02, 2004 3:30 am    Post subject: Reply with quote

dub.wav wrote:
Btw...Does someone know if bash can be used to download web pages as well?


Easy:
Code:
{
  {
    echo GET "/search?q=gentoo+linux"
    echo
  } >&0
  cat
} <>/dev/tcp/google.com/http
FTP might be a little trickier, though...

Surprised no-one's suggested using /usr/sbin/sendmail to send the email message - slightly less l33t than echoing to port 25, but more robust, and works on all systems without requiring Perl scripts or the like.
Back to top
View user's profile Send private message
AntiX
Tux's lil' helper
Tux's lil' helper


Joined: 04 Oct 2003
Posts: 76
Location: Edmonton

PostPosted: Fri Jan 02, 2004 4:14 am    Post subject: Reply with quote

Actualy I was going to use sendmail, but I couldn't get it working fast enough(I'm a linux newbie, and at the same time, due to lack of patience or short attention span) Sendemail I figured out easily.

Actualy the only reason why I can even script in or even use linux, is from past programming experience. Heheh, remember the old Commodore 64 or Atari 8-bit systems? I gave a new meaning to undocumented features.
_________________
In Nomine AntiX
Distort!
Back to top
View user's profile Send private message
dub.wav
Tux's lil' helper
Tux's lil' helper


Joined: 09 Apr 2003
Posts: 149
Location: Norway

PostPosted: Fri Jan 02, 2004 6:35 pm    Post subject: Reply with quote

ecatmur wrote:
dub.wav wrote:
Btw...Does someone know if bash can be used to download web pages as well?


Easy:
Code:
{
  {
    echo GET "/search?q=gentoo+linux"
    echo
  } >&0
  cat
} <>/dev/tcp/google.com/http
FTP might be a little trickier, though...

Surprised no-one's suggested using /usr/sbin/sendmail to send the email message - slightly less l33t than echoing to port 25, but more robust, and works on all systems without requiring Perl scripts or the like.
Thanks! :) I've tried googling and looking at the advanced bash scripting guide, but none had any info on doing the above.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum