撈取 DNS 紀錄寫到資料庫

最近因任務需要在網域內把 IP 轉成電腦名稱,通常這個可以利用 nslookup 指令來達成,但是需要由 Excel 報表內呈現就有點難度。

 

所以我想出來的解決方案是去網域內的 DNS 控制器去撈所有的紀錄,然後把它寫回到資料庫內,這樣在做 SQL Query 時就可以很快速的撈取出來,靠先前開發的 VBScript 來做改良。

 

主要的抓取 DNS 紀錄的原始碼還是由 http://gallery.technet.microsoft.com/scriptcenter/8ebcaa52-f5c0-4fc1-99fe-d1c8798fddf4 微軟的網站找到的,我只是修正抓到資料後回寫到資料庫去。

首先建立一個很簡單的資料表 (table),可以參考底下的指令來產生

 

CREATE TABLE “YourSchemaName”.”IP_COMPNAME”

  (

    “IP_ADDR”   VARCHAR2(16 BYTE) NOT NULL ENABLE,

    “COMP_NAME” VARCHAR2(50 BYTE) NOT NULL ENABLE,

    “FQDN”      VARCHAR2(100 BYTE),

    CONSTRAINT “IP_COMPNAME_PK” PRIMARY KEY (“IP_ADDR”, “COMP_NAME”) USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE “WEBAP_TBS” ENABLE

  )

  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE

  (

    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT

  )

  TABLESPACE ” YourSchemaTableSpace” ;

 

再把底下這一段 VB Script 貼到檔案去, 命名成 GetDNS.vbs

‘for standard query

const cnstCommand = 1

 

Dim IPAdd

Dim Computer

strComputer = “YourDNSServer”

 

‘ 設定連線字串

connectionString = “DRIVER={Microsoft ODBC for Oracle};SERVER=Oracle;User Id=yourschemaName;Password=YourPassword;”

Set connection = CreateObject(“ADODB.Connection”)

Set theCommand = CreateObject(“ADODB.Command”)

connection.Open connectionString

 

 

 

Set objWMIService = GetObject(“winmgmts:” & strComputer & “rootMicrosoftDNS”)

Set colItems = objWMIService.ExecQuery( “SELECT * FROM MicrosoftDNS_PTRType”,,48)

For Each objItem in colItems

 

    CompNameArray = Split(objItem.RecordData ,  “.”)

    For i = LBound(CompNameArray) to UBound(CompNameArray)

    Computer = CompNameArray(0)

    Next

 

 

    IPAddArray = Split (objItem.OwnerName , “.”)

    

    For i = LBound(IPAddArray) to UBound(IPAddArray)

    IPAdd = IPAddArray(3) & “.” & IPAddArray(2) & “.” & IPAddArray(1) & “.” & IPAddArray(0)

    Next

   

               

‘ 清除掉先前記錄的資料

               

        cmdStr = “DELETE FROM WEBAP.IP_COMPNAME WHERE IP_ADDR = ‘” & IPAdd & “‘”

        WScript.Echo “Clear ” & IPAdd &”‘s DNS Record”

        thecommand.CommandText = CmdStr

        theCommand.ActiveConnection = connection

        theCommand.Execute

               

      ‘ 製作 SQL Command

        CmdStr = “INSERT INTO WEBAP.IP_COMPNAME (IP_ADDR, COMP_NAME, FQDN) VALUES (‘”

        CmdStr = CmdStr & IPADD & “‘, ‘” & Computer & “‘, ‘”

        CmdStr = CmdStr & objItem.RecordData & “‘) “

               

 

        ‘ 執行 SQL 指令

    ‘WScript.Echo cmdstr

        thecommand.CommandText = CmdStr

        thecommand.CommandType = cnstCommand

        thecommand.ActiveConnection = connection

        thecommand.Execute            

               

 

Next

 

Set Connection = nothing

set thecommand = nothing

 

 

 

Set objFSO = Nothing

Set objFile = Nothing

Set objWMIService = Nothing

Set colItems = Nothing

 

 

需要執行他時,只要在 DOS BOX 打入 CScript GetDNS.vbs 即可,執行時間隨DNS紀錄的多寡有所不同,應該在幾分鐘之內就可以跑完

 

發表迴響

%d 位部落客按了讚: