分类
折腾

Linux 添加 RSX 命令来一键执行远程脚本

每当在VPS上使用网友撰写的 Shell 脚本时,都要先用 wget 下载,然后再用 bash 来执行。添加这一段文本到 ~/.bashrc 就可以使用 rsx (Remote Script Execute) 命令来一键下载执行。

现在添加

添加以下内容至当前用户家目录下的 .bashrc 文件:

rsx() { if [ $# -eq 0 ]; then echo -e "No arguments specified. Usage: rsx [URL]"; return 1; fi; wget -N --no-check-certificate -O /tmp/remote_script.sh $1 >/dev/null 2>&1; if [ $? -ne 0 ]; then echo -e "\033[41;37mAn error occured when downloading script.\033[0m"; echo -e "\033[41;37mCheck your Internet connection.\033[0m"; return 2; fi; bash /tmp/remote_script.sh; rm -rf /tmp/remote_script.sh >/dev/null 2>&1; }

然后使用

source ~/.bashrc

来重新加载 bash 配置档。

使用方法

rsx [脚本URL]

脚本URL:脚本的下载地址。只要 wget 命令能够识别,那就不需要添加 http:// 或 https://

  • wget 已经添加了 –no-check-certificate 选项,在处理 HTTPS 链接时不会检查证书有效性。
  • 脚本下载后,会暂存于 /tmp/remote_script.sh

错误代码

  • 1:没有指定 URL。
  • 2:wget 在下载文件时出现问题。

完整的函数

function rsx() {
    if [ $# -eq 0 ]; then 
        echo -e "No arguments specified. Usage: rsx [URL]";
        return 1;
    fi

    wget -N --no-check-certificate -O /tmp/remote_script.sh $1 >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo -e "\033[41;37mAn error occured when downloading script.\033[0m"
        echo -e "\033[41;37mCheck your Internet connection.\033[0m"
        return 2;
    fi

    bash /tmp/remote_script.sh
    rm -rf /tmp/remote_script.sh >/dev/null 2>&1
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注