shell 特殊字符应用实例、shell 控制浮动路由

2021/6/19 7:27:21

本文主要是介绍shell 特殊字符应用实例、shell 控制浮动路由,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本篇记录 分享shell中特殊字符应用实例,该实例实现功能是检查多wan口的路由状态,实现浮动路由的功能。

#!/bin/sh
addr=114.114.114.114

echo " net interface monitor process startup .... "
sleep 15          # wait for network init

echo 'eth0.2' > /tmp/route_tag    #eth0.2 有线上联 wan 口
route_port=`cat /tmp/route_tag|head -n 1`
res=`route -n|grep $route_port |awk '{print $1}'|grep -w "0.0.0.0"`  # 检查wan口是否在路由表中
if [ ${res} ];then
    echo 'c vpn' > /var/run/xl2tpd/l2tp-control  #开启 l2tp vpn 链接
    echo " eth0.2 routing okay... "
else
    echo 'usb0' > /tmp/route_tag                  #usb0 4g 网卡
    route_port=`cat /tmp/route_tag|head -n 1`
    res=`route -n|grep $route_port |awk '{print $1}'|grep -w "0.0.0.0"`
	if [ ${res} ];then
		echo 'c vpn' > /var/run/xl2tpd/l2tp-control
        echo " usb0 routing okay... "
	else
	    echo 'usb1' > /tmp/route_tag              #usb1 4g 网卡
	    route_port=`cat /tmp/route_tag|head -n 1`
	    res=`route -n|grep $route_port |awk '{print $1}'|grep -w "0.0.0.0"`
	    if [ ${res} ];then
	    	echo 'c vpn' > /var/run/xl2tpd/l2tp-control
            echo " usb1 routing okay... "
	    else
	        echo " no routing information... "
	    fi
	fi
fi

while [ 1 ]
do
	route_port=`cat /tmp/route_tag|head -n 1`
	case "$route_port" in
		"eth0.2")
		res=`ping -c 2 -I $route_port $addr |grep -w "0% packet loss"`   #检查 wan 口联网能力
		if [ ! "$res" ];then
			echo 'd vpn' > /var/run/xl2tpd/l2tp-control
			sleep 1
			gwip=`ifconfig usb0|grep 'inet addr'|awk '{print $2}'|cut -c 6-`
			if [ $gwip ];then
				ip route del default dev $route_port
				ip route add default via ${gwip} dev usb0 metric 10  #实现路由浮动功能
				echo 'usb0' > /tmp/route_tag
			else
				echo 'usb1' > /tmp/route_tag
			fi
			echo 'c vpn' > /var/run/xl2tpd/l2tp-control
		fi
		;;

		"usb0")
		res=`ping -c 2 -I $route_port $addr |grep -w "0% packet loss"`
		if [ ! "$res" ];then
			echo 'd vpn' > /var/run/xl2tpd/l2tp-control
			sleep 1
			gwip=`ifconfig usb1|grep 'inet addr'|awk '{print $2}'|cut -c 6-`
			if [ $gwip ];then
				ip route del default dev usb0
				ip route add default via ${gwip} dev usb1 metric 15
				echo 'usb1' > /tmp/route_tag
			else
				echo 'usb1' > /tmp/route_tag
			fi
			echo 'c vpn' > /var/run/xl2tpd/l2tp-control
		fi
		;;
		
		"usb1")
		res=`ping -c 2 -I $route_port $addr |grep -w "0% packet loss"`
		if [ ! "$res" ];then
			echo 'd vpn' > /var/run/xl2tpd/l2tp-control
			sleep 1
			gwip=`ifconfig eth0.2|grep 'inet addr'|awk '{print $2}'|cut -c 6-`
			if [ $gwip ];then
				ip route del default dev usb1
				ip route add default via ${gwip} metric 0
				echo 'eth0.2' > /tmp/route_tag
				echo 'c vpn' > /var/run/xl2tpd/l2tp-control
			else
				echo 'usb1' > /tmp/route_tag
			fi
		else
		    echo 'c vpn' > /var/run/xl2tpd/l2tp-control	
		fi
		;;
		
		*)
		echo "ERROR: network no ping NIC ...."
		;;
	esac

	echo " current route port: '$route_port' "
	sleep 5
done

此 shell 脚本功能主要实现路由浮动测试功能,在产品上实验还需要进一步完善。



这篇关于shell 特殊字符应用实例、shell 控制浮动路由的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程