Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
BASH - Get external IP and upload it to a FTP server
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
bazik
Retired Dev
Retired Dev


Joined: 22 Jul 2002
Posts: 277
Location: Behind you.

PostPosted: Tue Dec 10, 2002 6:26 pm    Post subject: BASH - Get external IP and upload it to a FTP server Reply with quote

I have a dynamic IP at home and want to be able to access my Linux box via SSH from work without using a dyndns provider.
This little script gets the the current external IP (from dyndns.org, how ironic :) ), writes it into a HTML file and uploads it to a FTP server. For additional security, I protected the directory on my webserver with a .htaccess file (luck that my provider supports this :) ).
Hope this helps anyone, improvment ideas are appreciated!

Code:

#!/bin/bash
##################################################################
#                                                                #
# upip.sh - Get external IP and upload it to a ftp server.       #
# Written by Sven Blumenstein <me@0x1337.net>                    #
#                                                                #
##################################################################
#                                                                #
# for security reasons, this file does not contain the username  #
# and password of the ftp server. for authentication, create a   #
# file called ".netrc" in the home directory of the user running #
# this script and write the following in it:                     #
#                                                                #
# machine yourdomain.com login youruser password yourpass        #
#                                                                #
# dont forget to "chmod 0400 .netrc"!                            #
# if you do a "ftp yourdomain.com" now, ftp gets the login data  #
# from ".netrc".  see "man netrc" for more info.                 #
#                                                                #
##################################################################

DOMAIN=yourdomain.com
DIR=myip
IP_PATH=/tmp/index.html
IP_FILE=`basename $IP_PATH`
MACHINE=`hostname`

# TIME=`date +%H:%M:%S`

if [ `uptime | awk -F, '{print NF}'` = 5 ]; then
  TIME=`uptime | awk -F, '{print $1}'`
else
  TIME=`uptime | awk -F, '{print $1, $2}'`
fi

IP=`wget -q -O - http://checkip.dyndns.org | grep Address | awk '{print $4}'`

 if [ -e $IP_PATH ]; then
    rm -f $IP_PATH
 else
    touch $IP_PATH
 fi

cat << EOF > $IP_PATH
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
 <head>
 <title>External IP Address of $MACHINE</title>
 </head>
 <body><center><p><br><br>
 IP: $IP<br>$TIME
 </p></center></body>
 </html>
EOF

 (echo cd $DIR; echo del $IP_FILE; echo put $IP_PATH $IP_FILE; echo bye) | ftp $DOMAIN 1>&2>/dev/null
Back to top
View user's profile Send private message
Binestar
n00b
n00b


Joined: 14 Nov 2002
Posts: 58

PostPosted: Tue Dec 10, 2002 7:57 pm    Post subject: Reply with quote

Pretty good script, but I don't like that it rely's on dyndns to report your IP Address to the script.

You can just use /sbin/ifconfig

Code:

#!/bin/bash
##################################################################
#                                                                #
# publiship.sh - Get external IP and upload it to a ftp server.  #
# Written by Sven Blumenstein <me@0x1337.net>                    #
# Modified by Christopher Fisk <chrisf@NOSPAM-mhonline.net       #
#                                                                #
##################################################################
#                                                                #
# dont forget to "chmod 0700 publiship.sh"!                      #
#                                                                #
# USE: Add this script to your crontab at an interval that       #
#  suits you.  15 minutes should be fine.                        #
#                                                                #
# Make sure that the FTPDIR exists.  This script will error out  #
#  if it does not.                                               #
#                                                                #
# If you want to setup a second crontab entry to upload the      #
#  IP address to the FTP server even if the IP Address hasn't    #
#  changed you can use the command 'publicip.sh override'        #
#                                                                #
# For example:                                                   #
#  */15 * * * * /path/to/publiship.sh                            #
#  3 */3 * * * /path/to/publiship.sh override                    #
#                                                                #
# The first crontab entry runs publiship.sh normally, exiting    #
#  if the IP Address has not changed.                            #
# The second crontab entry runs publiship.sh with the override   #
#  flag set.  That causes the IP Address to be uploaded again    #
#  thus setting a new timestamp on the file.  This can also      #
#  you to know if your computer has possibly crashed/lost it's   #
#  internet connection.                                          #
#                                                                #
##################################################################

