当前位置:网站首页>Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
2022-07-05 06:20:00 【yuyuyuliang00】
Use one with public network IP Address ECS , Write the following script on it :
#!/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!"
Execute the script written above as follows , Successful installation frp software package :
Enter :ss -tnlp You can see that the server has listened to the specified tcp port 7000 and 7500
On a computer that can connect to the Internet , Open a browser , Input :<IP Address >:7500,frp The background management client of successfully started :
Be careful : Access needs to be granted in the security group rules of the virtual machine 7000 and 7500 Of TCP Input direction of port .
边栏推荐
- MySQL advanced part 2: SQL optimization
- 【LeetCode】Day95-有效的数独&矩阵置零
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- 打印机脱机时一种容易被忽略的原因
- TypeScript 基础讲解
- 11-gorm-v2-02-create data
- QQ computer version cancels escape character input expression
- [rust notes] 14 set (Part 2)
- MIT-6874-Deep Learning in the Life Sciences Week 7
- 【LeetCode】Day94-重塑矩阵
猜你喜欢
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
Appium基础 — 使用Appium的第一个Demo
MySQL advanced part 1: View
Redis publish subscribe command line implementation
2021apmcm post game Summary - edge detection
博弈论 AcWing 894. 拆分-Nim游戏
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
随机推荐
Leetcode recursion
博弈论 AcWing 894. 拆分-Nim游戏
Appium foundation - use the first demo of appium
2021apmcm post game Summary - edge detection
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
Leetcode backtracking method
Sword finger offer II 058: schedule
LeetCode 1200. Minimum absolute difference
MySQL advanced part 2: the use of indexes
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Is it impossible for lamda to wake up?
C - XOR to all (binary topic)
4. 对象映射 - Mapping.Mapster
JS quickly converts JSON data into URL parameters
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
MySQL advanced part 1: triggers
Leetcode-9: palindromes
背包问题 AcWing 9. 分组背包问题
Currently clicked button and current mouse coordinates in QT judgment interface