0

im trying to figure out, b/c sometimes pings will work but thats b/c its just reg. ICMP but when real traffic tries to go through the tunnel it wont work b/c there are 2-3 tunnels up. I want to do an IF $tunnels > 1 AND $tunnels == 0 to do the below of restarting IPSec.

not that easy

#!/bin/bash

echo begin ping

ping -c 3 -w 3 -t 2 192.168.1.4 &> /dev/null ;

service ipsec status | awk 'NR==3' | cut -d" " -f1 $tunnels

if

        [ $? ==  0 ]

then
        echo "Connection is up" >> /root/restart_ipsec.log

else

        echo "Connection is down" >> /root/restart_ipsec.log

        date >> /root/restart_ipsec.log;

        /sbin/service crond stop >> /root/restart_ipsec.log;

        /sbin/service ipsec stop >> /root/restart_ipsec.log;

        sleep 120;

        /sbin/service ipsec start >> /root/restart_ipsec.log;

        /sbin/service crond start >> /root/restart_ipsec.log;

fi

i tried this BUT it keeps using the first if no matter what. even if i say the amount of tunnels is greater then 1 or 0 it still uses that statement. What gives!?

tunnels=$(service ipsec status | awk 'NR==3' | cut -d" " -f1 | sed -e 's/^[ ]*//')
a=0
if
        (( $tunnels > $a));
then
        echo "To many tunnels =" $tunnels >> /root/restart_ipsec2.log;
        echo $a;
elif
        [ $tunnels == 0 ]
then
        echo "To many tunnelss =" $tunnels >> /root/restart_ipsec2.log
elif
        echo "luis3";
        [ $? ==  0 ]
then
        echo "Connection is up" >> /root/restart_ipsec2.log
else
        echo "Connection is down" >> /root/restart_ipsec2.log
fi
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Luis
  • 83
  • 2
  • 10
  • `[ $? == 0 ]` is testing whether or not `cut` succeeded. Is that what you want? Tell is in simple English what you're trying to do so we can try to help you. – Ed Morton Jul 29 '13 at 17:55
  • the $? == 0 is asking if the ping replied or not, BUT i also need to find out how many tunnels ipsec has. if 1 thats ok, if more then 1 OR 0 for it to restart the service. – Luis Jul 30 '13 at 19:37
  • $? is the exit status of the last run command which is `cut` in your script, not `ping`. – Ed Morton Jul 30 '13 at 21:04

1 Answers1

1

Try [ $? -eq 0 ], as it is a numeric comparision.

Also, this line:

service ipsec status | awk 'NR==3' | cut -d" " -f1 $tunnels

does not make many sense. If you want it to print the first field then do:

service ipsec status | awk 'NR==3{print $1}'

what I don't know is the use of $tunnels in the line you have.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • im defining the variable $tunnels with the output of above. i tested it, so right now 1 tunnels up $tunnels will be 1. so if its more then 1 ex; 2 3 or 4 to restart ipsec OR if its 0 restart also. – Luis Jul 29 '13 at 14:57
  • The variable cannot be defined like that. You'd better do `tunnels=$(service ipsec status | awk 'NR==3{print $1}')` – fedorqui Jul 29 '13 at 14:59
  • hmm. that might be better yes. but im not sure b/c i do a if, else if else and i get errors. – Luis Jul 29 '13 at 15:14
  • So please update with proper explanation (don't use `b/c` but `because`, etc) and input / output. This way we will be able to trace your code in a better way. – fedorqui Jul 29 '13 at 15:16
  • So i have also the if and else statement that if the PING does not result is a proper reply to then restart. I am not sure where i can put the $tunnels > 1 statement in between the if and else statements. – Luis Jul 29 '13 at 15:48
  • Edited my post. hope that helps. i even tried -gt or (( > )) and nothing works. – Luis Jul 30 '13 at 17:09
  • Can you show the result of a simple `echo $tunnels` after the first line? So that I can see what is happenning. – fedorqui Jul 31 '13 at 10:57
  • If I hardcode `tunnels=1` I get inside the first `if` condition: `To many tunnels = 1`. Check if `$tunnels` is just "1" or it has some extra code that blocks the `if` condition. – fedorqui Jul 31 '13 at 16:03
  • i did, i made sure there are no spaces etc. still the first if condition should not happen. it should just pass to the next one. – Luis Jul 31 '13 at 16:04
  • I think I lost the reference of what is expected to be done. If `$tunnels=1` and `$a=0`, then it accomplishes the condition `(( $tunnels > $a));` – fedorqui Jul 31 '13 at 16:10
  • if the amount of tunnels > 1 then restart the server, $a=0 i was just tested because i thought 1 would not work. – Luis Jul 31 '13 at 17:34
  • i guess i would have to redo the whole thing =| – Luis Aug 01 '13 at 18:29
  • Just start with a basic script with basic conditions: `if [ $tunnels -ge 1 ]; then ... fi` and then keep giving more options. This way you will be able to find the errors easily. – fedorqui Aug 02 '13 at 09:54
  • @Luis update your question with your current code. Also, indicate the output of `service ipsec status` so I can know what can be in `tunnels` variable. – fedorqui Aug 08 '13 at 08:04