OVERRIDE=$1
DOMAIN=yourdomain.tld
FTPUSER=ftpusername
FTPPASS=ftppassword
FTPDIR='/path/to/webfolder/for/upload/'
IP_PATH=/tmp/index.html
OLDIPFILE=/tmp/oldip
MACHINE=`/bin/hostname`

IP=`/sbin/ifconfig eth0 | grep inet | awk '{print $2}'| awk -F : '{print $2}'`

if [ "$OVERRIDE" == "override" ]; then
  IPCHANGED=1
else
  if [ -e "$OLDIPFILE" ]; then
    OLDIP=`cat $OLDIPFILE`
    if [ "$OLDIP" = "$IP" ]; then
      IPCHANGED=0
    else
      IPCHANGED=1
    fi
  else
    IPCHANGED=1
  fi
fi

if [ "$IPCHANGED" = "1" ]; then
  if [ `uptime | awk -F, '{print NF}'` = 5 ]; then
    TIME=`uptime | awk -F, '{print $1}'`
  else
    TIME=`uptime | awk -F, '{print $1, $2}'`
  fi

echo $IP > $OLDIPFILE
cat << EOF > $IP_PATH
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>External IP Address of $MACHINE</title>
</head>
<body><center><p><br><br>
$MACHINE<br>IP: $IP<br>$TIME
</p></center></body>
</html>
EOF


ncftpput -u $FTPUSER -p $FTPPASS -a $DOMAIN $FTPDIR $IP_PATH

else
 echo 'IP Still the same'
fi


Make sure that code is mode 0700 and only the owner of the script will be able to read it. It's just as secure as having a .netrc file.

What my script does is cuts down on the amount of times that it sends the updated information to the ftp server, which the admin of the server will like.

BTW: I like the idea enough to edit it for my use, thanks =)

Binestar


Last edited by Binestar on Tue Dec 10, 2002 9:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
bazik
Retired Dev
Retired Dev


Joined: 22 Jul 2002
Posts: 277
Location: Behind you.

PostPosted: Tue Dec 10, 2002 9:26 pm    Post subject: Reply with quote

Binestar wrote:
Pretty good script, but I don't like that it rely's on dyndns to report your IP Address to the script.

You can just use /sbin/ifconfig


Thanks for your reply and the modified code!

The reason I dont use ifconfig is, that the machine I want to access is not directly connected to the Internet. The connection goes this way:

Linux box -> Switch -> Win2K box with 2 NIC -> DSL Modem

The Win2K box is running a Firewall and Proxy. I activated IP forwarding to get direct connection from the Linux box to the Internet (using the Win2K box as gateway). On the Win2K box I forward one port to port 22 on the Linux box for SSH access :)


P.S.: Looking over your code, here is one hint:
If you want to run something in crontab in regular intervalls (for example 15 minutes), you can use a slash to indicate the intervall instead of writing each cycle:

Code:

*/15 * * * * /blah/blub/bleh.sh

is the same as
Code:

0,15,30,45 * * * * /blah/blub/bleh.sh


I also noticed that my DSL modem times out too fast and therefore the time window to connect is a bit small. So I added this line to keep the connection open for 3 minutes:

Code:

ping -c 18 -i 10 $DOMAIN 1>&2>/dev/null


(make 18 pings and wait 10 seconds after each)

P.P.S.:
Just if someone wonders why I dont use a DynDns provider: Having a static DNS (to a dynamic IP) makes you more attackable to have a dynamic IP which changes on every reconnect :) (just my opinion and aslong as I dont run a private webserver, I dont need a static DNS / IP )

regards,
bazik
Back to top
View user's profile Send private message
Twink
Apprentice
Apprentice


Joined: 06 Dec 2002
Posts: 178
Location: New Zealand

PostPosted: Thu Dec 12, 2002 4:55 am    Post subject: Reply with quote

I have a similar script but I text it to my cell phone using a icq perl script I found, I was gonna make it so that everytime my ip changes it sends it to me, but I realised I have static, so no point.
Back to top
View user's profile Send private message
charlieg
Advocate
Advocate


Joined: 30 Jul 2002
Posts: 2148
Location: Manchester UK

PostPosted: Fri Dec 13, 2002 5:31 pm    Post subject: Hehe Reply with quote

