在shell脚本中用curl处理服务器开机、关机、强制关机、重启动作 获取服务器的开关机状态服务器的开机、关机、强制关机、复位脚本 思路:利用了一张主控板来获取服务器的开关机状态,开关机其
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文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-11
2024-05-11
2024-05-10
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0