Script to configure and manage frp service with one click

Script to configure and manage frp service with one click

Introduction: This is a simple and practical Bash script that helps you configure and manage frp services with one click. With this script, you can easily create a service file for frp and use the systemctl command to start, stop, restart, and reload the frp service configuration file.

text:

Frp is a powerful reverse proxy tool for mapping external traffic to services on your internal network. However, there is no service management in the official installation package, which makes the operation and management of the program relatively troublesome. In order to simplify the configuration and management process of the frp service, a Bash script is used, which can automatically create the frp service file and provide some commonly used systemctl commands for managing the frp service.

prerequisite

  1. Your system needs to have the Bash shell installed.
  2. frp has been correctly installed in the system and the path has been set. The command is used in the script findto find the executable files and configuration files of frpc and frps, so you need to ensure that these files exist in the system.
  3. appropriate permissions. Running the script requires sufficient permissions to create and modify system service files /etc/systemd/system/frpc.serviceand /etc/systemd/system/frps.service.
  4. Knowledge of systemd and systemctl commands. The script uses the systemctl command to start, stop, restart, and reload the frp service.

Please ensure that the above requirements are met before running the script, and use scripts with caution to configure and manage the frp service.

Quick to use

# 安装脚本
curl -O https://raw.githubusercontent.com/liuyuanchengweb/create_frp_service/main/create_frp_service.sh
# 或者
wget https://raw.githubusercontent.com/liuyuanchengweb/create_frp_service/main/create_frp_service.sh
# 或者
curl -O https://gitee.com/useryc/create_frp_service/raw/main/create_frp_service.sh
# 或者
wget https://gitee.com/useryc/create_frp_service/raw/main/create_frp_service.sh
# 给脚本添加执行权限
chmod +x create_frp_service.sh
# 执行脚本
./create_frp_service.sh

Script implementation

  1. First, findfind specific files in the system through commands, such as the paths of frpc, frpc.ini, frpsand frps.ini, and store the results in the corresponding variables.
  2. frpc_serviceNext, two functions and are defined frps_service, which are used to create configuration files of frpc.serviceand respectively frps.service.
  3. In frpc_servicethe function, first check whether the file already exists frpc.service, and if it exists, output a prompt message; if it does not exist, check frpcwhether frpc.inithe file exists. If it exists, use catthe command to frpc.servicewrite the configuration content into the file /etc/systemd/system/frpc.service, and output a prompt message indicating that the creation is successful. .
  4. In frps_servicethe function, also check whether frps.servicethe file already exists, and if it exists, output a prompt message; if it does not exist, check whether the file exists, and frpsif frps.iniit exists, use catthe command to frps.servicewrite the configuration content into the file /etc/systemd/system/frps.service, and output a prompt message indicating that the creation is successful. .
  5. Finally, a function is defined load_frpthat calls the frpc_serviceand frps_servicefunction and then uses systemctl daemon-reloadthe command to reload systemd's configuration file. Finally, the configuration completion and related command prompts are output.

Generally speaking, the implementation mechanism of this script is to create a corresponding service configuration file based on the existence of a specific file, and then manage the service through the systemd command.

Script code:

#! /bin/bash

frpc=$(find / -name "frpc")
frpc_ini=$(find / -name "frpc.ini")
frps=$(find / -name "frps")
frps_ini=$(find / -name "frps.ini")
frpc_service="/etc/systemd/system/frpc.service"
frps_service="/etc/systemd/system/frps.service"

frpc_service(){
    
    
    if [ -f $frpc_service ];then
        echo -e "frpc服务文件以存在$frpc_service"
    else
        if [ -f $frpc ] && [ -f $frpc_ini ];then
            cat > /etc/systemd/system/frpc.service << EOF
[Unit]
Description=Frpc Service
After=network.target

[Service]
ExecStart=$frpc -c $frpc_ini
ExecReload=/bin/kill -HUP
Restart=always

[Install]
WantedBy=multi-user.target
EOF
            echo -e "创建frpc.service文件成功,文件路径:$frpc_service"
        else
            echo -e "没有找到frpc程序的路径和frpc.ini文件的路径"

        fi
    fi
}

frps_service(){
    
    
    if [ -f $frps_service ];then
        echo -e "frps服务文件以存在$frps_service"
    else
        if [ -f $frps ] && [ -f $frps_ini ];then
            cat > /etc/systemd/system/frps.service << EOF
[Unit]
Description=Frps Service
After=network.target

[Service]
ExecStart=$frps -c $frps_ini
ExecReload=/bin/kill -HUP
Restart=always

[Install]
WantedBy=multi-user.target
EOF
            echo -e "创建frpc.service文件成功,文件路径:$frpc_service"
        else
            echo -e "没有找到frps程序的路径和frps.ini文件的路径"

        fi
    fi
}


load_frp(){
    
    
    frpc_service 
    frps_service
    systemctl daemon-reload
    echo -e "配置frp服务完成"
    echo -e "使用systemctl enable --now frpc.service 命令设置frpc开机自动启动并立即启动服务"
    echo -e "使用systemctl status frpc.service 命令查看frpc服务状态信息"
    echo -e "使用systemctl stop frpc.service 停止 frpc 服务"
    echo -e "使用systemctl disable frpc.service 将 frpc 服务从开机自动启动中移除"
    echo -e "使用systemctl restart frpc.service 将 frpc 服务重新启动"
    echo -e "使用systemctl reload frpc.service 重新加载frpc的配置文件"
    echo -e "使用systemctl enable --now frps.service 命令设置frps开机自动启动并立即启动服务"
    echo -e "使用systemctl status frps.service 命令查看frps服务状态信息"
    echo -e "使用systemctl stop frps.service 停止 frps 服务"
    echo -e "使用systemctl disable frps.service 将 frps 服务从开机自动启动中移除"
    echo -e "使用systemctl restart frps.service 将 frps 服务重新启动"
    echo -e "使用systemctl reload frps.service 重新加载frps的配置文件"
}
load_frp

Instructions

Follow these steps to use:

  1. Copy the above code to your own system to create an sh script file

  2. Give the script execution permissionchmod +x sh脚本文件

  3. Execute script./sh脚本文件

  4. Wait for the script execution to complete and output the corresponding results.

Precautions

When using this script, you need to pay attention to the following things:

  1. Permissions: Make sure you run the script as a user with sufficient permissions to be able to create and modify system files.

  2. File path: The script uses findcommands to find the path of specific files. Please ensure that the relevant software has been correctly installed and these files exist in the system before using the script. If the file path is incorrect or the file does not exist, the script will not run properly.

  3. Script path: Save the script to a suitable location and give it executable permissions. You can modify the file path and service configuration information in the script as needed.

  4. Conflict checking: When creating a service configuration file, the script checks whether the corresponding file already exists in the target path. If it already exists, the corresponding prompt message will be output. This avoids duplicating or overwriting existing configuration files.

  5. systemd service management: Commands are used in the script systemctlto manage services, such as starting, stopping, reloading and setting automatic startup at boot. Make sure you have systemd installed correctly and understand systemctlhow to use the commands.

  6. Note: The script outputs a series of command systemctlprompts at the end to remind users how to operate and manage the configured services. Please execute the corresponding command according to the prompt information.

Please note that before using the script, it is recommended to read the code of the script carefully and make appropriate modifications and adjustments according to the actual situation. Make sure you understand the function and operation mechanism of the script in order to use and manage the frp service correctly.

Guess you like

Origin blog.csdn.net/qq_41816198/article/details/131553532