GeekGoneCrazy Posted March 23, 2011 Posted March 23, 2011 (edited) 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 March 23, 2011 by GeekGoneCrazy Quote
Infiltrator Posted March 23, 2011 Posted March 23, 2011 (edited) 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 March 23, 2011 by Infiltrator Quote
GeekGoneCrazy Posted March 23, 2011 Author Posted March 23, 2011 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. Quote
Infiltrator Posted March 23, 2011 Posted March 23, 2011 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? Quote
GeekGoneCrazy Posted March 23, 2011 Author Posted March 23, 2011 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. Quote
GeekGoneCrazy Posted March 23, 2011 Author Posted March 23, 2011 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. Quote
digip Posted March 23, 2011 Posted March 23, 2011 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 Quote
Sitwon Posted March 23, 2011 Posted March 23, 2011 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/ Quote
GeekGoneCrazy Posted March 25, 2011 Author Posted March 25, 2011 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.