shell脚本----------路由和数据库的同步

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值