超微和AMI的IPMI web端进行远程操作开关电源的一些数据

首先是超微的一些资料

超微现成的车轮https://github.com/serverzone/Supermicro-ipmi/

获取登陆信息的数据

SESSION_ID=$(curl -s "http://${IPMI_HOST}/cgi/login.cgi" --data "name=${IPMI_USER}&pwd=${IPMI_PASS}" -i | awk '/SID=[^;]/ { print $2 }')
TOKEN=`curl -s "https://${IPMI_HOST}/cgi/url_redirect.cgi?url_name=sys_info" -H "Cookie: ${SESSION_ID}" --insecure |grep CSRF_TOKEN  |  cut -d\" -f 4`

#rest bmc
curl "https://${IPMI_HOST}/cgi/BMCReset.cgi?time_stamp=Thu%20Sep%2011%202014%2017%3A07%3A02%20GMT-0500%20(CDT)&_="  -H "CSRF_TOKEN:$TOKEN;"    -H "Cookie: ${SESSION_ID}" --insecure  -I


#电源操作
curl "http://${IPMI_HOST}/cgi/ipmi.cgi" -H "CSRF_TOKEN: $TOKEN" -H "Cookie: $SESSION_ID" -H "Referer: http://${IPMI_HOST}/cgi/url_redirect.cgi?url_name=man_chassis" --data 'GET_POWER_INFO.XML=(0%2C0)' --insecure

#必须要带TOKEN 带 Referer url_name=man_chassis才能操作电源

 

操作电源选项接口信息 (抓取地址https://xxxxx.xxxx.xxx/cgi/url_redirect.cgi?url_name=man_chassis)

          url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,0)&time_stamp=' + (new Date ());

电源状态判断 currentPwrStatus == 0  status Power Off 

 

    function getPwrStatus ()
    {
        Loading (true);
        var url    = '../cgi/ipmi.cgi';
        var pars   = '?GET_POWER_INFO.XML=(0,0)&time_stamp=' + (new Date ());
        var myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: getPwrStatusHandler}
                                      );
    }
          function getPwrStatusHandler (originalRequest)
    {
        Loading (false);
        if ( (originalRequest.readyState == 4) && (originalRequest.status == 200) ) {
            var response   = originalRequest.responseText.replace (/^\s+|\s+$/g, "");
            var xmldoc     = GetResponseXML (response);
            var IPMIRoot   = xmldoc.documentElement;//point to IPMI
            var POWER_INFO = IPMIRoot.getElementsByTagName ('POWER_INFO');//point to SENSOR_INFO
            var POWER      = POWER_INFO[0].getElementsByTagName ('POWER');
            var pwrStatus  = POWER[0].getAttribute ("STATUS");

            currentPwrStatus = (pwrStatus == "OFF") ? 0 : 1;
            if (expectStatus != 0xf) {
                if (currentPwrStatus != expectStatus) {
                    retryCount--;
                    Loading (true);
                    if (retryCount == 0) {
                        Loading (false);
                        var cableStr = externalBMC ? cableChkBMC:cableChkFeature;
                        if (pwr_Action == 5) {
                            alert (lang.LANG_CHASSIS_SOFTOFF_FAIL + cableStr);
                        }
                        else {
                            alert (lang.LANG_CHASSIS_ACTION_FAIL);
                        }
                        return;
                    }

                    setTimeout ("getPwrStatus()", 10000);
                    return;
                }
                else {
                    if ( (pwr_Action == 2) && (expectStatus == 0) ) {
                        document.getElementById ("currentStatus").innerHTML   = lang.LANG_CHASSIS_STATUS_OFF;
                        document.getElementById ("currentStatus").style.color = '#009900';
                        document.getElementById ("pwrReset").checked          = false;
                        document.getElementById ("pwrReset").disabled         = true;

                        document.getElementById ("pwrImmOff").checked         = false;
                        document.getElementById ("pwrImmOff").disabled        = true;

                        document.getElementById ("pwrOrdOff").checked         = false;
                        document.getElementById ("pwrOrdOff").disabled        = true;

                        document.getElementById ("pwrCycle").checked          = true;
                        document.getElementById ("pwrCycle").disabled         = true;

                        document.getElementById ("pwrOn").checked             = false;
                        document.getElementById ("pwrOn").disabled            = true;
                        expectStatus = 1;
                        setTimeout ("getPwrStatus()", 10000);
                        return;
                    }
                }
            }

            if (currentPwrStatus == 0) {
                document.getElementById ("currentStatus").innerHTML   = lang.LANG_CHASSIS_STATUS_OFF;
                document.getElementById ("currentStatus").style.color = '#009900';

                if (!disable) {
                    document.getElementById ("pwrReset").checked  = false;
                    document.getElementById ("pwrReset").disabled = true;

                    document.getElementById ("pwrImmOff").checked  = false;
                    document.getElementById ("pwrImmOff").disabled = true;

                    document.getElementById ("pwrOrdOff").checked  = false;
                    document.getElementById ("pwrOrdOff").disabled = true;

                    document.getElementById ("pwrCycle").checked  = false;
                    document.getElementById ("pwrCycle").disabled = true;

                    //power on == >power off
                    document.getElementById("pwrOn").checked  = true;
                    document.getElementById("pwrOn").disabled = false;
                }
                pwr_Action = 1;
                retryCount = 5;
            }
            else {
                document.getElementById ("currentStatus").innerHTML   = lang.LANG_CHASSIS_STATUS_ON;
                document.getElementById ("currentStatus").style.color = '#009900';
                //power off --> power on
                if (!disable) {
                    document.getElementById ("pwrReset").checked  = true;
                    document.getElementById ("pwrReset").disabled = false;

                    document.getElementById ("pwrImmOff").checked  = false;
                    document.getElementById ("pwrImmOff").disabled = false;

                    document.getElementById ("pwrOrdOff").checked  = false;
                    document.getElementById ("pwrOrdOff").disabled = false;

                    document.getElementById ("pwrCycle").checked  = false;
                    document.getElementById ("pwrCycle").disabled = false;

                    document.getElementById ("pwrOn").checked  = false;
                    document.getElementById ("pwrOn").disabled = true;
                }

                pwr_Action = 3;
                retryCount = 5;
            }
        }
        else {
            timeoutCount++
            if (timeoutCount <= 5) {
                setTimeout ("getPwrStatus()", 5000);
            }
            else {
                alert ("Retry time out. Please check your LAN connection.");
            }
        }
    }

    function selPwrReset ()
    {
        pwr_Action = 3;
    }

    function selPwrImmOff ()
    {
        pwr_Action = 0;
    }

    function selPwrOrdOff ()
    {
        pwr_Action = 5;
    }

    function selPwrOn ()
    {
        pwr_Action = 1;
    }

    function selPwrCycle ()
    {
        pwr_Action = 2;
    }

    function doPwrActionHandler ()
    {
        if (pwr_Action == 5) {
            setTimeout ("getPwrStatus()", 15000);
        }
        else {
            getPwrStatus ();
        }
    }

    function doPwrAction ()
    {
        var url;
        var pars;
        var myAjax;

        switch (pwr_Action) {
        case 0:     // Reset
            expectStatus = 0;
            Loading (true);
            url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,0)&time_stamp=' + (new Date ());
            myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: doPwrActionHandler}
                                      );
            break;
        case 1:     // Off Immediate
            expectStatus = 1;
            Loading (true);
            url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,1)&time_stamp=' + (new Date ());
            myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: doPwrActionHandler}
                                      );
            break;
        case 2:     // Off Orderly
            expectStatus = 0; //power on after some time I suppose
            Loading (true);
            url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,2)&time_stamp=' + (new Date ());
            myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: doPwrActionHandler}
                                      );
            break;
        case 3:     // On
            expectStatus = 1;
            Loading (true);
            url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,3)&time_stamp=' + (new Date ());
            myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: doPwrActionHandler}
                                      );
            break;
        case 5:     // Cycle
            expectStatus = 0;
            Loading (true);
            url    = '../cgi/ipmi.cgi';
            pars   = '?SET_POWER_INFO.XML=(1,5)&time_stamp=' + (new Date ());
            myAjax = new Ajax.Request (url,
                                       {method:     'post',
                                        parameters: pars,
                                        onComplete: doPwrActionHandler}
                                      );
            break;
        }
    }

 