Using a Win2k box as a router/firewall for your linux boxen. Almost poetic in it's irony.
_________________
Want Free games?
Free Gamer - open source games list & commentary

Open source web-enabled rich UI platform: Vexi
Back to top
View user's profile Send private message
Raccroc
n00b
n00b


Joined: 08 Sep 2002
Posts: 46
Location: Austin

PostPosted: Fri Dec 13, 2002 6:05 pm    Post subject: Reply with quote

Very nice...One piece of advice though.

Setup your keys and use scp instead of ftp. Much more secure and no passwords over the wire.
Back to top
View user's profile Send private message
bazik
Retired Dev
Retired Dev


Joined: 22 Jul 2002
Posts: 277
Location: Behind you.

PostPosted: Fri Dec 13, 2002 11:07 pm    Post subject: Re: Hehe Reply with quote

charlieg wrote:
Using a Win2k box as a router/firewall for your linux boxen. Almost poetic in it's irony.


Its not fully under my control so I cant change this :(
Back to top
View user's profile Send private message
bazik
Retired Dev
Retired Dev


Joined: 22 Jul 2002
Posts: 277
Location: Behind you.

PostPosted: Fri Dec 13, 2002 11:09 pm    Post subject: Reply with quote

Raccroc wrote:
Very nice...One piece of advice though.

Setup your keys and use scp instead of ftp. Much more secure and no passwords over the wire.


Nope, I dont have SSH access to my ISPs webserver :D
Back to top
View user's profile Send private message
Slack006
Tux's lil' helper
Tux's lil' helper


Joined: 10 Jan 2003
Posts: 97
Location: Desert of AZ

PostPosted: Sun Jan 26, 2003 5:45 am    Post subject: Help the N00b Reply with quote

Ok, I'm trying to modify this code for my personal use, and from what I can tell it's ready to go. I keep getting "line 73: syntax error: unexpected end of file" :( I've gone over the code a million times, but I am no expert. Any of you jedis see the obvious mistake I am making?

Code:
#!/bin/bash
############################################
# This script is used to retreive the      #
# external IP Address from a Linksys       #
# BEFSR41 Firewall Router.                 #
#                                          #
#     Last Modified: 01/25/03              #
############################################

### VARIABLES ###
OVERRIDE=$1
REM_HOST=ftp.mydomain.com
REM_DIR=/
OLD_IP_FILE=/storage/last_ip
IP_PATH=/
IP_FILE=index.html
MACHINE=home.mydomain.com
LOC_IP_FILE=/tmp/index.html

# Get's the Status page from the router, and places it in the /tmp directory.
wget -q -P/tmp --http-user= --http-passwd=firewallpassword 192.168.6.1/Status.htm

# Retreives the IP address from the mess that is the status page
IP=$(less /tmp/Status.htm | grep "IP Address" | awk '{print $236}' | awk -F '>' '{print $2}' | awk -F '<' '{print $1}')

# Removes Status.htm from the /tmp directory
rm -f /tmp/Status.htm

# Checks to see if the 'override' command was given.
# If it is not in override mode, then checks to see if IP
# has changed...
if [ "$OVERRIDE" == "override" ]; then
  IPCHANGED=1
else
  if [ -e "$OLD_IP_FILE" ]; then
    OLDIP=`cat $OLD_IP_FILE`
    if [ "$OLDIP" = "$IP" ]; then
      IPCHANGED=0
    else
      IPCHANGED=1
    fi
  else
    IPCHANGED=1
  fi
fi

# Stores current IP for next use.
echo $IP > $OLD_IP_FILE

# Builds HTML page for display.
if [ "$IPCHANGED" = "1" ]; then

  cat << EOF > $LOC_IP_FILE
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>External IP Address of $MACHINE</title>
</head>
<body><center><p><br><br>
$MACHINE<br>IP: $IP<br>
</p></center></body>
</html>
EOF

# Uploads file to remote server.
  (echo cd $REM_DIR; echo del $IP_FILE; echo put $IP_PATH $IP_FILE; echo bye) | ftp $REM_HOST 1>&2>/dev/null

  rm -f $LOC_IP_FILE
else
  echo 'IP Still the same'
fi

_________________
Slack

May the SOURCE be with you...
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