線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1845
推到 Plurk!
推到 Facebook!

TIdSNMP

尚未結案
astiya
一般會員


發表:9
回覆:7
積分:3
註冊:2003-08-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-29 15:25:01 IP:61.222.xxx.xxx 未訂閱
小弟最近想試著用TIdSNMP元件寫一些東西,可是一直找不到範例或說明,不知是否有熟悉這個元件的前輩能提供幾個簡單的範例作參考。
andychang1690
資深會員


發表:20
回覆:694
積分:442
註冊:2003-03-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-29 21:25:40 IP:202.104.xxx.xxx 未訂閱
BCB的沒有Deliphi的有
http://chuacw.hn.org/chuacw/archive/2004/01/28/206.aspx
Using ICMP and SNMP to detect the dynamic IP address of your router 
The following Delphi code uses ICMP and SNMP to detect the dynamic IP address of your router when it is connected to a WAN.    It first gets the IP address of www.borland.com and then attempts to do a trace route to it. The trace route is only allowed to travel at most 1 hop. In a corporate environment, some changes are required in order to detect the router for the corporate environment.    Since the first hop is definitely the router's IP address, it assumes it has gotten the gateway IP address.    It then uses the SNMP protocol to discover the dynamic IP address that the router has been assigned with.    var
  GatewayIP, BorlandIP: string;
  DynamicIP, origOid: string;
  I: Integer;
  SNMP: TIdSNMP;
  ICMP: TIdIcmpClient;
begin
  SNMP := TIdSNMP.Create(nil);
  ICMP := TIdIcmpClient.Create(nil);
  try
  // Step 1: Get the Gateway IP address
  // Assumes the Gateway is only 1 connection away from the machine
    BorlandIP := GStack.ResolveHost('www.borland.com');
    ICMP.Host := BorlandIP;
    ICMP.TTL := 1;
    ICMP.ReceiveTimeout := 5000;
    ICMP.Ping;        GatewayIP := ICMP.ReplyStatus.FromIpAddress;      // Step 2: Query the gateway using SNMP
  // This assumes the gateway is the immediate connection to the Internet
  // or leads to a xDSL modem running in bridging mode that connects to
  // the Internet        SNMP.Community := 'public';
    SNMP.Host := GatewayIP;
    SNMP.Query.Clear;
    origOID := '1.3.6.1.2.1.4.20.1.1';
    SNMP.Query.MIBAdd(origOID, '');
    SNMP.Query.PDUType := PDUGetNextRequest;        while SNMP.SendQuery do
      begin
        if Copy(SNMP.Reply.MIBOID[0], 1, Length(origOID)) <> origOID then
          Break;      // For each result, check that it is not the Gateway IP
  // If it is not, assume that it is the Dynamic IP
  // If a gateway has multiple IP addresses assigned, the last
  // IP address will be considered to be the Dynamic IP
        for I := 0 to SNMP.Reply.ValueCount - 1 do
          if SNMP.Reply.Value[I] <> GatewayIP then
            DynamicIP := SNMP.Reply.Value[I];            SNMP.Query.Clear;
        SNMP.Query.MIBAdd(SNMP.Reply.ValueOID[0], '');
        SNMP.Query.PDUType := PDUGetNextRequest;
      end;
  finally
    ICMP.Free;
    SNMP.Free;
  end;
posted on Wednesday, January 28, 2004 12:10 AM     
Andy Chang
------
Andy Chang
系統時間:2024-04-29 6:55:58
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!