#!/bin/bash #################################################### # # # This is a L2TP VPN installation for CentOS 7 # # Version: 1.1.0 20140803 # # Author: Travis Lee # # Website: http://www.stunnel.info # # # #################################################### #检测是否是root用户 if [[ $(id -u) != "0" ]]; then printf "\e[42m\e[31mError: You must be root to run this install script.\e[0m\n" exit 1 fi #检测是否是CentOS 7或者RHEL 7 if [[ $(grep "release 7." /etc/redhat-release 2>/dev/null | wc -l) -eq 0 ]]; then printf "\e[42m\e[31mError: Your OS is NOT CentOS 7 or RHEL 7.\e[0m\n" printf "\e[42m\e[31mThis install script is ONLY for CentOS 7 and RHEL 7.\e[0m\n" exit 1 fi clear printf " #################################################### # # # This is a L2TP VPN installation for CentOS 7 # # Version: 1.1.0 20140803 # # Author: Travis Lee # # Website: http://www.stunnel.info # # # #################################################### " #获取服务器IP serverip=$(ifconfig -a |grep -w "inet"| grep -v "127.0.0.1" |awk '{print $2;}') printf "\e[33m$serverip\e[0m is the server IP?" printf "If \e[33m$serverip\e[0m is \e[33mcorrect\e[0m, press enter directly." printf "If \e[33m$serverip\e[0m is \e[33mincorrect\e[0m, please input your server IP." printf "(Default server IP: \e[33m$serverip\e[0m):" read serveriptmp if [[ -n "$serveriptmp" ]]; then serverip=$serveriptmp fi #获取网卡接口名称 ethlist=$(ifconfig | grep ": flags" | cut -d ":" -f1) eth=$(printf "$ethlist\n" | head -n 1) if [[ $(printf "$ethlist\n" | wc -l) -gt 2 ]]; then echo ====================================== echo "Network Interface list:" printf "\e[33m$ethlist\e[0m\n" echo ====================================== echo "Which network interface you want to listen for ocserv?" printf "Default network interface is \e[33m$eth\e[0m, let it blank to use default network interface: " read ethtmp if [ -n "$ethtmp" ]; then eth=$ethtmp fi fi #设置VPN拨号后分配的IP段 iprange="10.0.1" echo "Please input IP-Range:" printf "(Default IP-Range: \e[33m$iprange\e[0m): " read iprangetmp if [[ -n "$iprangetmp" ]]; then iprange=$iprangetmp fi #设置预共享密钥 mypsk="stunnel.info" echo "Please input PSK:" printf "(Default PSK: \e[33mstunnel.info\e[0m): " read mypsktmp if [[ -n "$mypsktmp" ]]; then mypsk=$mypsktmp fi #设置VPN用户名 username="stunnel" echo "Please input VPN username:" printf "(Default VPN username: \e[33mstunnel\e[0m): " read usernametmp if [[ -n "$usernametmp" ]]; then username=$usernametmp fi #随机密码 randstr() { index=0 str="" for i in {a..z}; do arr[index]=$i; index=$(expr ${index} + 1); done for i in {A..Z}; do arr[index]=$i; index=$(expr ${index} + 1); done for i in {0..9}; do arr[index]=$i; index=$(expr ${index} + 1); done for i in {1..10}; do str="$str${arr[$RANDOM%$index]}"; done echo $str } #设置VPN用户密码 password=$(randstr) printf "Please input \e[33m$username\e[0m's password:\n" printf "Default password is \e[33m$password\e[0m, let it blank to use default password: " read passwordtmp if [[ -n "$passwordtmp" ]]; then password=$passwordtmp fi clear #打印配置参数 clear echo "Server IP:" echo "$serverip" echo echo "Server Local IP:" echo "$iprange.1" echo echo "Client Remote IP Range:" echo "$iprange.10-$iprange.254" echo echo "PSK:" echo "$mypsk" echo echo "Press any key to start..." get_char() { SAVEDSTTY=`stty -g` stty -echo stty cbreak dd if=/dev/tty bs=1 count=1 2> /dev/null stty -raw stty echo stty $SAVEDSTTY } char=$(get_char) clear mknod /dev/random c 1 9 #安装依赖的组件 yum -y update yum install -y openswan ppp xl2tpd wget rm -f /etc/ipsec.conf #创建ipsec.conf配置文件 cat >>/etc/ipsec.conf<>/etc/ipsec.secrets<>/etc/xl2tpd/xl2tpd.conf<>/etc/ppp/options.xl2tpd<>/etc/ppp/chap-secrets<>/etc/sysctl.conf</usr/lib/firewalld/services/l2tpd.xml< l2tpd L2TP IPSec EOF firewall-cmd --permanent --add-service=l2tpd firewall-cmd --permanent --add-service=ipsec firewall-cmd --permanent --add-masquerade firewall-cmd --reload #iptables --table nat --append POSTROUTING --jump MASQUERADE #iptables -t nat -A POSTROUTING -s $iprange.0/24 -o $eth -j MASQUERADE #iptables -t nat -A POSTROUTING -s $iprange.0/24 -j SNAT --to-source $serverip #service iptables save #允许开机启动 systemctl enable ipsec xl2tpd systemctl restart ipsec xl2tpd clear #测试ipsec ipsec verify printf " #################################################### # # # This is a L2TP VPN installation for CentOS 7 # # Version: 1.1.0 20140803 # # Author: Travis Lee # # Website: http://www.stunnel.info # # # #################################################### if there are no [FAILED] above, then you can connect to your L2TP VPN Server with the default user/password below: ServerIP: $serverip username: $username password: $password PSK: $mypsk "