检查隧道的脚本 - Awk

时间:2013-07-29 14:54:08

标签: bash awk grep ipsec

我试图弄明白,b / c有时ping会起作用,但那是b / c它只是reg。 ICMP但是当真正的交通试图通过隧道它不会工作b / c有2-3个隧道。我想做IF $隧道> 1 AND $ tunnels == 0执行以下重启IPSec。

不那么容易

#!/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

我试过这个,但不管怎么样,它一直使用第一个。即使我说隧道的数量大于1或0,它仍然使用该语句。什么给了!?

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

1 个答案:

答案 0 :(得分:1)

尝试[ $? -eq 0 ],因为这是一个数字比较。

此外,这一行:

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

没有多大意义。如果您想要打印第一个字段,请执行以下操作:

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

我不知道的是使用$tunnels就行了。

相关问题