iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >在SHELL脚本中用curl处理服务器开机、关机、强制关机、重启动作
  • 368
分享到

在SHELL脚本中用curl处理服务器开机、关机、强制关机、重启动作

服务器运维 2023-09-29 22:09:47 368人浏览 泡泡鱼
摘要

在shell脚本中用curl处理服务器开机、关机、强制关机、重启动作 获取服务器的开关机状态服务器的开机、关机、强制关机、复位脚本 思路:利用了一张主控板来获取服务器的开关机状态,开关机其

shell脚本中用curl处理服务器开机、关机、强制关机、重启动作


思路:利用了一张主控板来获取服务器的开关机状态,开关机其实是给服务器一个500ms~1000ms的脉冲,等同与按了机箱面板的开关机按钮开关。

获取服务器的开关机状态

if [ $# -lt 1 ]; then    echo "no ip"    exit 1fiIP=$1#-----------------------------if [ $# -eq 1 ]; then#-----------------------------# 只有1个参数,第一通道获取开关机状态    rsp=$(curl -X GET "Http://${IP}/led0.cgi?led0_2&WEBToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)    ret=$(echo "$rsp" | awk -F ';' '{print $1}' | grep red)    if [ ! -z "${ret}" ]; then        echo "ON"        exit 0    fi    ret=$(echo "$rsp" | awk -F ';' '{print $1}' | grep black)    if [ ! -z "${ret}" ]; then        echo "OFF"        exit 0    fi    echo "Unknown"    exit 1fi

服务器的开机、关机、强制关机、复位脚本

#!/bin/bashif [ $# -lt 1 ]; then    echo "no ip"    exit 1fiIP=$1#-----------------------------if [ $# -eq 1 ]; then#-----------------------------# 只有1个参数,第一通道获取开关机状态    rsp=$(curl -X GET "http://${IP}/led0.cgi?led0_2&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)    ret=$(echo "$rsp" | awk -F ';' '{print $1}' | grep red)    if [ ! -z "${ret}" ]; then        echo "ON"        exit 0    fi    ret=$(echo "$rsp" | awk -F ';' '{print $1}' | grep black)    if [ ! -z "${ret}" ]; then        echo "OFF"        exit 0    fi    echo "Unknown"    exit 1fiecho " " >resultfail.txtOP=$2#-----------------------------if [ $# -eq 2 ]; then#-----------------------------# 只有2个参数,第一通道测试开关机、复位    if [[ "${OP}" == "on" ]]; then        rsp=$(curl -X GET "http://${IP}/led0.cgi?led0_0=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "on succ"        else            echo "on fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "foff" ]]; then        rsp=$(curl -X GET "http://${IP}/led0.cgi?led0_1=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "off" ]]; then        rsp=$(curl -X GET "http://${IP}/led0.cgi?led0_4=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "rst" ]]; then        rsp=$(curl -X GET "http://${IP}/led0.cgi?led0_3=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    else        echo "Invalid Op"        exit 1    fi#-----------------------------elif [ $# -eq 3 ]; then#-----------------------------    # 有3个参数 ,多通道测试开关机、复位    CH=$3    if [ $CH -lt 0 ]; then        echo "Channel number:0~8"        exit 1    fi    if [ $CH -gt 8 ]; then        echo "Channel number:0~8"        exit 1    fi        if [[ "${OP}" == "on" ]]; then        rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_0=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "on succ"        else            echo "on fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "foff" ]]; then        rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_1=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "off" ]]; then        rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_4=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    elif [[ "${OP}" == "rst" ]]; then        rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_3=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)        ret=$(echo "$rsp" | grep '.gif' | wc -l)        if [ ${ret} -gt 0 ];then            echo "${OP} succ"        else            echo "${OP} fail"            echo "${OP} fail" >>resultfail.txt            exit 1        fi    else        echo "Invalid Channel"        exit 1    fi#-----------------------------elif [ $# -eq 4 ]; then#-----------------------------    # 有4个参数,开关机循环测试    CH=$3    TIMES=$4        if [ $CH -lt 0 ]; then        echo "Channel number:0~8"        exit 1    fi    if [ $CH -gt 8 ]; then        echo "Channel number:0~8"        exit 1    fi    if [ $TIMES -lt 0 ]; then        echo "times number:1~10000"        exit 1    fi    if [ $TIMES -gt 10000 ]; then        echo "Channel number:0~10000"        exit 1    fi        if [ "${OP}" == "on" ] || [ "${OP}" == "foff" ] || [ "${OP}" == "off" ] || [ "${OP}" == "rst" ]; then        for ((i=1;i<=$TIMES ;i++))        do             rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_0=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)            ret=$(echo "$rsp" | grep '.gif' | wc -l)            if [ ${ret} -gt 0 ];then                echo "on succ"            else                echo "on fail"                #exit 1            fi            sleep 2            rsp=$(curl -X GET "http://${IP}/led${CH}.cgi?led${CH}_1=0&WebToken=18c70020e5240008550c0008570c0008&t=1692190322" 2> /dev/null)            ret=$(echo "$rsp" | grep '.gif' | wc -l)            if [ ${ret} -gt 0 ];then                echo "${OP} succ"            else                echo "${OP} fail"                echo "${i}----${OP} fail" >>resultfail.txt                #exit 1            fi            sleep 7        done    else        echo "Invalid TIMES"        exit 1    fifi#-----------------------------exit 0

来源地址:https://blog.csdn.net/kingpower2018/article/details/132424189

--结束END--

本文标题: 在SHELL脚本中用curl处理服务器开机、关机、强制关机、重启动作

本文链接: https://www.lsjlt.com/news/421653.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作