7

ssh一键部署frpc内网穿透脚本

 1 year ago
source link: https://www.wyr.me/post/735
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ssh一键部署frpc内网穿透脚本 - 轶哥

ssh一键部署frpc内网穿透脚本

以下是实现自动安装frpc的Shell脚本,实现ssh的22端口内网穿透,注册systemd服务开机自动启动。

直接使用线上地址快速下载并执行Shell脚本一件部署可以使用以下命令:

使用curl:

curl -sSL https://openapi.site/install_frpc.sh | bash -s 端口号 token server_addr [server_port=7000]

使用wget:

wget -qO- https://openapi.site/install_frpc.sh | bash -s 端口号 token server_addr [server_port=7000]

其中端口号、token、server_addr为必须传入的参数,server_port为可选参数,如果没有传入则默认为7000。请根据自己的需要替换相应的参数值。

如果执行成功,将会输出“Successfully started frpc”,否则会输出失败信息。

附:

install_frpc.sh文件内容:

#!/bin/bash

# 获取参数
if [ $# -lt 3 ]; then
    echo "Usage: $0 port token server_addr [server_port=7000]"
    exit 1
fi
port=$1
token=$2
server_addr=$3
server_port=${4:-7000}

# 获取操作系统类型和架构
case $(uname -m) in
    x86_64)
        arch="amd64"
        ;;
    i386 | i686)
        arch="386"
        ;;
    armv6l | armv7l | armv7hl)
        arch="arm"
        ;;
    aarch64 | arm64)
        arch="arm64"
        ;;
    mips*)
        if [ "$(uname -m)" = "mips64" ]; then
            arch="mips64"
        elif [ "$(uname -m)" = "mips64el" ]; then
            arch="mips64le"
        elif [ "$(uname -m)" = "mips" ]; then
            arch="mips"
        else
            arch="mipsle"
        fi
        ;;
    riscv64)
        arch="riscv64"
        ;;
    *)
        echo "Unsupported architecture $(uname -m)"
        exit 1
esac

case $(uname -s) in
    Linux)
        os_type="linux"
        ;;
    Darwin)
        os_type="darwin"
        ;;
    *)
        echo "Unsupported operating system $(uname -s)"
        exit 1
esac

# 下载最新版frp压缩文件
wget -O frp.tar.gz "https://nat.openapi.site/frp/download?os_type=${os_type}&arch=${arch}"
if [ $? -ne 0 ]; then
    echo "Failed to download frp"
    exit 1
fi

# 解压文件并拷贝可执行文件到/usr/bin/frpc目录下
tar -zxvf frp.tar.gz
if [ $? -ne 0 ]; then
    echo "Failed to extract frp"
    exit 1
fi
sudo mv frp*/frpc /usr/bin/frpc

# 创建/etc/frp/目录和配置文件
sudo mkdir -p /etc/frp/
if [ ! -f /etc/frp/frpc.ini ]; then
cat > frpc.ini << EOF
[common]
token = ${token}
server_addr = ${server_addr}
server_port = ${server_port}

[ssh-${os_type}-${arch}-${port}]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = ${port}
EOF
fi
sudo mv frpc.ini /etc/frp/frpc.ini

# 创建service配置文件
cat > frpc.service << EOF
[Unit]
Description=Frp Client Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target
EOF

# 根据操作系统类型放置service配置文件到对应的systemd目录下
if [[ "$(uname -s)" == "Linux" ]]; then
    if [[ -d "/lib/systemd/system" ]]; then
        sudo mv frpc.service /lib/systemd/system/frpc.service
    elif [[ -d "/usr/lib/systemd/system" ]]; then
        sudo mv frpc.service /usr/lib/systemd/system/frpc.service
    else
        echo "Failed to find systemd directory"
        exit 1
    fi
else
    echo "Unsupported operating system"
    exit 1
fi

rm -rf frp*

# 启动frpc
sudo systemctl enable frpc --now
if [ $? -eq 0 ]; then
    echo "Successfully started frpc"
else
    echo "Failed to start frpc"
    exit 1
fi

echo -e "\n"
systemctl status frpc

使用方式:

bash install_frpc.sh 端口号 token server_addr [server_port=7000]

其中端口号、token、server_addr为必须传入的参数,server_port为可选参数,如果没有传入则默认为7000。

shell中下载最新版frp压缩文件的中转站请参考《frp更新中心API》


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK