雙網卡雙IP_PPTP_***

#!/bin/bash
#本腳本爲mpls_pptp ***配置
#注意事項:
#1.服務器配置香港IP和國內IP地址,香港爲默認網關,國內IP不要配置網關,rc.local中不需要添加任何策略,如有其他策略請執行完腳本後添加。
#2.本腳本會清空iptables,如果原iptables中有策略,請從/etc/sysconfig/iptables.bak中找回
yum -y install  wget vim
input_address()
{
read -p "please input HK IP adderss:"HKADDR
read -p "please input CN IP adderss:"CNADDR
read -p "please input CN Gateway IP adderss:"cngateway
read -p "please input CN Gateway IP adderss:"hkgateway
#寫入rc.local
ifconfig -a |grep 'eth\|em' |awk '{ print $1}' > 1.txt
ip addr | grep inet |grep -v inet6|grep -v "127.0.0.1" | awk '{ print $2 }' |cut -d/ -f1 >2.txt
 paste 1.txt 2.txt >3.txt
if [ `cat /etc/rc.local| grep "ip route replace default " | wc -l` -eq 0 ];then
    echo "ip route replace default via $hkgateway dev `cat 3.txt |grep $HKADDR | awk '{ print $1}' ` " > /etc/rc.local
    echo "ip route flush table ctc" >> /etc/rc.local
    echo "ip route add default via $cngateway dev $CNADDR src `cat 3.txt|grep $CNADDR |awk '{ print $1 }'` table ctc" >>/etc/rc.local
    echo "ip rule add from $CNADDR table ctc" >>/etc/rc.local
    echo "ip route flush table HK" >>/etc/rc.local
    echo "ip route add default via $hkgateway dev `cat 3.txt|grep $CNADDR |awk '{ print $1 }'`  src $HKADDR  table HK" >> /etc/rc.local
    echo "ip rule add from $HKADDR table HK" >>/etc/rc.local
else
    echo "Routing table has been added to the rc.local"
fi
echo "252     ctc ">> /etc/iproute2/rt_tables  
echo "251     HK"  >> /etc/iproute2/rt_tables
/etc/rc.local
}
#判斷網卡IP地址歸屬地,及電信網關地址
auto_address()
{
ADDR1=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk 'NR==1 { print $1}' `
ADDR2=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk 'NR==2 { print $1}' `
AS1=`curl -s ipinfo.io/$ADDR1/org |awk '{ print $1 }'`
AS2=`curl -s ipinfo.io/$ADDR2/org |awk '{ print $1 }'`
if [ "$AS1" = "AS58879" ];then
        HKADDR=$ADDR1
        CNADDR=$ADDR2
elif [ "$AS2" = "AS58879" ]; then
        HKADDR=$ADDR2
        CNADDR=$ADDR1
else
        echo -e "\033[31m Not Found HK IP address \033[0m" 
        exit 1
fi
cngateway=${CNADDR%.*}.1
hkgateway=${HKADDR%.*}.1
echo -e "\033[32m Please check and confirm CN Gateway IP address is $cngateway ? \033[0m"
read -p "(please input: y or n):" a
case "$a" in
y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
echo -e  "\033[32m CN Gateway IP address is $cngateway \033[0m"
;;
n|N|No|NO|no|nO)
        input_address 
;;
*)
echo "input error"
    input_address
esac
#寫入rc.local 
ifconfig -a |grep 'eth\|em' |awk '{ print $1}' > 1.txt
ip addr | grep inet |grep -v inet6|grep -v "127.0.0.1" | awk '{ print $2 }' |cut -d/ -f1 >2.txt
 paste 1.txt 2.txt >3.txt
if [ `cat /etc/rc.local| grep "ip route replace default " | wc -l` -eq 0 ];then
    echo "ip route replace default via $hkgateway dev `cat 3.txt |grep $HKADDR | awk '{ print $1}' ` " > /etc/rc.local
    echo "ip route flush table ctc" >> /etc/rc.local
    echo "ip route add default via $cngateway dev `cat 3.txt|grep $CNADDR |awk '{ print $1 }'` src  $CNADDR table ctc" >>/etc/rc.local
    echo "ip rule add from $CNADDR table ctc" >>/etc/rc.local
    echo "ip route flush table HK" >>/etc/rc.local
    echo "ip route add default via $hkgateway dev `cat 3.txt|grep $HKADDR |awk '{ print $1 }'`  src $HKADDR  table HK" >> /etc/rc.local
    echo "ip rule add from $HKADDR table HK" >>/etc/rc.local
else
    echo "Routing table has been added to the rc.local"
fi
echo "252     ctc ">> /etc/iproute2/rt_tables  
echo "251     HK"  >> /etc/iproute2/rt_tables
/etc/rc.local
}
curl -s ipinfo.io/114.114.114.114/org >/dev/null 2>&1
if [[ $? == 2 ]]; then
    echo "nameserver 114.114.114.114
nameserver 8.8.8.8" > /etc/resolv.conf
fi
curl -s ipinfo.io/114.114.114.114/org >/dev/null 2>&1
if [[ $? == 2 ]]; then
   input_address
else
   auto_address
fi
#生成路由表
wget http://download.ks.51idc.com:8000/%B3%A3%D3%C3%C8%ED%BC%FE/shell%BD%C5%B1%BE/route.txt
echo "#!/bin/bash" > /etc/route.sh
for i in `cat route.txt`
do
    echo "route add -net $i gw $cngateway" >> /etc/route.sh
done
echo "sh /etc/route.sh" >> /etc/rc.local
sh /etc/route.sh
#配置pptp
cp -rp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
yum remove -y pptpd ppp
iptables -F
iptables -t nat -F
iptables --flush POSTROUTING --table nat
iptables --flush FORWARD
rm -rf /etc/pptpd.conf
rm -rf /etc/ppp
yum install -y make libpcap iptables gcc-c++ logrotate tar cpio perl pam tcp_wrappers dkms kernel_ppp_mppe ppp
arch=`uname -m`
wget http://download.ks.51idc.com:8000/%b3%a3%d3%c3%c8%ed%bc%fe/%cd%f8%d5%be%b3%cc%d0%f2/***/pptpd-1.4.0-1.el6.$arch.rpm
rpm -Uvh pptpd-1.4.0-1.el6.$arch.rpm
mknod /dev/ppp c 108 0 
echo 1 > /proc/sys/net/ipv4/ip_forward 
echo "mknod /dev/ppp c 108 0" >> /etc/rc.local
echo "echo 1 > /proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
echo "localip 172.16.36.1" >> /etc/pptpd.conf
echo "remoteip 172.16.36.2-254" >> /etc/pptpd.conf
echo "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd
echo "ms-dns 114.114.114.114" >> /etc/ppp/options.pptpd
pass=`openssl rand 6 -base64`
if [ "$1" != "" ]
then pass=$1
fi
echo "*** pptpd ${pass} *" >> /etc/ppp/chap-secrets
iptables -A FORWARD -p tcp --syn -s 172.16.36.0/24 -j TCPMSS --set-mss 1356
chkconfig iptables on
chkconfig pptpd on
service iptables start
service pptpd start
echo "*** service is installed, your *** username is ***, *** password is ${pass}" > ***_password.txt
#添加snat表
for i in `cat route.txt`
do
    iptables -t nat -A POSTROUTING -s 172.16.36.0/24 -d $i  -j SNAT --to-source $CNADDR
done
iptables -t nat -A POSTROUTING -s 172.16.36.0/24   -j SNAT --to-source $HKADDR
service iptables save
rm -rf 1.txt 2.txt 3.txt route.txt


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章