一键安装OpenVPN——支持CentOS和Ubuntu

2014/12/1817:18:18 3
#!/bin/bash
OS=''
OpenVPNVersion='2.3.6'
InstallModel='0'
PacketDir='/root/openvpn'
InstallDir='/usr/local/openvpn'
PWDDir="`pwd`"
ServerIP="`hostname -i`"
GetChar()
{
    SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty SAVEDSTTY
}
function Uninstall()
{
    /etc/init.d/openvpn stop
    rm -Rf InstallDir;
    rm -f /etc/init.d/openvpn;
}
function ConfirmInstall()
{
    [ "`id -u`" -ne 0 ] && echo "Not root user,exit." && exit;
    echo "[Notice] Confirm Install/Uninstall OpenVPN? please select: (1~3)";
    select selected in 'Install OpenVPN 2.3.6' 'Uninstall OpenVPN 2.3.6' 'Exit'; do break; done;
    [ "selected" == 'Exit' ] && echo 'Exit Install.' && exit;
    if [ "selected" == 'Install OpenVPN 2.3.6' ]; then
InstallModel='1';
    elif [ "selected" == 'Uninstall OpenVPN 2.3.6' ]; then
Uninstall;
    else
ConfirmInstall;
    return;
    fi;
    echo "[OK] You Selected: {selected}";
    read -p "Please input your server IP [ServerIP]:"  ServerIP;
    read -p "Where do you want to install openvpn [/usr/local/openvpn]:" InstallDir;
    [ -z InstallDir ] && InstallDir='/usr/local/openvpn';
    echo "Your server IP is:ServerIP";
    echo "OpenVPN install path is:InstallDir";
    echo "======================================"
    echo "Press any key to contine..."
    echo "======================================"
}
function CheckOSVersion()
{
    egrep -i 'centos' /etc/issue && OS='centos';
    egrep -i 'ubuntu' /etc/issue && OS='ubuntu';
    [ "OS" = ''  ] && echo '[Error] Your system is not supported install OpenVPN' && exit;
    if [ "OS" = 'centos' ]
    then
yum -y install gcc gcc-c++ automake autoconf lzo openssl pam lzo-devel pam-devel openssl-devel iptables;
    elif [ "OS" = 'ubuntu' ]
    then
        apt-get -y install  gcc g++ automake  lzo openssl pam lzo-devel pam-devel openssl-devel iptables;
    fi
}
function Timezone()
{
    rm -rf /etc/localtime;
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime;
    echo '[ntp Installing] ******************************** >>';
    [ "OS" = 'centos' ] && yum install -y ntp || apt-get install -y ntpdate;
    ntpdate -u pool.ntp.org;
    StartDate=(date);
    StartDateSecond=(date +%s);
    echo "Start time: {StartDate}";
}
function CompileInstall()
{
    mkdir -p PacketDir;
    cd PacketDir;
    if [ ! -f PacketDir/openvpn-{OpenVPNVersion}.tar.gz ]
    then
        wget -c http://swupdate.openvpn.org/community/releases/openvpn-{OpenVPNVersion}.tar.gz;
    fi
    tar zxvf openvpn-{OpenVPNVersion}.tar.gz
    cd openvpn-{OpenVPNVersion}
    ./configure  --prefix=InstallDir;
    make;
    [ -d InstallDir ] && echo "Path [InstallDir] exsit,press any key to conitune..."
    char=`GetChar`
    make install;
    mkdir -p InstallDir/etc
}
function ConfigOpenVPN()
{
    echo "Get checkpsw.sh............";
    wget -c http://openvpn.se/files/other/checkpsw.sh -O InstallDir/etc/checkpsw.sh;
    sed -i "/PASSFILE/ c PASSFILE=\"InstallDir/etc/psw-file\"" InstallDir/etc/checkpsw.sh;
    chmod +x InstallDir/etc/checkpsw.sh;
    cat >>InstallDir/etc/server.conf <<EOF
port 1194
proto       udp
dev         tun
ca InstallDir/etc/easy-rsa/2.0/keys/ca.crt
cert InstallDir/etc/easy-rsa/2.0/keys/server.crt
key InstallDir/etc/easy-rsa/2.0/keys/server.key
dh InstallDir/etc/easy-rsa/2.0/keys/dh1024.pem
server 10.1.1.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
log /var/log/openvpn.log
keepalive 10 120 verb 3 client-to-client
comp-lzo
persist-key
persist-tun
auth-user-pass-verify InstallDir/etc/checkpsw.sh via-env
client-cert-not-required
username-as-common-name
script-security 3 system
ifconfig-pool-persist ipp.txt
client-to-client
EOF
    cp PWDDir/easy-rsa.tgz InstallDir/etc -Rf;
    cp PWDDir/easy-rsa.tgz PacketDir -Rf;
    cd InstallDir/etc;
    tar zxvf easy-rsa.tgz;
    cp PWDDir/openvpn  /etc/init.d/ -f;
    sed -i "/^openvpn_locations/ c openvpn_locations=\"InstallDir/sbin/openvpn\""  /etc/init.d/openvpn
    chmod a+x /etc/init.d/openvpn
    echo 1 > /proc/sys/net/ipv4/ip_forward ;
    echo "echo 1 > /proc/sys/net/ipv4/ip_forward" >> /etc/rc.local;
    sysctl -p
    iptables -I INPUT -p udp --dport 1194 -j ACCEPT;
    iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -j SNAT --to-source ServerIP
}
ConfirmInstall
char=`GetChar`
CheckOSVersion
Timezone
CompileInstall
ConfigOpenVPN
echo "test test" >>InstallDir/etc/psw-file;
if [ "OS" = 'centos' ]
then
    chkconfig --add openvpn
    chkconfig openvpn  on
fi
/etc/init.d/openvpn start
echo "`date "+%F %T"` OpenVPN install finished."
echo "Please use winscp or flashxp to download client authentication files in [$PacketDir/easy-rsa.tgz]"
echo "Your user is:test"
echo "Your password is:test"

  • 微信扫码赞助
  • weinxin
  • 支付宝赞助
  • weinxin

发表评论

您必须才能发表评论!

目前评论:3   其中:访客  0   博主  0

    • guozi

      高危动作,秒杀概率很高的

      • fujianan

        大神,带我飞
         

        • thordox

          啊!膜拜大神!