個定時網路喚醒的Windows 2000服務(Service)程式

來源:互聯網
上載者:User

單位有兩台伺服器,一台的Bios中有定時啟動的功能,而另外一台沒有。所以每天早上到單位後第一件事情就是要去按一下這台伺服器的電源開關來開機,不甚其煩!今天編寫了一個定時網路喚醒的服務(Service)程式,安裝在第一台伺服器上,這樣當第一台伺服器啟動後,如果到了時間,就會自動通過網路發送WakeOnLine(WOL)的廣播包來喚醒另外一台伺服器。

步驟:

啟動Delphi,File-New-Other-Server Application,然後定義該Service的屬性

 

儲存檔案名稱為WakeOnlineSrv.pas,該單元的代碼如下:

unit WakeOnlineSrv;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  WakeOnlineExecuteThread;

type
  TWakeOnLineServer = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServicePause(Sender: TService; var Paused: Boolean);
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
  private
    { Private declarations }
    ThdWakeOnline:TWakeOnline;
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  WakeOnLineServer: TWakeOnLineServer;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  WakeOnLineServer.Controller(CtrlCode);
end;

function TWakeOnLineServer.GetServiceController: TServiceController;
begin
  Result:=ServiceController;
end;

procedure TWakeOnLineServer.ServiceStart(Sender: TService;
  var Started: Boolean);
begin
//啟動服務
  ThdWakeOnline:=TWakeOnline.Create(False);
  Started:=True;
end;

procedure TWakeOnLineServer.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
//停止服務
  ThdWakeOnline.Terminate;
  Stopped:=True;
end;

procedure TWakeOnLineServer.ServicePause(Sender: TService;
  var Paused: Boolean);
begin
//暫停服務
  ThdWakeOnline.Suspend;
  Paused:=True;
end;

procedure TWakeOnLineServer.ServiceContinue(Sender: TService;
  var Continued: Boolean);
begin
//恢複服務
  ThdWakeOnline.Resume;
  Continued:=True;
end;

end.

再從File-New-Other-Thread Object,建立一個單元檔案,儲存檔案名稱為WakeOnlineExecuteThread.pas,代碼如下:

unit WakeOnlineExecuteThread;

interface

uses
  Classes, IdUDPClient, SysUtils;

Const
  WakeUpTime='07:50';  //Notice that Not 7:50!
  StopSendTime='07:51';
  TargetComputerMacAddress='00025556E5C5';

type
  TWakeOnLine = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;

var Sent:Boolean=False;

implementation

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure WakeOnLine.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ WakeOnLine }

procedure TWakeOnLine.Execute;
var
  MacAddr:String;
  FUDP: TIdUDPClient;
  SendCode:string;
  I,J: Integer;
  Bin: String[6];
begin
{ Place thread code here }
  //線程終止時自動釋放
  Self.FreeOnTerminate:=True;
  //發送標誌
  Sent:=False;
  //如果線程沒有被終止則運行下面的迴圈
  while not Terminated do
  begin
    //如果沒有發送,並且已經到了發送的時間
    if (not Sent) and (Copy(FormatDateTime('hh:mm:ss',Now),1,5)=WakeUpTime) then
    begin
      //開始發送
      MacAddr:=TargetComputerMacAddress;

      FUDP:=TIdUDPClient.Create(nil);
      try
        FUDP.Port:=7;
        FUDP.Host:='10.10.10.2';
        FUDP.Active:=True;

        HextoBin(PChar(MacAddr),@Bin,6);
        SendCode:=StringofChar(#255,6);

        //填充緩衝區
        for I:=0 to 15 do
          for J:=0 to 5 do
            SendCode:=SendCode+Bin[J];

        //連續發送6次,保險啊。。。
        for I:=0 to 5 do
          FUDP.Broadcast(SendCode,7);

      finally
        FUDP.Free;
      end;

      Sent:=True;

    end else

    begin
      //如果已經發送,並且已經到了發送時間的下一分鐘,則重設發送標誌以備下一天還能發送
      if Sent and (Copy(FormatDateTime('hh:mm:ss',Now),1,5)=StopSendTime) then
         Sent:=False;

      //休息30秒,避免浪費資源
      Sleep(30000);
    end;

    //如果有終止訊號則終止
    if Terminated then Break;

  end;

end;

end.

最後儲存工程檔案名稱為WakeOnline.dpr,內容如下:

program WakeOnline;

uses
  SvcMgr,
  WakeOnlineSrv in 'WakeOnlineSrv.pas' {WakeOnLineServer: TService},
  WakeOnlineExecuteThread in 'WakeOnlineExecuteThread.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TWakeOnLineServer, WakeOnLineServer);
  Application.Run;
end.

給這個服務程式選擇一個合適的表徵圖,最後編譯產生一個名稱為WakeOnLine.Exe可執行檔。

到Cmd視窗去安裝該服務程式:執行:WakeOnLine -install,會提示服務已經成功安裝

然後滑鼠右擊我的電腦-管理-服務和應用程式-服務,然後找到剛剛安裝好的服務:Wake On Line Server,

 

可以看到它還沒有啟動,現在雙擊它,點擊啟動:

我在程式中指定的時間是早上7:50分發送廣播,啟動一個網路包抓取程式,將時間調節到早上7:49來測試一下,等帶一會兒,看看抓到了什麼:

 

目標機器啟動啦。。。

(說明:Delphi 7編譯;CISCO 2950交換器;被喚醒的是IBM X205伺服器,注意將Bios中的Wake On Line選項Enable)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.