admin管理员组

文章数量:1625857

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

  • 获取服务器的开关机状态
  • 服务器的开机、关机、强制关机、复位脚本

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

获取服务器的开关机状态

if [ $# -lt 1 ]; then
    echo "no ip"
    exit 1
fi

IP=$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 1
fi

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

#!/bin/bash


if [ $# -lt 1 ]; then
    echo "no ip"
    exit 1
fi

IP=$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 1
fi

echo " " >resultfail.txt

OP=$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
    fi
fi
#-----------------------------


exit 0


本文标签: 重启中用脚本动作服务器