文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-09-29 22:43

关注

在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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-服务器
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