内网穿透神器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.youkuaiyun.com/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.youkuaiyun.com/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里面测试过,请放心使用。

本处高能:因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、付费专栏及课程。

余额充值