Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Pull information from a file based on search criteria

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40341
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Pull information from a file based on search criteria
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2 3 4 5   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Pull information from a file based on search criteria - 12/4/2006 9:35:28 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Since I am a scripting newbie I put this question out here for the pros.

I need to pull data from a text file based on a search criteria.  Not sure how to go about setting the script up.  

What I am trying to do is Loop through a list of IP addresses.  If the IP Address exist in the txt file pull the Mach Address and Machine name if it does not I want to assign that IP with a special Name and Mach Address.


Since I New Im sure it something simple.  I created the loop and the text file is a CSV file pulled from the DHCP Server.


So Far here is what I have


Dim arrTxtArray()
Dim myFile
Dim SearchString
Dim objTextFile
Dim strNextLine
Dim intSize
Dim objFSO
Dim J
intSize = 0
myFile = "d:\dhcpouts.txt"
SearchString = "IP"
Const ForReading = 1

'Loop through IPs
i = 0
 Do While i < 256
  IP = "100.10.9."& i
        i = i + 1
        WScript.Echo "IP Address " & IP
    Loop

   'Search List 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objTextFile = objFSO.OpenTextFile _
      (myFile, ForReading)
 
    Do Until objTextFile.AtEndOfStream
       strNextLine = objTextFile.Readline
      
        If InStr (strNextLine, SearchString) Then
            ReDim Preserve arrTxtArray(intSize)
            arrTxtArray(intSize) = strNextLine
            intSize = intSize +1
        End If
    Loop
 
 
objTextFile.Close

For i = LBound(arrTxtArray) To UBound(arrTxtArray)
   WScript.Echo arrTxtArray(J)
Next
WScript.Echo("all done")
 
 
 
Post #: 1
 
 RE: Pull information from a file based on search criteria - 12/4/2006 5:36:18 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
load your IP addresses into an array (you already have a loop for that) and use a for each to search thru the array of IP addresses on each readline

since you are parsing a csv file I would use the builtin split() function to parse the line out after you have a match

create another array for all your IPs that dont match

are you having a specific problem with your script? everything looks to be okay with it to me.

sorry if im not making much sense, its waaaay past my bedtime

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to aburt)
 
 
Post #: 2
 
 RE: Pull information from a file based on search criteria - 12/5/2006 2:43:51 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Yes, not sure how to go about doing that.

(in reply to kirrilian)
 
 
Post #: 3
 
 RE: Pull information from a file based on search criteria - 12/5/2006 2:49:59 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Could you show an example of dhcpout.txt? You can of course change the information for security reasons, I would just like to see the format.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to aburt)
 
 
Post #: 4
 
 RE: Pull information from a file based on search criteria - 12/5/2006 3:22:07 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Sure Thing

DHCP Server version 5.6 
Num Client info read = 573. 
Total Client count = 573. 
ClientInfo : 
IP Address = 100.10.12.11.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00.
Name = MachineNameA.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).
ClientInfo : 
IP Address = 100.10.12.12.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00.
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).

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Pull information from a file based on search criteria - 12/5/2006 3:37:08 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
"...the text file is a CSV file pulled from the DHCP Server."


The csv file is the one I need to see. That is the one you need to parse right?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to aburt)
 
 
Post #: 6
 
 RE: Pull information from a file based on search criteria - 12/5/2006 5:38:36 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Yes, csv file from the DHCP server is the one I need to parse

In Essence what I am trying to do is take a list of IPs (Thats the loop you see) if the IP exist on the DHCP Server then create a reservation using that IP the Netbios name and the mac address and if there is no matching IP in the CSV file create a DHCP Reservation with a special Netbios Name and Mach Address that I assign. 

Thanks

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: Pull information from a file based on search criteria - 12/5/2006 5:50:14 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi aburt,

a .CSV file (in at least one sense of the word) means a file of Comma Separated Values. So ebgreen
- and I - expected something like:

  100.10.12.11.,255.255.254.0.,00-00-00-00-00-00., ...
  100.10.12.11.,255.255.254.1.,00-00-00-00-00-00., ...
   ...

Perhaps you are using CSV iwith a different meaning?

(in reply to aburt)
 
 
Post #: 8
 
 RE: Pull information from a file based on search criteria - 12/5/2006 6:14:00 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Ok then lets say the data is like this:
IP Address = 100.10.12.11,SubnetMask = 255.255.254.0,Client Hardware Address = 00-00-00-00-00-00,Name = MachineNameA.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).

IP Address = 100.10.12.12,SubnetMask = 255.255.254.0,Client Hardware Address = 00-00-00-00-00-02,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).

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: Pull information from a file based on search criteria - 12/5/2006 6:36:10 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi aburt,

I'm sorry about causing a misunderstanding. I didn't want you to force you to change the format
of your dhcpouts.txt file. If the sample

DHCP Server version 5.6 
Num Client info read = 573. 
Total Client count = 573. 
ClientInfo : 
IP Address = 100.10.12.11.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00.
Name = MachineNameA.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).
ClientInfo : 
IP Address = 100.10.12.12.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00.
....

is representative of the file you want to work with, let's forget about CSV.

(in reply to aburt)
 
 
Post #: 10
 
 RE: Pull information from a file based on search criteria - 12/5/2006 6:58:11 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
no prob Just deperate for help will do it whatever way to get it working.

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: Pull information from a file based on search criteria - 12/5/2006 7:10:04 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
Well, there are two types of output that you have there ... the first one you proposed is more like an .ini file which you would read line by line with either a split() using " = " as a delimiter

The other type of file would be a .csv which would have

