bash练习17 编写脚本安装frp反向代理软件的服务器端

1、用一台有公网IP地址的云服务器,在其上编写以下脚本:

#!/usr/bin/bash
# install frp server in a cloud server

[ $UID -ne 0 ] && (echo "You are not the root, can not excute this script!" && exit 1)

Usage(){
    echo "Usage: $0 -f <frp_package_lcation> -p <install_location> -P <listen_port> -t <token> -d <dashboard_port>  -u <dashboard_username> -s <dashboard_pwd>"
}

PACKAGE=""
LOCATION=""
PORT=""
DUSER=""
PASSWD=""
TOKEN=""
DPORT=""
DPASSWD=""
while getopts f:p:P:t:d:u:s: arg
do
    case $arg in
    f)
        PACKAGE=$OPTARG
        ;;
    p)
        LOCATION=$OPTARG
        ;;
    P)
        PORT=$OPTARG
        ;;
    t)
        TOKEN=$OPTARG
        ;;
    d)
        DPORT=$OPTARG
        ;;
    u)
        DUSER=$OPTARG
        ;;
    s)
        DPASSWD=$OPTARG
        ;;
    ?)
        echo "Invalid Options:~$OPTARG"
        Usage
        exit 1
        ;;
    esac
done

# check the package  whether exists and starts with frp
if [ -f ${PACKAGE} ];then
    package=$(basename "${PACKAGE}")
    if [[ ! "${package}"=~^frp ]] || [[  ! "{package}"=~\.tar\.gz$ ]];then
        echo "The package is not the frp package, please use the right package!"
        exit 1
    fi
else
    echo "The package to install was wrong, please check again! "
    Usage
    exit 1
fi

# check the path to install the package is right
if [ -n "${LOCATION}" ];then
    LOCATION=${LOCATION%/}
    if [ ! -e ${LOCATION} ];then
        mkdir -p ${LOCATION}
        [ $? -ne 0 ] && ( echo "can not make the path to install the package"  && exit 2)
    elif [ ! -d ${LOCATION} -o ! -x ${LOCATION} ];then
        echo "you did not give the right location or it can not be accessed!"
        exit 2
    fi
else
    echo "You must give the location to install with -p option argument"
    Usage
    exit 2
fi

if [ -n "${PORT}" ];then
    if [[ ! "${PORT}" =~ ^[0-9]+$  ]];then
        echo "The Listen port must be a number smaller than 65536"
        exit 3
    fi
    if [ ${PORT} -gt 65536 ];then
        echo "The Listen port must be a number smaller than 65536"
        exit 3
    fi
    port=$( ss -ltnp | sed -n '2,$p' | awk '{print $4}' | cut -d':' -f2 | grep ${PORT})
    if [ -n "${port}" ];then
        echo "${PORT} is already be used by another program, Please use anther port"
        exit 3
    fi

else
    PORT=7000
fi

if [ -n "$TOKEN" ];then
    if [[ ! "$TOKEN" =~ ^[a-zA-Z0-9_]{6,10}$ ]];then
        echo "token can only contain alphabeta , digital or underscore from 6 to 10"
        exit 4
    fi
else
    echo "You must give the token consisting of only alphabeta, digital or underscore from 6 to 10 using -t option"
    Usage
    exit 4
fi

if [ -n "$DPORT" ];then
    if [[ ! "${DPORT}" =~ ^[0-9]+$  ]];then
        echo "The Listen port must be a number smaller than 65536"
        exit 3
    fi
    if [ ${DPORT} -gt 65536 ];then
        echo "The Listen port must be a number smaller than 65536"
        exit 3
    fi
    port=$( ss -ltnp | sed -n '2,$p' | awk '{print $4}' | cut -d':' -f2 | grep ${DPORT})
    if [ -n "${port}" -o "${port}" == "${PORT}" ];then
        echo "${DPORT} is already be used by another program, Please use anther port"
        exit 3
    fi

else
    DPORT=7500
fi

if [ -n "DUSER" ];then
    if [[ ! "${DUSER}" =~ ^[a-zA-Z]{1}[a-zA-Z0-9_]{2,7}$ ]];then
        echo "dash board username should start with alphabeta and only contain alphabeta, digital or underscore from 3 to 8 characters"
        exit 5
    fi
else
        echo "You shold give dash board username  starting with alphabeta and only containing alphabeta, digital or underscore from 3 to 8 characters"
        Usage
        exit 5
fi

if [ -n "$DPASSWD" ];then
    if [[ ! "$DPASSWD" =~ ^[a-zA-Z0-9_]{6,10}$ ]];then
        echo "dash board password can only contain alphabeta , digital or underscore from 6 to 10"
        exit 6
    fi
else
    echo "You must give the dash board password consisting of only alphabeta, digital or underscore from 6 to 10 using -s option"
    Usage
    exit 6
fi

echo "Package Location:$PACKAGE"
echo "Install Location:$LOCATION"
echo "Listen Port:$PORT"
echo "Token:$TOKEN"
echo "Dash board Port:$DPORT"
echo "Dash board User:$DUSER"
echo "Dash board Password:$DPASSWD"

read -p "Are you Sure to use these above arguments[Yes/No]" CHOSE

if [[  ! "$CHOSE" =~ ^[Yy]es$ ]];then
    echo "You did not choose to install the package, exit"
    exit 1
fi

echo "Starting to install frp package ..."
echo "uncompress the ${PACKAGE} to ${LOCATION} ..."
tar -xvzf ${PACKAGE} -C ${LOCATION} &>/dev/null
[ $? -ne 0 ] && (echo "can not uncompress the package to destination" && exit 1)
echo "uncompressed  completed"

CURDIR=$(pwd)
package=$(basename ${PACKAGE})
INSTDIR=${LOCATION}/${package%.tar.gz}

echo "Goto to install path: ${INSTDIR}"
cd ${INSTDIR} || (echo "can not access the path: ${INSTDIR}" && exit 1)
echo "backup frps.ini to frp.ini.bak"
mv frps.ini frps.ini.bak
[ $? -ne 0 ] && (echo "backup failed" && exit 1)

echo "Edit the server configuration: "
cat >frps.ini<<EOF
[common]
bind_port = ${PORT}
# this token will be used by clients
token =${TOKEN}

dashboard_port = ${DPORT}
# frp background manager will use this username and password
dashboard_user = ${DUSER}
dashboard_pwd = ${DPASSWD}
enable_prometheus = true

# frp log configuration
log_file = /var/log/frps.log
log_level = info
log_max_days = 3
EOF
echo "Edit finished!"

CONFIGDIR=/etc/frp

if [ ! -d /etc/frp ];then
    mkdir -p "${CONFIGDIR}"
    [ $? -ne 0 ] && (echo "can not make the configuration path:${CONFIGDIR}" && exit)
fi
cp frps.ini /etc/frp
cp frps /usr/bin
cp systemd/frps.service /usr/lib/systemd/system/
systemctl enable frps
systemctl start frps

firewall-cmd --permanent --add-port=${PORT}/tcp
firewall-cmd --permanent --add-port=${DPORT}/tcp
firewall-cmd --reload

echo "back to the path: ${CURDIR}"
cd ${CURDIR}
echo "Install and start the frp package successfully!"

2、按如下执行以上编写好的脚本,成功安装frp软件包:

3、在云主机上输入:ss -tnlp 可以看到服务器已经监听指定的tcp端口7000和7500

4、在一台能够连接互联网的计算机上,打开一个浏览器,输入:<IP地址>:7500,frp的后台管理客户端成功启动:

注意:在云主机的安全组规则中需要放行访问7000和7500的TCP端口的入方向。

