|
2010/04/03 22:46
□ DHCP インストール
「スタート」→「管理ツール」→「サーバーマネージャ」を起動し、
「役割フォルダを」選択し、「役割の追加」を選択する
「DHCP サーバー」にチェックを入れて、「次へ」をクリックする。
├ [次へ] → 「優先 DNSサーバのIPv4アドレス」を確認して、 [次へ]
├ [次へ] → [次へ]
└ [次へ] → [次へ] → [次へ] → 「インストール」 → 「閉じる」
上記、手順だけDHCPのスコープ設定は自動登録スクリプトを作成しました。
動作として、リストを作成しておきスクリプトから読み込ませ自動登録させます。
*** DHCPScopeAdd.vbs ***
実行方法
DHCPScopeAdd.vbs デキストファイル名
Option Explicit
'定数の宣言
Const ForReading = 1 '読み込み
Const ForWriting = 2 '書きこみ(上書きモード)
Const ForAppending = 8 '書きこみ(追記モード)
Const DHCP01 = IPアドレス
Dim objParm, objFS, objText
'ログ取得
Set objFS = CreateObject("Scripting.FileSystemObject")
objFS.CreateTextFile("c:\AddScopeResult.txt")
Set objText = objFS.OpenTextFile("c:\AddScopeResult.txt", 8)
objText.WriteLine Time & " 処理開始"
Set objParm = Wscript.Arguments
ObjCheck(objParm)
Call prcTextFileRead
'オブジェクト破棄
Set objParm = Nothing
Wscript.Echo "DHCPの登録が終了しました"
'これ以降はCallなどで呼び出さない限り実行しない。
'** コマンドラインからの入力が正しいか確認
'**************************************************
Function ObjCheck(objParm)
'取得したパラメタが1コ未満のときはエラー
If objParm.Count < 1 Then
WScript.echo "パラメタが足りません"
wscript.quit
'取得したパラメタが1コより多いときもエラー
ElseIf objParm.Count > 1 Then
WScript.echo "パラメタが多いです"
wscript.quit
End If
End Function
'** 読み込んだレコードをDHCPに登録
'**************************************************
Sub prcTextFileRead()
'On Error Resume Next
Dim objFileSys
Dim objInFile
Dim strScriptPath
Dim strFileName
Dim strFilePath
Dim strRecord
Dim scopeName
Dim sIP,sMask,startIP,endIP,defIP,NstartIP,NendIP
Dim wsh
Dim aryStrings
Dim set1, set2, set3, set4, set5
strFileName = objParm(0)
strScriptPath = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
strFilePath = objFileSys.BuildPath(strScriptPath,strFileName)
Set objInFile = objFileSys.OpenTextFile(strFilePath,ForReading)
'ファイルのオープンが成功(ファイル有り)のときはErr.Numberが0
If Err.Number=0 Then
Set wsh = WScript.CreateObject("WScript.Shell")
'ファイルの内容を全部読み終えるまでループ
Do Until objInFile.AtEndOfStream = true
strRecord = objInFile.ReadLine
aryStrings = Split(strRecord, vbTab)
scopeName = aryStrings(0)
sIP = aryStrings(1) 'IPアドレス
sMask = aryStrings(2) 'サブネットマスク
startIP = aryStrings(3) '開始IPアドレス
endIP = aryStrings(4) '終了IPアドレス
defIP = aryStrings(5) 'デフォルトゲートウェイ
NstartIP = aryStrings(6) '除外開始IPアドレス
NendIP = aryStrings(7) '除外終了IPアドレス
objText.WriteLine Time & " " & scopeName &" スコープを登録します。"
'netsh dhcp server "DHCPサーバのIP" add scope "スコープIPアドレス" "サブネットマスク" "スコープ名" "説明"
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " add scope " & sIP & " " & sMask & " " & scopeName & " GripsScope",0,False
Wscript.Sleep 1000
'netsh dhcp server "DHCPサーバのIP" scope "スコープIPアドレス" add iprange "開始IP" "終了IP"
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " scope " & sIP & " add iprange " & startIP & " " & endIP,0,False
Wscript.Sleep 1000
'除外アドレス域を設定
If NstartIP <> "n" Then
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " scope " & sIP & " add excluderange " & NstartIP & " " & NendIP,0,False
Wscript.Sleep 1000
Else
objText.WriteLine "除外なし"
End If
'デフォゲ登録
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " scope " & sIP & " set optionvalue 003 IPADDRESS "& defIP,0,False
Wscript.Sleep 1000
'リース時間登録 43200秒(12時間)
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " scope " & sIP & " set optionvalue 051 DWORD 43200",0,False
Wscript.Sleep 1000
'登録したスコープをActiveにする。
wsh.Run "cmd /c netsh dhcp server " & DHCP01 & " scope " & sIP & " set state 1",0,False
Wscript.Sleep 2000
objText.WriteLine Time & " " & scopeName &" スコープを登録しました。"
Loop
Set wsh = Nothing
objText.WriteLine Time & " 処理終了"
objText.Close
objInFile.Close
Else
Wscript.Echo strScriptPath & "にファイル" & strFileName & "がありませんでした。"
end if
Set objFileSys = Nothing
Set objInFile = Nothing
End Sub
Function GetIPFromNSLookup(hostname, ipadress)
Dim WshShell
Dim Pipe
Dim line
Dim aryStrings
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Pipe = WshShell.Exec("nslookup " & hostname)
GetIPFromNSLookup = True
Do Until Pipe.StdOut.AtEndOfStream
'On Error Resume Next
line = Pipe.StdOut.ReadLine()
line = Replace(line , " "," ")
aryStrings = Split(line, " ")
If IsArray(aryStrings) Then
If UBound(AryStrings) > 0 Then
If aryStrings(1) = ipadress Then
WScript.Echo aryStrings(1) & " " & ipadress
WScript.Echo "False"
GetIPFromNSLookup = False
End If
End If
End If
'On Error Goto 0
Loop
Set Pipe = Nothing
Set WshShell = Nothing
End Function
*** list.txt ***
例
スコープ名 ネットワーク サブネットワーク 開始IPアドレス 終了IPアドレス デフォルトゲートウェイ 除外開始IP 除外終了IP
Scope-10.10.10.0/24 10.10.10.0 255.255.255.0 10.10.10.2 10.10.10.10 10.10.10.1 n n
Scope-10.1.4.0/22 10.1.4.0 255.255.252.0 10.1.4.10 10.1.5.229 10.1.4.1 10.1.4.230 10.1.5.9
|