127.0.0.1,00:00:00:00:00:00,computername
127.0.0.2,00:00:00:00:00:00,computername

in which you would grab each line and use split() with "," as the delimiter (but not using any labels ... under the assumption that the IP goes in the same column in each line) I personally use the .csv file being as how it is much easier to pull into an array with minimal effort.

_____________________________

Thanks for Answering my Questions ... I hope my Answer to your questions help!

(in reply to aburt)
 
 
Post #: 12
 
 RE: Pull information from a file based on search criteria - 12/5/2006 7:14:20 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
If possible would like to use the format specified here.

DHCP Server version 5.6 
Num Client info read = 573. 
Total Client count = 573. 
ClientInfo : 
IP Address = 100.10.12.11.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00.
Name = MachineNameA.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).
ClientInfo : 
IP Address = 100.10.12.12.
SubnetMask = 255.255.254.0.
Client Hardware Address = 00-00-00-00-00-00

(in reply to CaffeineAddiction)
 
 
Post #: 13
 
 RE: Pull information from a file based on search criteria - 12/5/2006 7:27:22 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Here is some sample code using the first file format:

      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to aburt)
 
 
Post #: 14
 
 RE: Pull information from a file based on search criteria - 12/5/2006 7:48:37 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Thanks for your help

Im confused about something if a client (IP) is not in the cvs file where does is get assigned the Mac and Netbios name that I assign. 

(in reply to ebgreen)
 
 
Post #: 15
 
 RE: Pull information from a file based on search criteria - 12/5/2006 8:05:04 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
   If Not dicClients.Exists("100.10.9." & i) Then
      Set dicTemp = CreateObject("Scripting.Dictionary")
      dicTemp.Add "IP Address", "100.10.9." & i
      dicTemp.Add "Name", "Something.org.whatever.com"
      dicClients.Add dicTemp("IP Address"), dicTemp
  End If

this part would assign it to whatever you want.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to aburt)
 
 
Post #: 16
 
 RE: Pull information from a file based on search criteria - 12/5/2006 8:22:12 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Ok I do see that but,  I what I am trying to do is get the IPs that are in the text to use the  Mac Address and Machine name from the text file and if the IP does not exist use the one I assign.

The output for the script you gave me seems to assign all IPs in the text file a new Machine name and Mac address

100.10.9.0 = Something.org.whatever.com
100.10.9.1 = Something.org.whatever.com
100.10.9.2 = Something.org.whatever.com
100.10.9.3 = Something.org.whatever.com
100.10.9.4 = Something.org.whatever.com
100.10.9.5 = Something.org.whatever.com
100.10.9.6 = Something.org.whatever.com
100.10.9.7 = Something.org.whatever.com






I WAS PLANNING ON USING THIS TO CREATE THE RESERVATION.

' ------ SCRIPT CONFIGURATION ------
strScope = "100.10.9.1"
strReservedIP = "100.10.9.189"
strMAC = "000000000000"
strClientName = "Machine Name"
strServer = "Server" ' leave blank for local server
' ------ END CONFIGURATION ---------
strCommand = "netsh dhcp server " & strServer & " scope " & strScope & " add reservedip " & _
             strReservedIP & " " & strMAC & " " & strClientName & " """ & strClientComment & """"
' command: netsh dhcp server \\<ServerName> scope <ScopeID> add reservedip <ReservedIP> <MAC_Address> <ClientName> <ClientComment>
WScript.Echo "Running command: " & strCommand
WScript.Echo
set objShell = CreateObject("Wscript.Shell")
set objProc  = objShell.Exec(strCommand)
Do
  WScript.Sleep 100
Loop Until objProc.Status <> 0

(in reply to ebgreen)
 
 
Post #: 17
 
 RE: Pull information from a file based on search criteria - 12/5/2006 8:25:55 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Well, for one thing, the loop that you posted (and that I used for a guide) loops though ip address 10.100.9.0 to 10.100.9.255. In the text snippet that you provided, none of those IP addresses are present. So, if none are present, they will all be set to whatever you have set up as the default.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to aburt)
 
 
Post #: 18
 
 RE: Pull information from a file based on search criteria - 12/5/2006 8:40:43 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
I just want you to know I appreciate you hanging in here to help me out.

I have run the script with a text file that includes the IPs from the loop and still get the same results. 

let me clarify the loop contains all the ip that we need to create reservations for.

The text file contains current machines that need a reservation created but not all IPs in the loop hence the reason if the IP does not exist in the text file we need to assign it a Mac address and Netbios name.

Sorry If I am not explaining this correctly.


Thanks

(in reply to ebgreen)
 
 
Post #: 19
 
 RE: Pull information from a file based on search criteria - 12/5/2006 9:08:11 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi aburt,

I'm willing to try to help you; but I still have difficulties to understand your problem.
Are these assumptions correct:

   (1) The sample lines represent the fact that there clients with IP *.11 and *.12
   (2)  Seeking for *.10, *.11, *.12, *.13 should give results like:

      searching info from .\dhcpouts.txt from 10 to 13
      Looking for 100.10.12.10
      Not Found
      Looking for 100.10.12.11
      Found
      Looking for 100.10.12.12
      Found
      Looking for 100.10.12.13
      Not Found

   (3) Instead of echoing "not found" you want to execute a netsh command
 
Questions:

   (1) What sould be done instead of echoing "Found"?
   (2)  Will the netsh commands for *10 and *.13 the same? If not - how would you like to specify
          the different params?

(in reply to aburt)
 
 
Post #: 20
 
 
Page:   [1] 2 3 4 5   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Pull information from a file based on search criteria Page: [1] 2 3 4 5   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts