#!/bin/bash
netstat -rn | egrep UG | egrep -v 0.0.0.0
route_num=$(netstat -rn | egrep UG | egrep -v 0.0.0.0|wc -l)
db=/ln/ln.db
#判断行数<=0脚本不会往下执行
if [ $route_num -le 0 ];
then
exit 0
fi
[ -z $1 ] && exit 0;
cd /sonicom/apache2/htdocs/Admin/shell/
#查询IP,网关,掩码
netstat -rn | egrep UG | egrep -v 0.0.0.0 |egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" |xargs -n 3 >route.txt
#判断路由是否有记录
coulmn=$(cat route.txt|wc -l)
if [ $coulmn -le 0 ];
then
exit 0
fi
#创建SQL文件
echo -e "delete from Route;" |/usr/local/bin/sqlite3 $db
count=0
for line in `cat route.txt`
do
[ $count -eq 0 ] && ip=$line;
[ $count -eq 1 ] && netmask=$line;
[ $count -eq 2 ] && gw=$line;
let count++
if [ $count = 3 ]
then
echo -e "insert into Route(route_ip,route_netMask,route_gw) values(\"$ip\",\"$netmask\",\"$gw\");"|/usr/local/bin/sqlite3 $db
count=0;
eth="";
ip="";
netmask="";
fi
done
#写入配置文件
touch /etc/network/if-up.d/static-route
rm /etc/network/if-up.d/static-route
touch /etc/network/if-up.d/static-route
count=0
if [ $route_num -gt 0 ];
then
echo -e "#/bin/bash">/etc/network/if-up.d/static-route
for line in `cat route.txt`
do
[ $count -eq 0 ] && ip=$line;
[ $count -eq 1 ] && netmask=$line;
[ $count -eq 2 ] && gw=$line;
let count++
if [ $count = 3 ]
then
echo -e "route add -net $ip netmask $netmask gw $gw">>/etc/network/if-up.d/static-route
count=0;
eth="";
ip="";
netmask="";
fi
done
echo -e "exit 0">>/etc/network/if-up.d/static-route
else
echo -e "#/bin/bash\nexit 0">>/etc/network/if-up.d/static-route
fi
if [ $? -eq 0 ];
then
rm route.txt
fi