Login | |
|
 |
RE: Pull information from a file based on search criteria - 12/7/2006 3:05:35 AM
|
|
 |
|
| |
aburt
Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
|
1) The code that you are running. ' ############################################################################ ''# netshrun.vbs : run netsh for a list of IPs ' [ If you start a new project, first try to describe the purpose (task) of the ' script in one short sentence; I don't know enough about DHCP and its use ' and about your environment to do this properly, so update the second line. ' If you can't just implement the purpose, start with a script that makes it ' easy to work/experiment with the different sub parts of your main problem. ' ] ' ============================================================================ ' (c) 2006 by YourCompany, Inc. ' 77 YourStreet YourTown Whatever ' T: (+49) 123 45 67 89 * E: YourName@YourCompany.com ' ============================================================================ ''# usage: cscript netshrun.vbs <action> ''# <action> : frstest - just show the 'call a func' part works ''# sectest - just show the 'call a func' part really works ''# bldCmd1 - first try to build netsh command ''# testCmd - building netsh command from hardcoded params and .Run ''# parseDHCP - parse DHCP output file into dictionary ''# cmdFound - netsh cmd for found IPs ' [ update this usage as you add more experimental functions ] ''# usage: cscript netshrun.vbs ??? ' [ update this usage when erverything works as it should; for now we can't, ' because the main problem isn't solved yet. ' ] ' ############################################################################ Option Explicit ' ############################################################################ ''# global vars ' [ will grow as we proceed ] ' ############################################################################ Dim gsCmd : gsCmd = _ "netsh dhcp server \\<ServerName> scope <ScopeID> add reservedip <ReservedIP> <MAC_Address> <ClientName> <ClientComment>" Dim gdicDefParms : Set gdicDefParms = CreateObject( "Scripting.Dictionary" ) Dim gsDelim : gsDelim = "ClientInfo : " & vbCrLf ' remember, remember the problem of trailing blanks! ' ############################################################################ ''# main - calls experimental func specified as first param on the command ' # or - the now still empty - 'doTheRealWork' func ' ############################################################################ WScript.Quit doMain() Function doMain() Dim nRVal : nRVal = 0 ' assume all is well Dim sFunc : sFunc = "doTheRealWork" ' ---------- init ----------- gdicDefParms( "<ServerName>" ) = "Server" ' taken from aburt's postings, gdicDefParms( "<ScopeID>" ) = "100.10.12.1" ' check carefully! I'm still gdicDefParms( "<ReservedIP>" ) = "100.10.9.189" ' afraid we need *different* gdicDefParms( "<MAC_Address>" ) = "000000000000" ' params for the missing IPs gdicDefParms( "<ClientName>" ) = "Machine Name" ' ??? gdicDefParms( "<ClientComment>" ) = "nocomment" ' ??? ' ---------- args ----------- If 1 <= WScript.Arguments.Count Then sFunc = WSCript.Arguments( 0 ) ' ---------- func ----------- Select Case LCase( sFunc ) Case "frstest" nRVal = frsTest() Case "sectest" nRVal = secTest() Case "bldcmd1" nRVal = bldCmd1() Case "testcmd" nRVal = testCmd() Case "parsedhcp" nRVal = parseDHCP() Case "cmdfound" nRVal = cmdFound() Case "dotherealwork" nRVal = doTheRealWork() End Select ' ---------- term ----------- doMain = nRVal End Function ' ############################################################################ ''# frsTest - just show the 'call a func' part works ' ############################################################################ Function frsTest() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, frsTest() was called." frsTest = nRVal End Function ' ############################################################################ ''# secTest - just show the 'call a func' part really works ' ############################################################################ Function secTest() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, secTest() was called." secTest = nRVal End Function ' ############################################################################ ''# bldCmd1 - first try to build netsh command ' ############################################################################ Function bldCmd1() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, bldCmd1() was called." bldCmd1 = nRVal End Function Function bldCmd1() Dim nRVal : nRVal = 0 ' assume ok ' Start with the command containing place holders; use copy/paste if possible Dim sCmd : sCmd = _ "netsh dhcp server \\<ServerName> scope <ScopeID> add reservedip <ReservedIP> <MAC_Address> <ClientName> <ClientComment>" ' Show what we have WScript.Echo "sCmd:", "|" + sCmd + "|" ' Get the place holder into an array: Dim aNames : aNames = cutRE00( "(<\w+>)", sCmd ) ' Show what we have WScript.Echo "aNames: (", Join( aNames, ", " ), ")" ' setup dictionary from aNames Dim dicParms : Set dicParms = CreateObject( "Scripting.Dictionary" ) Dim sKey For Each sKey In aNames dicParms( sKey ) = StrReverse( sKey ) ' just for testing Next ' Show what we have For Each sKey In dicParms.Keys WScript.Echo sKey, "=>", dicParms( sKey ) Next ' Apply dicParms to sCmd and prove applyRpl WScript.Echo "sCmd:", "|" + sCmd + "|" WScript.Echo "sCmd:", "|" + applyRpl( dicParms, sCmd ) + "|" ' for the lazy programmer: build an assignment block for dicParms For Each sKey In dicParms.Keys WScript.Echo "dicParms( """ + sKey + """ ) = """" " Next bldCmd1 = nRVal End Function ' ############################################################################ ''# testCmd - building netsh command from hardcoded params and .Run ' ############################################################################ Function testCmd() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, testCmd() was called." testCmd = nRVal End Function Function testCmd() Dim nRVal : nRVal = 0 ' assume ok WScript.Echo "ok, testCmd() was called." WScript.Echo "Using gdicDefParms" Dim sCmd Dim sRet sCmd = applyRpl( gdicDefParms, gsCmd ) sRet = runCmd( sCmd ) If "" = sRet Then WScript.Echo "ok" Else WScript.Echo "error" WScript.Echo sRet End If WScript.Echo "Using dicParms" Dim dicParms : Set dicParms = CreateObject( "Scripting.Dictionary" ) dicParms( "<ServerName>" ) = "your" dicParms( "<ScopeID>" ) = "suitable" dicParms( "<ReservedIP>" ) = "test" dicParms( "<MAC_Address>" ) = "values" dicParms( "<ClientName>" ) = "here" dicParms( "<ClientComment>" ) = "please" sCmd = applyRpl( dicParms, gsCmd ) sRet = runCmd( sCmd ) testCmd = nRVal End Function ' ############################################################################ ''# parseDHCP - parse DHCP output file into dictionary ' ############################################################################ Function parseDHCP() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, parseDHCP() was called." parseDHCP = nRVal End Function Function parseDHCP() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, parseDHCP() was called." Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") Dim strDHCPFile : strDHCPFile = "d:\DHCPOuts.txt" 'Dim sDelim : sDelim = "ClientInfo :" & VbCrLf ' maybe: "ClientInfo : " (trailing blank) Dim arrEntries, nIdx arrEntries = Split( oFSO.OpenTextFile( strDHCPFile ).ReadAll(), gsDelim ) ' Skip header! For nIdx = 1 To UBound( arrEntries ) ' The entry is still just a block of text so break it into lines for processing Dim arrLines : arrLines = Split( arrEntries( nIdx ), vbCrLf ) WScript.Echo arrLines( 0 ) Next parseDHCP = nRVal End Function Function parseDHCP() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, parseDHCP() was called." Dim dicClients : Set dicClients = CreateObject( "Scripting.Dictionary" ) Dim oFSO : Set oFSO = CreateObject( "Scripting.FileSystemObject" ) Dim strDHCPFile : strDHCPFile = "d:\DHCPOuts.txt" 'Dim sDelim : sDelim = vbCrLf + "ClientInfo :" & vbCrLf ' maybe: "ClientInfo : " (trailing blank) Dim bVerbose : bVerbose = True ' set to False for big strDHCPFile Dim arrEntries, nIdx arrEntries = Split( oFSO.OpenTextFile( strDHCPFile ).ReadAll(), gsDelim ) ' Skip header! For nIdx = 1 To UBound( arrEntries ) ' The entry is still just a block of text so break it into lines for processing Dim arrLines : arrLines = Split( arrEntries( nIdx ), vbCrLf ) WScript.Echo arrLines( 0 ) Dim dicTemp : Set dicTemp = CreateObject( "Scripting.Dictionary" ) Dim strLine For Each strLine In arrLines ' Clean up strLine = Trim( strLine ) If "" < strLine Then If "." = Right( strLine, 1 ) Then strLine = Left( strLine, Len( strLine ) - 1 ) ' Split each line into a parameter and a value and add those to a temporary dictionary Dim arrParts : arrParts = Split( strLine, " = ") If 1 = UBound( arrParts ) Then dicTemp.Add Trim( arrParts( 0 ) ), Trim( arrParts( 1 ) ) Else WScript.Echo "??? |" + strLine + "|" End If Else ' no need to process empty lines End If Next ' Now add that entry to the client dictionary with the IP address as the key Dim sIP : sIP = dicTemp( "IP Address" ) WScript.Echo "Handling IP: |" + sIP + "|" If dicTemp.Exists( "IP Address" ) Then If dicClients.Exists( sIP ) Then WScript.Echo "??? - double IP: " + sIP Else dicClients.Add sIP, dicTemp End If Else WScript.Echo "??? - no IP Address found." End If Next ' Now we have a dictionary of all the clients reported in the file ' Dump the info to gain confidence If bVerbose Then Dim sKeyClient, dicParams, sKeyParam For Each sKeyClient In dicClients WScript.Echo "-------------", sKeyClient, "-------------" Set dicParams = dicClients( sKeyClient ) For Each sKeyParam In dicParams WScript.Echo "", sKeyParam, "|" + dicParams( sKeyParam ) + "|" Next Next End If parseDHCP = nRVal End Function ' ############################################################################ ''# cmdFound - netsh cmd for found IPs ' ############################################################################ Function cmdFound() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, cmdFound() was called." Dim strDHCPFile : strDHCPFile = "d:\DHCPOuts.txt" Dim dicClients : Set dicClients = parseDHCPebgreen( strDHCPFile ) Dim sKeyClient, dicParams For Each sKeyClient In dicClients WScript.Echo "-------------", sKeyClient, "-------------" Next cmdFound = nRVal End Function Function cmdFound() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, cmdFound() was called." Dim strDHCPFile : strDHCPFile = "d:\DHCPOuts.txt" Dim dicClients : Set dicClients = parseDHCPebgreen( strDHCPFile ) ' stolen from testCmd() Dim dicParms : Set dicParms = CreateObject( "Scripting.Dictionary" ) Dim sCmd, sRet Dim sKeyClient, dicParams For Each sKeyClient In dicClients WScript.Echo "-------------", sKeyClient, "-------------" ' copied from dox box ' IP Address |100.10.12.12| ' SubnetMask |255.255.254.0| ' Client Hardware Address |12-12-12-12-12-12| ' Name |MachineNameb.org.company.com| ' Comment |(null)| ' Type |DHCP| ' Expires |12/05/2006 08:07:58| ' Owner Host IP Address |100.10.12.11| ' Owner Host NetBios Name |Server| ' Owner Host Name |(null)| ' stolen from testCmd() dicParms( "<ServerName>" ) = dicClients( sKeyClient )( "Owner Host NetBios Name" ) dicParms( "<ScopeID>" ) = "?<ScopeID>?" dicParms( "<ReservedIP>" ) = dicClients( sKeyClient )( "IP Address" ) dicParms( "<MAC_Address>" ) = dicClients( sKeyClient )( "Client Hardware Address" ) dicParms( "<ClientName>" ) = dicClients( sKeyClient )( "Name" ) dicParms( "<ClientComment>" ) = "" sCmd = applyRpl( dicParms, gsCmd ) sRet = runCmd( sCmd ) WScript.Echo "-------------", sKeyClient, "-------------" Next cmdFound = nRVal End Function ' ############################################################################ ''# doTheRealWork - not known yet ' ############################################################################ Function doTheRealWork() Dim nRVal : nRVal = 0 ' assume ok WScript.Echo "ok, doTheRealWork() was called, but we aren't done yet!" doTheRealWork = nRVal End Function Function doTheRealWork() Dim nRVal : nRVal = 0 ' assume ok WScript.Echo "ok, doTheRealWork() was called, but we aren't done yet!" ' stolen from Function cmdFound() Dim strDHCPFile : strDHCPFile = "d:\DHCPOuts.txt" Dim dicClients : Set dicClients = parseDHCPebgreen( strDHCPFile ) Dim dicParms : Set dicParms = CreateObject( "Scripting.Dictionary" ) Dim sCmd, sRet Dim sKeyClient, dicParams ' first try to specify IPs to look for Dim aIPs : aIPs = Array( "10", "11", "12", "13" ) ' that's my problem: sIPPfx/Scope/Netmask Dim sIPPfx : sIPPfx = "100.10.12." Dim sIP For Each sIP In aIPs sIP = sIPPfx + sIP WScript.Echo "-------------", sIP, "-------------" If dicClients.Exists( sIP ) Then WScript.Echo "Found" ' stolen from cmdFound() dicParms( "<ServerName>" ) = dicClients( sIP )( "Owner Host NetBios Name" ) dicParms( "<ScopeID>" ) = "?<ScopeID>?" dicParms( "<ReservedIP>" ) = dicClients( sIP )( "IP Address" ) dicParms( "<MAC_Address>" ) = dicClients( sIP )( "Client Hardware Address" ) dicParms( "<ClientName>" ) = dicClients( sIP )( "Name" ) dicParms( "<ClientComment>" ) = "" Else WScript.Echo "Not Found" Dim sKey For Each sKey In gdicDefParms dicParms( sKey ) = gdicDefParms( sKey ) Next End If sCmd = applyRpl( dicParms, gsCmd ) sRet = runCmd( sCmd ) WScript.Echo "----------------------------------------" Next doTheRealWork = nRVal End Function ' ############################################################################ ''# runCmd - WShell.Exec sCmd (just echo the command for now; use copy/paste ''# to run the command manually; fix all errors (gsCmd, gdicDefParms); ''# then enable actual call ' ############################################################################ Function runCmd( sCmd ) Dim sRVal : sRVal = "" ' assume ok WScript.Echo "Run this from the commandline and hope for the best" WScript.Echo sCmd If False Then ' fragment of aburt's code Dim objShell : Set objShell = CreateObject("Wscript.Shell") Dim objProc : Set objProc = objShell.Exec(strCommand) Do WScript.Sleep 100 Loop Until objProc.Status <> 0 If SomeError() Then sRVal = "suitable message" End If runCmd = sRVal End Function ' ############################################################################ ''# cutRE00 - Q & Dirty: all matches.value into 1 dim array ' ############################################################################ Function cutRE00( sPattern, sText ) Dim aRVal : aRVal = Array() Dim oRE : Set oRE = New RegExp Dim oMTS, nIdx oRE.Pattern = sPattern oRE.Global = True Set oMTS = oRE.Execute( sText ) If 0 < oMTS.Count Then ReDim aRVal( oMTS.Count - 1 ) For nIdx = 0 To UBound( aRVal ) aRVal( nIdx ) = oMTS( nIdx ).Value Next End If cutRE00 = aRVal End Function ' ############################################################################ ''# applyRpl - replace placeholders in sText with values from dicParms ' ############################################################################ Function applyRpl( dicParms, ByVal sText ) Dim sKey For Each sKey In dicParms sText = Replace( sText, sKey, dicParms( sKey ) ) Next applyRpl = sText End Function ' ############################################################################ ''# parseDHCPebgreen - parse strDHCPFile into a dictionary ' ############################################################################ Function parseDHCPebgreen( strDHCPFile ) Dim dicClients : Set dicClients = CreateObject( "Scripting.Dictionary" ) Dim oFSO : Set oFSO = CreateObject( "Scripting.FileSystemObject" ) 'Dim sDelim : sDelim = vbCrLf + "ClientInfo :" & vbCrLf ' maybe: "ClientInfo : " (trailing blank) Dim arrEntries, nIdx arrEntries = Split( oFSO.OpenTextFile( strDHCPFile ).ReadAll(), gsDelim ) ' Skip header! For nIdx = 1 To UBound( arrEntries ) ' The entry is still just a block of text so break it into lines for processing Dim arrLines : arrLines = Split( arrEntries( nIdx ), vbCrLf ) Dim dicTemp : Set dicTemp = CreateObject( "Scripting.Dictionary" ) Dim strLine For Each strLine In arrLines ' Clean up strLine = Trim( strLine ) If "" < strLine Then If "." = Right( strLine, 1 ) Then strLine = Left( strLine, Len( strLine ) - 1 ) ' Split each line into a parameter and a value and add those to a temporary dictionary Dim arrParts : arrParts = Split( strLine, " = ") If 1 = UBound( arrParts ) Then dicTemp.Add Trim( arrParts( 0 ) ), Trim( arrParts( 1 ) ) Else ' Add (better) error handling here Err.Raise 4711, "??? |" + strLine + "|" End If Else ' no need to process empty lines End If Next ' Now add that entry to the client dictionary with the IP address as the key Dim sIP : sIP = dicTemp( "IP Address" ) If dicTemp.Exists( "IP Address" ) Then If dicClients.Exists( sIP ) Then ' Add (better) error handling here Err.Raise 4711, "??? - double IP: " + sIP Else dicClients.Add sIP, dicTemp End If Else ' Add (better) error handling here Err.Raise 4711, "??? - no IP Address found." End If Next ' Now we have a dictionary of all the clients reported in the file ' return this to caller; remember the Set!! Set parseDHCPebgreen = dicClients End Function 2) The command that you run. cscript netshrun.vbs parsedhcp 3) The result that you get ok, parseDHCP() was called. 4) The data file being used. This is the sample text DHCPouts.txt DHCP Server version 5.6 Num Client info read = 273. Total Client count = 273. ClientInfo : IP Address = 100.10.12.11. SubnetMask = 255.255.254.0. Client Hardware Address = 00-11-43-13-1f-f6. Name = DT-82429.company.org. Comment = (null). Type = DHCP Expires = 12/05/2006 08:07:58. Owner Host IP Address = 100.10.9.1. Owner Host NetBios Name = DHCPServer. Owner Host Name = (null). ClientInfo : IP Address = 100.10.12.12. SubnetMask = 255.255.254.0. Client Hardware Address = 00-0b-db-cb-b1-d9. Name = dt-81214.company.org. Comment = (null). Type = DHCP Expires = 12/02/2006 10:51:13. Owner Host IP Address = 100.10.9.4. Owner Host NetBios Name = DHCPServer. Owner Host Name = (null).
|
|
| |
|
|
|
|