Jump to content

Bash Scripting Issues


GeekGoneCrazy
 Share

Recommended Posts

Hello again all,

Looking for some assistance with a bash script I wrote. Its throwing some odd errors, and doesn't seem to be working correctly. Errors are in my loop and if statement. Shows the content of the variable and says command not found. I'm not executing a command... I'm comparing variables. I don't get it. Any pretty much any other language i've ever used this would be valid.

Related to my topic about network setup.

Its suppose to initially grab my public ip. Then every 30 minutes check and make sure the ip hasn't changed. If so update via a POST

https://gist.github.com/882540

#!/bin/bash
#
#Script to update ip on Hurricane Electric every 30 minutes
#@author Aaron Ogle

#Variables
HE_userid=<userid>
HE_passwd=<password hash>
HE_tunnelid=<tunnelid>

#Get current ip.
CurrentIP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')

#Initial update to services
echo ***Starting update script***
HE_updateparms='ipv4b='$CurrentIP'&user_id='$HE_userid'&pass='$HE_passwd'&tunnel_id='$HE_tunnelid
curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
echo 'Ip set:'$CurrentIP
#Loop to keep time updated
address='dynamic'
until [$address -eq 'static'];
   do


   #Get Current IP again.
   IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.$//')

   if ["$CurrentIP" != "$IP"];
   then
      #Could use AUTO instead of passing.  But were've already got it.
      #Might as well pass it
      curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
      echo 'Updated IP:'$IP
   else
      echo 'Unchanged.'
   fi

   #Sleep for 30 Minutes
   sleep 1800

   done

Any ideas? Thanks,

Edited by GeekGoneCrazy
Link to comment
Share on other sites

I can sort of see where you went wrong.

Your script needs to compare two different values and at the moment its not doing it. In order to make it work, you need to tell your script, to write your current ip address to a text file and then after 30 minutes compare it again with the "checkip.dyndns.org".

If the ip address from the text file does not match to the current one "checkip.dyndns.org" it will need to update the text file to reflect with the new ip address.

Edit: That's how dydndns.org does it. In order to keep track of your ADSL IP changes. But instead of text files they use database systems, like Mysql or Oracle.

Edited by Infiltrator
Link to comment
Share on other sites

Yeah, that makes sense. Can bash not compare those 2 Variables directly? It seems to keep $CurrentIP.

lol wrote to file using > filename

Now when trying to compare it doesn't work.

What does it say or do?

Link to comment
Share on other sites

The updated section of code

#Get current ip.
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > /tmp/ipaddress

#Initial update to services
echo ***Starting update script***
HE_updateparms='ipv4b='$CurrentIP'&user_id='$HE_userid'&pass='$HE_passwd'&tunnel_id='$HE_tunnelid
curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
echo 'Ip set:'$CurrentIP
#Loop to keep time updated
address='dynamic'
until [$address -eq 'static'];
   do

   #Get Current IP again.
   IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.$//')

   if [/tmp/ipaddress != $IP];
   then
      curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
      echo 'Updated IP:'$IP

./updateip: line 21: [dynamic: command not found

./updateip: line 27: [/tmp/ipaddress: No such file or directory

Obviously line 21 is invalid. As far as I'm concerned it can just remain un-found.

I tried using grep and it said command not found.

Link to comment
Share on other sites

https://gist.github.com/882540

#!/bin/bash
#
#Script to update ip on Hurricane Electric every 30 minutes
#@author Aaron Ogle

#Variables
HE_userid=<userid>
HE_passwd=<password hash>
HE_tunnelid=<tunnelid>

HE_updateparms='ipv4b=AUTO&user_id='$HE_userid'&pass='$HE_passwd'&tunnel_id='$HE_tunnelid

#Loop to keep IP updated
while [ true ]
   do

   #Get Current IP.
   NewIP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')

   if [ "$OldIP" != "$NewIP" ]
   then
      curl -sF $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php > /dev/null
      echo 'Updated IP:'$NewIP
   else 
       echo 'Unchanged'
    fi 

   #Sleep for 1 Minute
   OldIP="$NewIP"
   sleep 60
done

Ok this works. If I was to switch it over to a cron job I would need to figure out from a file.

Link to comment
Share on other sites

To read from a file, since this is only 1 line and 1 word in the file, we can get away without an array and just call it by $line:

#!/bin/bash
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > ipaddress
cat ipaddress | while read line; do
   echo "Your IPaddress is" $line
done

Link to comment
Share on other sites

The updated section of code

#Get current ip.
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > /tmp/ipaddress

#Initial update to services
echo ***Starting update script***
HE_updateparms='ipv4b='$CurrentIP'&user_id='$HE_userid'&pass='$HE_passwd'&tunnel_id='$HE_tunnelid
curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
echo 'Ip set:'$CurrentIP
#Loop to keep time updated
address='dynamic'
until [$address -eq 'static'];
   do

   #Get Current IP again.
   IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.$//')

   if [/tmp/ipaddress != $IP];
   then
      curl -F $HE_updateparms http://ipv4.tunnelbroker.net/ipv4_end.php
      echo 'Updated IP:'$IP

Obviously line 21 is invalid. As far as I'm concerned it can just remain un-found.

I tried using grep and it said command not found.

On line 21 you need to put a space between [ and $address. Same with line 27. Spaces after [ are essential to the syntax of Bash. [ is just an alias for /bin/test, so anywhere you see a [ imagine it replaced with /bin/test.

For example, you wouldn't write "until test$address..." so the same applies to [.

(Actually, when you use '[' or 'test' it doesn't really call /bin/test. For efficiency reasons it uses a shell built-in instead. But the idea is the same.)

http://tldp.org/LDP/abs/html/

Link to comment
Share on other sites

On line 21 you need to put a space between [ and $address. Same with line 27. Spaces after [ are essential to the syntax of Bash. [ is just an alias for /bin/test, so anywhere you see a [ imagine it replaced with /bin/test.

For example, you wouldn't write "until test$address..." so the same applies to [.

(Actually, when you use '[' or 'test' it doesn't really call /bin/test. For efficiency reasons it uses a shell built-in instead. But the idea is the same.)

http://tldp.org/LDP/abs/html/

Ok thanks! I didn't know that. That is what struck me as odd. Compared to other languages that should have worked. But bash does work a bit different so that does make sense.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...