内网穿透神器FRP一键部署脚本(Centos7)

内网穿透神器FRP一键部署脚本(Centos7)

提示:本文分frp服务端、frp客户端,均在腾讯云里面测试可用


准备工作

  1. 一台公网服务器
  2. 一台内网服务器
  3. 一个公网IP
  4. 关闭服务器防火墙
  5. 关闭selinux
  6. root身份执行脚本
  7. 服务器安装wget

提示:
以Centos7为例:
临时关闭防火墙:systemctl stop firewalld
临时selinux关闭方法:setenforce 0
安装wget命令:yum install wget -y


提示:以下是本篇文章正文内容,下面案例可供参考

一、一键部署frp服务端(Centos7)

提示:脚本的每个参数输入时间只有5秒,超过5秒将默认安装v0.35.1版本的frp

脚本使用步骤:
 1. touch frps.sh
 2. chmod +x frps.sh
 3. vi frps.sh
 4. 按 I 或者 insert 键 开始编辑
 5. 复制下面的代码
 6. 粘贴代码
 7. 保存  :wq
 8. 运行脚本:  ./frps.sh
#!/bin/bash
############################################
#Dev by wzx                                #
#Dev Time 2021/02/17                       #
#Ver 1.0                                   #
#Blog:https://blog.csdn.net/sinat_38457621 #
############################################

#声明需要下载的FRP版本号,可在https://github.com/fatedier/frp/releases/ 里面查看

#本次下载的默认版本为0.35.1
read -t 5 -p "Input you want Download FRP_Version(Default:0.35.1):" ver
if [ -z "${ver}" ];then
	ver=0.35.1
fi

#本次下载的默认版本为linux
echo "Your FRP_Version is $ver"
read -t 5 -p "Input your OS(Default:linux):" OS
if [ -z "${OS}" ];then
	OS=linux
fi
echo "Your OS is $OS"

#本次下载的版本默认64位架构
echo "Tips to CoreType : You can find in https://github.com/fatedier/frp/releases/"
read -t 5 -p "Input your CoreType(Default:amd64):" coreType
if [ -z "${coreType}" ];then
  coreType=amd64
fi
echo "Your coreType is $coreType"
echo "Your Version: $ver_$OS_$coreType"
sleep 1

#检测根目录有没有安装包,如果有跳过下载,没有就继续下载
if [ -f /root/frp_"$ver"_"$OS"_"$coreType".tar.gz ]
then
    echo "File exists"
else
	echo "Starting Download FRP , please wait...."
    download="https://github.com/fatedier/frp/releases/download/v$ver/frp_""$ver""_""$OS""_amd64.tar.gz"
    wget $download
fi

#检测下载有没有成功执行,返回0代表命令正常完成
if [ $? -eq 0 ];then
    echo "Download success"
else
   echo "Download Faild.Check FRP_Version & OS"
fi
sleep 2

#创建/etc/frp目录
mkdir -p /etc/frp

#解压缩安装包到/etc/目录里面
tar -zxvf /root/frp_"$ver"_"$OS"_"$coreType".tar.gz -C /etc/

#将解压的文件夹重命名为frp
mv /etc/frp_"$ver"_"$OS"_"$coreType"/ /etc/frp/

#进入目录
cd /etc/frp/frp_"$ver"_"$OS"_"$coreType"

