CBrother腳本語言10分鐘寫一個拯救“小霸王服務器”的程式
到了一家新公司,接手了一坨c++服務器代碼,到處記憶體泄漏,這服務器沒有資料庫,掛了后重啟一下就好了,公司就這么炊訓著用了幾年了,定時重啟吧,也不是天天掛,不定時重啟吧,說不準哪天就掛了,,,,,,
小公司,從寫代碼到維護就我一個人,每到下班后或者周末,電話一響,我就知道掛了,得找電腦重啟一下,要是出去玩了還得找網吧,裝個遠程軟體吧,公司說服務器之前被遠程軟體黑過,不準裝,煩,
想著弄個什么程式在手機上點一下就給重啟了就好了,咱這種寫C++后臺十幾年的人,手機APP是不會開了,那就弄個網頁服吧,選了半天,python的語法把我快看暈了,PHP研究一早上環境都沒搭起來,找其他的,找到了一門CBrother腳本,語法跟C++共通之處,寫http介面極其簡單,跟作者聊了一下,10分鐘就寫出了我要的東西,現在我給公司其他同事一人一個賬號,誰都可以重啟服務了,為了感謝CBrother作者,這里寫篇文章幫他宣傳一下,
主函式:
//入口函式,和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=https://www.cnblogs.com/"搶救\"></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 = https://www.cnblogs.com/wjsyj/p/request.getFormData(); //獲取表單資料 if (fromdata =https://www.cnblogs.com/wjsyj/p/= 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,打電話的人自己頁可以重啟了,
服務器界面顯示

總體來說,CBrother腳本很符合我的編程理念,而且封裝的很簡單,后面還要深入學習下,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/170819.html
標籤:JavaScript
上一篇:JS、JQ相關小技巧積攢
