思路:利用了一張主控板來獲取服務器的開關機狀態(tài),開關機其實是給服務器一個500ms~1000ms的脈沖,等同與按了機箱面板的開關機按鈕開關。
獲取服務器的開關機狀態(tài)
if [ $# -lt 1 ]; then
echo "no ip"
exit 1
fi
IP=$1
#-----------------------------
if [ $# -eq 1 ]; then
#-----------------------------
# 只有1個參數(shù),第一通道獲取開關機狀態(tài)
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個參數(shù),第一通道獲取開關機狀態(tài)
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個參數(shù),第一通道測試開關機、復位
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個參數(shù) ,多通道測試開關機、復位
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個參數(shù),開關機循環(huán)測試
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
文章來源地址http://www.zghlxwxcb.cn/news/detail-733319.html
文章來源:http://www.zghlxwxcb.cn/news/detail-733319.html
到了這里,關于在SHELL腳本中用curl處理服務器開機、關機、強制關機、重啟動作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!