CBrother腳本10分鐘寫一個拯救「小霸王服務器」的程序

CBrother腳本語言10分鐘寫一個拯救「小霸王服務器」的程序

到了一家新公司,接手了一坨c++服務器代碼,處處內存泄漏,這服務器沒有數據庫,掛了後重啓一下就行了,公司就這麼湊活着用了幾年了,定時重啓吧,也不是每天掛,不定時重啓吧,說不許哪天就掛了。。。。。。html

小公司,從寫代碼到維護就我一我的,每到下班後或者週末,電話一響,我就知道掛了,得找電腦重啓一下,要是出去玩了還得找網吧,裝個遠程軟件吧,公司說服務器以前被遠程軟件黑過,不許裝,煩。python

想着弄個什麼程序在手機上點一下就給重啓了就行了,咱這種寫C++後臺十幾年的人,手機APP是不會開了,那就弄個網頁服吧,選了半天,python的語法把我快看暈了。PHP研究一早上環境都沒搭起來。找其餘的,找到了一門CBrother腳本,語法跟C++共通之處,寫http接口極其簡單,跟做者聊了一下,10分鐘就寫出了我要的東西,如今我給公司其餘同事一人一個帳號,誰均可以重啓服務了。爲了感謝CBrother做者,這裏寫篇文章幫他宣傳一下。c++

主函數:

//入口函數,和c++同樣,啓動調用main函數,這點我喜歡
function main(params)
{
    var httpServer = new HttpServer();    //建立一個http服務
    httpServer.addAction("hello.cb",new HelloAction());    //註冊hello.cb接口響應類HelloAction
    httpServer.addAction("120120.cb",new Action120());        //註冊重啓接口爲120120.cb
    httpServer.setNormalAction("hello.cb");                //設置默認頁調用 hello.cb
    httpServer.startServer(11120);                         //設置服務端口爲11120
    
    //主線程不退出,除了語法簡化外,套路和c++簡直一摸同樣
    while (1)
    {
        Sleep(1000);
    }    
}

hello.cb:

class HelloAction
{
    //接口的執行函數
    function DoAction(request,respone)
    {
        //寫一個表單,這都是大學時候學的東西,十幾年了,幸好還記了一點點,哈哈
        respone.write("<html><head><title>搶救服務器</title><meta http-equiv=\"content-type\" content=\"txt/html; charset=utf-8\" /></head><body>");
        respone.write("<br><FORM method=\"post\" action=\"120120.cb\">");
        respone.write("<INPUT type=\"text\" name=\"username\"><br>");
        respone.write("<input type=\"password\" name=\"userpass\"><br>");
        respone.write("<input type=\"submit\" value=\"搶救\"></FORM></body><html>");
        respone.flush();
    }
}

120120.cb重啓服務器的操做:

var g_userMap = {"admin":"123.123","huangyi":"256.256","boss":".boss1boss"};//定義用戶密碼
var g_exepath = "D:\\work\\FVServer\\FVServer.exe";            //進程路徑
var g_exeName = "FVServer.exe";    //進程名稱

class Action120
{
    function DoAction(request,respone)
    {
        respone.write("<html><head><title>搶救服務器</title><meta http-equiv=\"content-type\" content=\"txt/html; charset=utf-8\" /></head><body>");
        var fromdata = request.getFormData();            //獲取表單數據
        if (fromdata == null)
        {
            respone.write("我不認識你</body><html>");
            respone.flush();
            return;
        }

        var userName = strlower(fromdata.getText("username"));    //獲取用戶名,轉小寫
        var pwd = fromdata.getText("userpass");
        var pwdlocal = g_userMap[userName];                        //判斷用戶是否存在
        if (pwdlocal == null)
        {
            respone.write("我不認識你</body><html>");
            respone.flush();
            return;            
        }

        if (pwdlocal != pwd)                                        //判斷密碼是否正確
        {
            respone.write("密碼錯誤</body><html>");
            respone.flush();            
            return;            
        }

        var oldid = 0;
        respone.write("搶救中...");
        var pidarr = GetProcessByName(g_exeName);                    //獲取進程ID列表,應該只有一個
        if (pidarr != null)
        {
            for (var i = 0; i < pidarr.size() ; i++)
            {
                print "查殺進程ID:" + pidarr[i];
                KillProcessByID(pidarr[i]);                    //查殺進程
            }
            Sleep(2000);
        }

        respone.write("<br>搶救中...");
        var pid = CreateProcess(g_exepath);                    //從新啓動一個
        respone.write("<br>搶救成功。進程ID:" + pid);
        respone.flush();

        //把當前時間打印出來
        var t = new Time();
        print userName + "搶救了服務器。" + oldid + "===>" + pid + t.strftime(" [%Y/%m/%d %H:%M:%S]");
    }
}

以後從官網(http://www.cbrother.net/)下載CBrother,而後再控制檯輸入啓動命令數據庫

d:\cbrother_v2.1.0_win64\cbrother.exe d:\cbrother\check.cb,服務器就啓起來了。編程

而後能夠經過手機訪問這個端口重啓服務器了服務器

 

 輸入帳號密碼重啓函數

 

 

再打電話也不用急急忙忙去找電腦了,手機一點就OK,打電話的人本身頁能夠重啓了。 post

服務器界面顯示學習

 

整體來講,CBrother腳本很符合個人編程理念,並且封裝的很簡單,後面還要深刻學習下。ui

相關文章
相關標籤/搜索