本处高能:因Frp作者更换新的模块,导致Frp v0.18.0与之前的版本不兼容,如果升级请服务器端同步升级。升级命令: wget --no-check-certificate https://raw.githubusercontent.com/clangcn/onekey-install-shell/master/frps/install-frps.sh -O ./install-frps.sh bash install-frps.sh update 首先感谢@sadoneli S大的帮助完成了frp插件web页面的制作。 WARNING:请仔细阅读完本教程1楼和2楼后再动手安装frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持根据域名进行路由转发。 *因为frp是go语言写的,所以在路由器上使用的时候,请使用虚拟内存,请使用虚拟内存,请使用虚拟内存。 脚本是业余爱好,英文属于文盲,写的不好,欢迎您批评指正。 安装平台:CentOS、Debian、Ubuntu。 已测试过的平台: CentOS 6 32/64bit CentOS 7 32/64bit Debian 6 32/64bit Debian 7 32/64bit Debian 8 32/64bit Ubuntu 14 32/64bit 一、安装命令这个命令是在你自己的服务器上运行的!是在你自己的服务器上运行的!是在你自己的服务器上运行的!不是在路由器里运行的!不是在路由器里运行的!不是在路由器里运行的! wget --no-check-certificate https://raw.githubusercontent.com/clangcn/onekey-install-shell/master/frps/install-frps.sh -O ./install-frps.sh chmod 700 ./install-frps.sh ./install-frps.sh install 二、安装步骤 Loading network version for frps, please wait... frps Latest release file frp_0.8.1_linux_amd64.tar.gz #此步骤会自动获取frp最新版本,自动操作,无需理会 Loading You Server IP, please wait... You Server IP:12.12.12.12 #自动获取你服务器的IP地址 Please input your server setting: Please input frps bind_port [1-65535](Default Server Port: 5443): #输入frp提供服务的端口,用于服务器端和客户端通信 Please input frps dashboard_port [1-65535](Default dashboard_port: 6443): #输入frp的控制台服务端口,用于查看frp工作状态 Please input frps vhost_http_port [1-65535](Default vhost_http_port: 80): #输入frp进行http穿透的http服务端口 Please input frps vhost_https_port [1-65535](Default vhost_https_port: 443): #输入frp进行https穿透的https服务端口 Please input privilege_token (Default: WEWLRgwRjIJVPx2kuqzkGnvuftPLQniq): #输入frp服务器和客户端通信的密码,默认是随机生成的 Please input frps max_pool_count [1-200](Default max_pool_count: 50): #设置每个代理可以创建的连接池上限,默认50 ##### Please select log_level ##### 1: info 2: warn 3: error 4: debug ##################################################### Enter your choice (1, 2, 3, 4 or exit. default [1]): #设置日志等级,4个选项,默认是info Please input frps log_max_days [1-30] (Default log_max_days: 3 day): #设置日志保留天数,范围是1到30天,默认保留3天。 ##### Please select log_file ##### 1: enable 2: disable ##################################################### Enter your choice (1, 2 or exit. default [1]): #设置是否开启日志记录,默认开启,开启后日志等级及保留天数生效,否则等级和保留天数无效 设置完成后检查你的输入,如果没有问题按任意键继续安装 ============== Check your input ============== You Server IP : 12.12.12.12 Bind port : 5443 Dashboard port : 6443 vhost http port : 80 vhost https port: 443 Privilege token : WEWLRgwRjIJVPx2kuqzkGnvuftPLQniq Max Pool count : 50 Log level : info Log max days : 3 Log file : enable ============================================== 安装结束后显示: Congratulations, frps install completed! ============================================== You Server IP : 12.12.12.12 Bind port : 5443 Dashboard port : 6443 vhost http port : 80 vhost https port: 443 Privilege token : WEWLRgwRjIJVPx2kuqzkGnvuftPLQniq Max Pool count : 50 Log level : info Log max days : 3 Log file : enable # 将上面信息添加到你的路由器frp穿透插件中吧 ============================================== frps Dashboard: http://12.12.12.12:6443/ # 这个是frp控制台访问地址 ============================================== 三、更新命令 ./install-frps.sh update 四、卸载命令 ./install-frps.sh uninstall 五、服务器端管理命令 /etc/init.d/frps start /etc/init.d/frps stop /etc/init.d/frps restart /etc/init.d/frps status /etc/init.d/frps config /etc/init.d/frps version 七、更多帮助请移步官方帮助文件 https://github.com/fatedier/frp/blob/master/README_zh.md
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值