#将目录内文件移动至/etc/frp里面
mv ./* /etc/frp

#清除frp多余的目录
rm -rf /etc/frp/frp_"$ver"_"$OS"_"$coreType"

#声明frp服务端配置文件的位置
ServerConf="/etc/frp/frps.ini"

#声明frp服务端端口,默认值:7000
read -t 5 -p "FRP_Server_PORT is(Dafault:7000):" FRP_Server_PORT
if [ -z "${FRP_Server_PORT}" ];then
	FRP_Server_PORT=7000
fi
echo "Your FRP_Server_PORT is $FRP_Server_PORT"

#声明frp服务端与客户端的认证密码
read -t 5 -p -s "FRP_token is(Dafault:123321):" token
if [ -z "${token}" ];then
  token=123321
fi
echo "Your FRP_token is $token"
echo "Please remember your Token use to client Install"
sleep 3

#声明frp的仪表盘的端口、用户、用户密码,用于查看frp流量情况
dashboard_port = 6000
dashboard_user = root
dashboard_pwd = 123456

#FRP服务端配置文件配置
cat>"$ServerConf"<<EOF

[common]

bind_port = $FRP_Server_PORT
token = $token
dashboard_port = $dashboard_port
dashboard_user = $dashboard_user
dashboard_pwd = $dashboard_pwd

EOF
sleep 1

#将frp服务端配置成Linux服务
echo "Start input frpsService config.."

frpsService="/etc/frp/systemd/frps.service"

cat>"$frpsService"<<EOF

[Unit]
Description=FRP to bypass NAT Network
Wants = network-online.target
After=network.target 
[Service] 
Type=simple 
ExecStart=/etc/frp/frps -c /etc/frp/frps.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frps
StandardOutput = syslog
StandardError = inherit
PrivateTmp=true 
[Install] 
WantedBy=multi-user.target
EOF

echo "copy to /lib/systemd/system/...."
cp "$frpsService" /lib/systemd/system/
systemctl daemon-reload
sleep 1

#将frp服务端设置为开机自启动
systemctl enable frps.service

#启动frp
echo "frp is starting..."
systemctl start frps.service
if [ $? -eq 0 ];then
    echo "Start success"
else
   echo "FRP start faild"
fi
clear

#frp启动状态
systemctl status frps.service |grep active |cut -d : -f 2

#frp服务端的配置信息展示
echo "Your FRP_Server_PORT is $FRP_Server_PORT"
echo "Your FRP_token is $token"
echo "Your FRP_dashboard is : $dashboard_port"
echo "Your FRP_dashboard_User is : $dashboard_user"
echo "Your FRP_dashboard_Pwd is : $dashboard_pwd"

二、一键部署frp客户端(Centos7)

使用提示:参数输入时间只有5秒,请正确输入参数,否则服务无法正常运行
代码如下:

#!/bin/bash
############################################
#Dev by wzx                                #
#Dev Time 2021/02/17                       #
#Ver 1.0                                   #
#Blog:https://blog.csdn.net/sinat_38457621 #
############################################

#声明需要下载的FRP版本号,可在https://github.com/fatedier/frp/releases/ 里面查看
#本次下载的默认版本为0.35.1
read -t 5 -p "Input you want Download FRP_Version(Default:0.35.1):" ver
if [ -z "${ver}" ];then
	ver=0.35.1
fi
echo "Your FRP_Version is $ver"

#本次下载的默认版本为linux
read -t 5 -p "Input your OS(Default:linux):" OS
if [ -z "${OS}" ];then
	OS=linux
fi
echo "Your OS is $OS"

#本次下载的版本默认64位架构
echo "Tips to CoreType : You can find in https://github.com/fatedier/frp/releases/"
read -t 5 -p "Input your CoreType(Default:amd64):" coreType
if [ -z "${coreType}" ];then
  coreType=amd64
fi
echo "Your coreType is $coreType"
echo "Your Version: $ver_$OS_$coreType"
sleep 1

#检测根目录有没有安装包,如果有跳过下载,没有就继续下载
if [ -f /root/frp_"$ver"_"$OS"_"$coreType".tar.gz ]
then
    echo "File exists"
else
	echo "Starting Download FRP , please wait...."
    download="https://github.com/fatedier/frp/releases/download/v$ver/frp_""$ver""_""$OS""_amd64.tar.gz"
    wget $download
fi

#检测下载有没有成功执行,返回0代表命令正常完成
if [ $? -eq 0 ];then
    echo "Download success"
else
   echo "Download Faild.Check FRP_Version & OS"
fi
sleep 2

#创建/etc/frp目录
mkdir -p /etc/frp

#解压缩安装包到/etc/目录里面
tar -zxvf /root/frp_"$ver"_"$OS"_"$coreType".tar.gz -C /etc/

#将解压的文件夹重命名为frp
mv /etc/frp_"$ver"_"$OS"_"$coreType"/ /etc/frp/

#进入目录
cd /etc/frp/frp_"$ver"_"$OS"_"$coreType"

#将目录内文件移动至/etc/frp里面
mv ./* /etc/frp

#清除frp多余的目录
rm -rf /etc/frp/frp_"$ver"_"$OS"_"$coreType"

#声明frp服务端配置文件的位置
ServerConf="/etc/frp/frps.ini"

#声明frp客户端配置文件的位置
ClientConf="/etc/frp/frpc.ini"

#输入您的FRP服务器IP地址
read -t 5 -p "FRP_Server IP is(Tips:127.0.0.1)" frps_ip
if [ -z "${frps_ip}" ];then
	frps_ip=127.0.0.1
fi

#输入您的FRP服务器端口,默认是7000
read -t 5 -p "FRP_Server PORT is(Dafault:7000):" frps_port
if [ -z "${frps_port}" ];then
	frps_port=7000
fi

#输入您需要发布的服务器IP地址,可以是本机也可以是其他服务器
read -t 5 -p "Client_IP is(Dafault:127.0.0.1):" Client_IP
if [ -z "${Client_IP}" ];then
	Client_IP=127.0.0.1
fi

#输入您需要发布的服务器对应的服务端口,这里以SSH为例,SSH是22端口
read -t 5 -p "Client_PORT is(Dafault:22):" Client_PORT
if [ -z "${Client_PORT}" ];then
	Client_PORT=22
fi

#输入您需要对外提供SSH服务的端口,这里是2222端口
read -t 5 -p "Client_remote_port is(Dafault:2222):" Client_remote_port
if [ -z "${Client_remote_port}" ];then
	Client_remote_port=2222
fi

#输入您安装FRP服务端时输入的认证密码,默认123321
read -t 5 -p -s "FRP_token is(Dafault:123321):" token
if [ -z "${token}" ];then
  token=123321
fi

#安装客户端的信息确认
echo "Your FRP Server IP is: $frps_ip"
echo "Your FRP Server Port is: $frps_port"
echo "Your FRP Client IP is: $Client_IP"
echo "Your FRP Client Port is: $Client_PORT"
echo "Your FRP Client Remote Port is: $Client_remote_port"
echo "Your FRP_token is $token"
sleep 2

#配置客户端配置文件/etc/frpc.ini
cat>"$ClientConf"<<EOF
[common]
server_addr = $frps_ip
server_port = $frps_port
token = $token
[ssh] 
type = tcp	
local_ip = $Client_IP
local_port = $Client_PORT
remote_port = $Client_remote_port
EOF

#检测是否配置成功
if [ $? -eq 0 ];then
    echo "FRP_Client config success"
else
   echo "FRP_Client config Faild"
fi
sleep 1

#将客户端配置成Linux服务
frpcService="/etc/frp/systemd/frpc.service"

cat>"$frpcService"<<EOF
[Unit]
Description=FRP to bypass NAT Network
Wants = network-online.target
After=network.target 
[Service] 
Type=simple 
ExecStart=/etc/frp/frpc -c /etc/frp/frpc.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frps
StandardOutput = syslog
StandardError = inherit
PrivateTmp=true 
[Install] 
WantedBy=multi-user.target
EOF

cp "$frpcService" /lib/systemd/system/
systemctl daemon-reload
systemctl enable frpc.service

#启动frp客户端
echo "frp Client is starting..."
systemctl start frpc.service

#检测启动状态
if [ $? -eq 0 ];then
	systemctl status frpc.service |grep active |cut -d : -f 2
    echo "FRP start faild.check /etc/frp/frpc.ini"
else
   echo "Start success"
fi
clear
echo "如果您在安装过程中录入错误参数,可以通过vi /etc/frpc/ini 进行修改或者添加喔"

FAQ

  1. 如果FRP服务端安装的脚本参数输入错误怎么办?
    答:可以通过执行 vi /etc/frp/frps.ini 进行修改

  2. 如果FRP客户端安装的脚本参数输入错误怎么办?
    答:可以通过执行 vi /etc/frp/frps.ini 进行修改

  3. frp客户端运行失败,怎么处理?
    答:检查/etc/frp/frpc.ini的 服务器信息是否正确,能否正常与服务端的端口通信,可用telnet检测

  4. 复制的代码无法正常执行
    答:检查是否格式有无,可在vi文本编辑器里面转换格式

set ff=unix
  1. frp服务端服务启动、关闭命令是什么?
    答:
		 systemctl start frps.service      
		 systemctl stop frps.service
  1. frp客户端服务启动、关闭命令是什么?
    答:
	systemctl start frpc.service      
	systemctl stop frpc.service
  1. 脚本参数输入的时间5秒可以修改吗?
    答:可以在脚本中找到read -t 命令修改。
read -t 10 -p "input you time" var 
  1. frp下载的版本哪里可以看?
    答:https://github.com/fatedier/frp/releases/
    在这里插入图片描述

总结

本次编写的脚本主要便于大家随时随地可以轻松进行内网穿透。减少大家查看文档思考的时间,服务端脚本是可以无人值守静默部署的,客户端安装时需要输入正确的参数,如果参数没有输入正常,客户端是无法正常运行的。脚本在腾讯云Centos7.8里面测试过,请放心使用。

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值