出发点:在linux电脑上面绑定多条adsl链路做家庭带宽及个人DC使用:
0、账号文件:adsl.user【账号 密码】
1 2 3 4 5 |
adsluser1 adslpasswd1 adsluser2 adslpasswd2 adsluser3 adslpasswd3 adsluser4 adslpasswd4 …… |
管理网卡策略路由:
vi /etc/iproute2/rt_tables
1 2 3 |
…… 252manage …… |
下面两条策略放到/etc/rc.local中,并手动执行一遍:
ip route add default via 10.255.255.1 table manage
ip rule add from 10.255.255.0/25 table manage
升级网卡驱动:[注意内核要和内核的头文件版本号完全一致]
https://downloadcenter.intel.com/zh-cn/download/14687/-PCIe-10-Linux-?product=41282
https://downloadmirror.intel.com/14687/eng/ixgbe-5.7.1.tar.gz
yum install -y gcc rpm-build kernel-devel
rpmbuild -tb ixgbe-5.7.1.tar.gz
rpm -Uvh /root/rpmbuild/RPMS/x86_64/ixgbe-5.7.1-1.x86_64.rpm
1、拨号建立脚本:0-adsl.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
#!/bin/bash #------------------------------------------------------------------------| # @Version : 1.1.0 | # @Writer : Tyumen <tyumen@zhoufengjie.cn> | # @Date : 2020-06-09 | #------------------------------------------------------------------------| adsluserlist=$1 dev=$2 function ShowUsage() { echo -e '\033[32;1mUsage: '$0 'adslfile netdev(like p5p1) \033[0m' echo exit 1 } function CheckPPPOE() { ( rpm -q rp-pppoe > /dev/null || yum install rp-pppoe -y ) && ( exit 0 ) || ( exit 1 ) } function adduser() { adsluser=$1 adslpwd=$2 num=$3 echo "$adsluser * $adslpwd " >> /etc/ppp/chap-secrets echo "$adsluser * $adslpwd " >> /etc/ppp/pap-secrets ip link add link $dev dev adsl$num type macvlan mode private cat > /etc/sysconfig/network-scripts/ifcfg-ppp$num <<EOF USERCTL=yes BOOTPROTO=dialup NAME=DSLppp$num DEVICE=ppp$num TYPE=xDSL ONBOOT=yes PIDFILE=/var/run/pppoe-adsl$num.pid FIREWALL=NONE PING=. PPPOE_TIMEOUT=80 LCP_FAILURE=3 LCP_INTERVAL=20 CLAMPMSS=1412 CONNECT_POLL=6 CONNECT_TIMEOUT=60 DEFROUTE=yes SYNCHRONOUS=no ETH=adsl$num PROVIDER=DSLppp$num USER=$adsluser PEERDNS=no DEMAND=no LINUX_PLUGIN=/usr/lib64/pppd/2.4.5/rp-pppoe.so EOF grep "ppp"$num /etc/iproute2/rt_tables > /dev/null || echo "1"$num" ppp"$num >> /etc/iproute2/rt_tables grep "ppp"$num /etc/rc.local > /dev/null || ( echo "ip route add 0/0 dev ppp"$num" table ppp"$num >> /etc/rc.local && ip route add 0/0 dev ppp$num table ppp$num) ifup ppp$num } function AddadslUser() { adslfile=$1 num=0 cat $adslfile|while read adsluser adslpwd do modprobe macvlan if [ "$adslpwd" != "" ];then ( ( grep $adsluser /etc/ppp/chap-secrets|grep $adslpwd > /dev/null ) && ( grep $adsluser /etc/ppp/pap-secrets|grep $adslpwd > /dev/null ) ) && (echo "user $adsluser has already exit." ) || ( adduser $adsluser $adslpwd $num ; let num++) let num++ fi done } if [ $# -ne 2 ] then ShowUsage else if [ -f $adsluserlist ];then CheckPPPOE && AddadslUser $adsluserlist fi fi |
sh /root/adsl/0-adsl.sh /root/adsl/adsl.user p5p1
2、拨号启动脚本:1-iprule.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#!/bin/bash beginnum=0 #endnum=17 endnum=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|wc -l` let endnum=endnum-1 function upnet() { begnum=$1 endnum=$2 for num in `seq $beginnum $endnum` do ifup ppp$num done } function addrule() { begnum=$1 endnum=$2 for num in `seq $beginnum $endnum` do rulenum=`ip rule | awk '$NF~"^ppp'$num'$"'|wc -l` while [ $rulenum -gt 0 ] ;do ip rule del lookup ppp$num;let rulenum--;done #route add default dev ppp$num ip route add 0/0 dev ppp$num table ppp$num ip rule add fwmark 0x1$num table ppp$num pref 100 ipaddr=`ifconfig ppp$num|awk '$1~"inet" && $2~"^[0-9]"{print $2 }'` if [ "$ipaddr" != "" ] then ip rule add from $ipaddr table ppp$num pref 200 fi done } function addiptable() { begnum=$1 endnum=$2 rpm -q iptables > /dev/null || yum install iptables -y for num in `seq $beginnum $endnum` do iptablenum=`iptables -t mangle -L -n|awk '$1~"MARK" && $NF~"^0x1'$num'$"'|wc -l` while [ $iptablenum -gt 0 ] ; do iptables -t mangle -D INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num ; let iptablenum--;done iptables -t mangle -A INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num done } upnet $beginnum $endnum addrule $beginnum $endnum addiptable $beginnum $endnum |
sh /root/adsl/1-iprule.sh
3、进程检查脚本:/root/adsl/2-adslcheck.sh【adsl出现down掉之后需要进行重启】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#!/bin/bash beginnum=0 endnum=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|wc -l` let endnum=endnum-1 PATH="/sbin:/usr/sbin:/bin:/usr/bin" export PATH function UPppp() { num=$1 ifup ppp$num rulenum=`ip rule | awk '$NF~"^ppp'$num'$"'|wc -l` while [ "$rulenum" -gt "0" ] ;do ip rule del lookup ppp$num;let rulenum--;done ip route add 0/0 dev ppp$num table ppp$num ip rule add fwmark 0x1$num table ppp$num pref 100 ipaddr=`ifconfig ppp$num|awk '$1~"inet" && $2~"^[0-9]"{print $2}'` if [ "$ipaddr" != "" ] then ip rule add from $ipaddr table ppp$num pref 200 fi iptablenum=`iptables -t mangle -L -n|awk '$1~"MARK" && $NF~"^0x1'$num'$"'|wc -l` while [ $iptablenum -gt 0 ] ; do iptables -t mangle -D INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num ; let iptablenum--;done iptables -t mangle -A INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num } function checkadsl() { begnum=$1 endnum=$2 for num in `seq $beginnum $endnum` do ipaddr=`ifconfig ppp$num|awk '$1~"inet" && $2~"^[0-9]"{print $2}'` if [ "$ipaddr" != "" ] then routenum=`ip route show table ppp$num|wc -l` if [ "$routenum" == "0" ] then rulenum=`ip rule | awk '$NF~"^ppp'$num'$"'|wc -l` while [ "$rulenum" -gt "0" ] ;do ip rule del lookup ppp$num;let rulenum--;done ip route add 0/0 dev ppp$num table ppp$num ip rule add fwmark 0x1$num table ppp$num pref 100 ipaddr=`ifconfig ppp$num|awk '$1~"inet" && $2~"^[0-9]"{print $2 }'` if [ "$ipaddr" != "" ] then ip rule add from $ipaddr table ppp$num pref 200 fi iptablenum=`iptables -t mangle -L -n|awk '$1~"MARK" && $NF~"^0x1'$num'$"'|wc -l` while [ "$iptablenum" -gt 0 ] ; do iptables -t mangle -D INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num ; let iptablenum--;done iptables -t mangle -A INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num fi else UPppp $num fi done } function checkroute() { for net in `ip a|awk -F"[ :]+" '$2~"^ppp"{print $2}'` do pingsucess=`ping 114.114.114.114 -I $net -c 10 -f|awk '$5~"received"{print $4}'` if [ "$pingsucess" == "0" ];then ip route add 0/0 dev $net table $net fi done } checkadsl $beginnum $endnum #checkroute |
chmod +x /root/adsl/2-adslcheck.sh
echo “*/10 * * * * root /root/adsl/2-adslcheck.sh > /dev/null 2>&1″ > /etc/cron.d/adslcheck
4、adsl定期重启
定期重启脚本:3-restartadsl.sh,防止在晚高峰的时候网络掉线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/bin/bash beginnum=0 endnum=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|wc -l` let endnum=endnum-1 function UPppp() { num=$1 ifup ppp$num sleep 5 rulenum=`ip rule | awk '$NF~"^ppp'$num'$"'|wc -l` while [ "$rulenum" -gt "0" ] ;do ip rule del lookup ppp$num;let rulenum--;done ip route add 0/0 dev ppp$num table ppp$num ip rule add fwmark 0x1$num table ppp$num pref 100 ipaddr=`ifconfig ppp$num|awk '$1~"inet" && $2~"^[0-9]"{print $2}'` if [ "$ipaddr" != "" ] then ip rule add from $ipaddr table ppp$num pref 200 fi iptablenum=`iptables -t mangle -L -n|awk '$1~"MARK" && $NF~"^0x1'$num'$"'|wc -l` while [ "$iptablenum" -gt "0" ] ; do iptables -t mangle -D INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num ; let iptablenum--;done iptables -t mangle -A INPUT -i ppp$num -m state --state NEW -j MARK --set-mark 0x1$num } function restartadsl() { begnum=$1 endnum=$2 for num in `seq $beginnum $endnum` do ifdown ppp$num done sleep 5 for num in `seq $beginnum $endnum` do UPppp $num done } restartadsl $beginnum $endnum |
echo “0 6 */3 * * root /root/adsl/3-restartadsl.sh > /dev/null 2>&1″ > /etc/cron.d/adslrestart
5、做随机启动配置:添加网卡脚本addnet.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/bin/bash adslfile=$1 dev=$2 num=0 function ShowUsage() { echo -e '\033[32;1mUsage: '$0 'adslfile netdev(like p5p1) \033[0m' echo exit 1 } function addnet() { adslfile=$1 cat $adslfile|while read adsluser adslpasswd vlanid other do ip link add link $dev dev adsl$num type macvlan mode private let num++ done } if [ $# -ne 2 ] then ShowUsage else if [ -f $adslfile ];then addnet $adslfile fi fi |
1 2 3 4 5 6 |
…… ip route add default via 10.255.255.1 table manage ip rule add from 10.255.255.0/25 table manage /root/adsl/addnet.sh /root/adsl/adsl.user p5p1 /root/adsl/1-iprule.sh …… |