AMI的ASPEED通用ipmi的资料如下

 

POST http://XX.xxx.xxx.xxx/rpc/hostctl.asp    WEBVAR_POWER_CMD=2

WEBVAR_POWER_CMD: 0  poweroff Power Off Server - Immediate
WEBVAR_POWER_CMD: 1  poweron  Power On Server
WEBVAR_POWER_CMD: 3  power reset
WEBVAR_POWER_CMD: 5  Power Off Server - Orderly Shutdown
WEBVAR_POWER_CMD: 2  Power Cycle Server


http:///XX.xxx.xxx.xxx/rpc/hoststatus.asp
 { 'JF_STATE' : 0 } 关机
  { 'JF_STATE' : 0 } 开机
LOGIN_ENDPOINT=https://$MGMT_IP/rpc/WEBSES/create.asp
RC_ENDPOINT=https://$MGMT_IP/rpc/hostctl.asp

curl --silent -X POST \
        -H "Content-Type: application/x-www-form-urlencoded" \
    -d "WEBVAR_USERNAME=root&WEBVAR_PASSWORD=root" \
    --insecure \
    $LOGIN_ENDPOINT | grep SESSION_COOKIE | awk '{print "SessionCookie="$4}' | sed "s/'//g" > session.cookie

curl --silent -X POST \
    -H "Cookie: $(<session.cookie)" \
        -H "Content-Type: application/x-www-form-urlencoded" \
    -d "WEBVAR_POWER_CMD=$RC_CMD" \
    --insecure \
    $RC_ENDPOINT
@zbuse

 

 

用shell环境实现的

function ASTjnlp(){
Getdata=`curl -s -k  --insecure --data "WEBVAR_USERNAME=$USER&WEBVAR_PASSWORD=$PASS" http://$HOST/rpc/WEBSES/create.asp | grep SESSION_COOKIE | sed  "s#' : '#=#g"`
COOKIE=`echo $Getdata|  cut -d\' -f 2|sed 's/SESSION_COOKIE/SessionCookie/g'`;
TOKEN=`echo $Getdata|  cut -d\' -f 8|sed 's/=/ : /g'`;
curl -s -k   --insecure --cookie Cookie=$COOKIE "http://$MGMT_IP/Java/jviewer.jnlp?EXTRNIP=$HOST&JNLPSTR=JViewer"
}


function ASTPower(){
Getdata=`curl -s -k  --insecure --data "WEBVAR_USERNAME=$USER&WEBVAR_PASSWORD=$PASS" http://$HOST/rpc/WEBSES/create.asp | grep SESSION_COOKIE | sed  "s#' : '#=#g"`
COOKIE=`echo $Getdata|  cut -d\' -f 2|sed 's/SESSION_COOKIE/SessionCookie/g'`;
TOKEN=`echo $Getdata|  cut -d\' -f 8|sed 's/=/ : /g'`;
curl "http://$HOST/rpc/hostctl.asp"   -H "$TOKEN"  -H "Cookie: SessionCookie=$COOKIE"  -d 'WEBVAR_POWER_CMD=3'  --insecure
}

操作电源需要一个CSRFTOKEN

添加新评论 »