Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


RE: 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 >> RE: 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: <<   < prev  1 2 3 [4] 5   next >   >>
Login
Message << Older Topic   Newer Topic >>
 RE: Pull information from a file based on search criteria - 12/7/2006 7:43:15 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
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 = Machinea.company.com.
Comment = (null).
Type = DHCP
Expires = 12/05/2006 08:07:58.
Owner Host IP Address = 100.10.12.189.
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 =  Machineb.company.com.
Comment = (null).
Type = DHCP
Expires = 12/02/2006 10:51:13.
Owner Host IP Address = 100.10.12.189.
Owner Host NetBios Name = DHCPSERVER.
Owner Host Name = (null).

(in reply to ehvbs)
 
 
Post #: 61
 
 RE: Pull information from a file based on search criteria - 12/7/2006 8:22:58 AM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Sorry aburt, *posting* the data won't help me - if I copy it from the forum HTML,
all tabs are lost (and I don't know what else was changed). I know it's a lot to
ask, but: could you *send* it to me (attachment, email)?

In exchange, I promise you a revised netshrun.vbs that will parse your
original sample.

By the way: There are two spaces after the = in

    Name =  Machineb.company.com.

I say that not to keep the blame from me, but to show the factual difficulties
we have to cope with.

(in reply to aburt)
 
 
Post #: 62
 
 RE: Pull information from a file based on search criteria - 12/7/2006 8:31:07 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
No problem.  I know this has been a pain.  I have emailed you the text file.  There should be one space before equal and one space after the equal.

(in reply to ehvbs)
 
 
Post #: 63
 
 RE: Pull information from a file based on search criteria - 12/7/2006 9:02:34 AM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Thanks aburt, got it. While it look like the sample you posted, this is what
you get if you change spaces to ~ and tabs to §:


      

(in reply to aburt)
 
 
Post #: 64
 
 RE: Pull information from a file based on search criteria - 12/7/2006 9:26:22 AM   
  ehvbs

 

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

look what I got from seekc-02i.vbs:

cscript seekci-02.vbs
Got 744  chars form .\data\dhcpoutsam.txt
Found |ClientInfo| at Pos 85
065 69 i
066 65 e
067 6E n
068 74 t
069 20 .
070 63 c
071 6F o
072 75 u
073 6E n
074 74 t
075 20 .
076 3D =
077 20 .
078 32 2
079 37 7
080 33 3
081 2E .
082 20 .    no tab
083 20 .    still no tab
084 0A .    LF only
085 43 C
086 6C l
087 69 i
088 65 e
089 6E n
090 74 t
091 49 I
092 6E n
093 66 f
094 6F o
095 20 .
096 3A :
097 20 .     no tab
098 20 .     but a second blank
099 0A .     LF only
100 09 .     here comes the tab!
101 49 I
102 50 P
103 20 .
104 41 A
105 64 d
106 64 d
107 72 r
108 65 e
109 73 s
110 73 s
111 20 .
112 3D =
113 20 .
114 31 1
115 30 0

I think we'll have to change the parsing method; Split() is ok, if you can specify the delimiter
exactly, for moving targets (and I don't think we can garantee a fixed format for the whitespace)
you need regular expression. I'm working on this - but don't hold your breath.

(in reply to ehvbs)
 
 
Post #: 65
 
 RE: Pull information from a file based on search criteria - 12/7/2006 12:18:43 PM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Not quick & very dirty:


      

(in reply to ehvbs)
 
 
Post #: 66
 
 RE: Pull information from a file based on search criteria - 12/8/2006 3:10:46 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Ok Some progress is being made :) Got a few questions.

I ran the new script heres the output

ok, doTheRealWork() was called, but we aren't done yet!
nUB 2 Count 3 Clients 2
------------- 100.10.12.10 -------------
Not Found
Run this from the commandline and hope for the best
netsh dhcp server \\Server scope 100.10.9.1 add reservedip 100.10.9.189 000000000000 Machine Name nocomment
----------------------------------------
------------- 100.10.12.11 -------------
Found
Run this from the commandline and hope for the best
netsh dhcp server \\DHCPSERVER scope ?<ScopeID>? add reservedip 100.10.12.11 00-11-43-13-1f-f6 Machinea.company.com
----------------------------------------
------------- 100.10.12.12 -------------
Found
Run this from the commandline and hope for the best
netsh dhcp server \\DHCPSERVER scope ?<ScopeID>? add reservedip 100.10.12.12 00-0b-db-cb-b1-d9 Machineb.company.com
----------------------------------------
------------- 100.10.12.13 -------------
Not Found
Run this from the commandline and hope for the best
netsh dhcp server \\Server scope 100.10.9.1 add reservedip 100.10.9.189 000000000000 Machine Name nocomment
----------------------------------------
Exit code: 0 , 0000h


*******************************************************
But It does not create a dhcp reservation for any IP (found or not found.)

(in reply to ehvbs)
 
 
Post #: 67
 
 RE: Pull information from a file based on search criteria - 12/8/2006 4:51:54 AM   
  ehvbs

 

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

if you look at the code:

' ############################################################################
 ''# 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

you'll realize, that the

  netsh dhcp server \\Server scope 100.10.9.1 add reservedip 100.10.9.189 000000000000 Machine Name nocomment

is just echoed to screen. I sincerely advise to check the generated commands very carefully before you enable the
actual execution by

     If True Then

Did you spare a thought on possible error and how to handle them?

Good luck!

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

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Ehvbs,

Are u saying that if I rewrite the code to If True Then, It would then create the reservation.  How can i echo the netsh command line to make sure it is entered in correctly. 

As far as errors are concerned,  The complexity of this script is has gone beyond my current skill set.  Is it possible to output everything to a text file including errors?

(in reply to ehvbs)
 
 
Post #: 69
 
 RE: Pull information from a file based on search criteria - 12/8/2006 7:29:07 AM   
  ehvbs

 

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

If you replace "False" with "True" the code in the IF-THEN-Block will be executed. That is
not the same as "create the reservation" - the last is what we programmers hope.
Seriously: The script echos the command it would send to the .Exec() method to give
you the chance to check its validity.
If you are sure the 4 four commands echoed by a run on the short test file are correct
you can use copy/paste to execute them from the command line.
You could device reroute a run on your real data to file

      netshrun-02.vbs >log.tmp

and study that. I can't really help you there, because I know next to nothing about
DHCP server administration.

As to the error handling (in general and especially for the .Exec()) - I'll have to think
about that (i.e. need some time).

(in reply to aburt)
 
 
Post #: 70
 
 RE: Pull information from a file based on search criteria - 12/10/2006 3:35:43 PM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
hey it me again

I have been testing this script all weekend :( Trying to understand how it worksgetting much closer.)  Took your advice on copy paste for the netsh run command. 

problem:  for the not found address i need there reserved IP to be the IP address that is NOt found instead of gdicDefParms( "<ReservedIP>"    ) = "0.0.0.0"  


output should look like this
------------- 100.10.12.14 -------------
Not Found
Run this from the commandline and hope for the best
netsh dhcp server \\ scope 100.10.12.0 add reservedip 100.10.12.14 000000000000 Machine Name


I need to remove the dashes from the mac address on the Found IPs the netsh command line will error out if the dashes r included

Can you help out?

(in reply to ehvbs)
 
 
Post #: 71
 
 RE: Pull information from a file based on search criteria - 12/10/2006 8:47:54 PM   
  ehvbs

 

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

I changed the "not found" part of doTheRealWork() to


      

and got

------------- 100.10.12.13 -------------
Not Found
Run this from the commandline and hope for the best
netsh dhcp server \\Server scope 100.10.9.1 add reservedip 100.10.12.13 000000000000 Machine Name nocomment
----------------------------------------

(Sorry for being terse, being pressed for time just now)

(in reply to aburt)
 
 
Post #: 72
 
 RE: Pull information from a file based on search criteria - 12/11/2006 9:05:51 AM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
Ok I got a new problem the now the found IPs have dashes where they didnt have dashed before.  Also I have tested the run command with copy paste how can I get them to execute. 

(in reply to ehvbs)
 
 
Post #: 73
 
 RE: Pull information from a file based on search criteria - 12/11/2006 9:35:26 AM   
  ehvbs

 

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

nice to see/read from you again. As to the first problem:

If I run my netshrun-02.vbs on my sample file, I get

cscript netshrun-02.vbs
ok, doTheRealWork() was called, but we aren't done yet!
nUB 2 Count 3 Clients 2
------------- 100.10.12.10 -------------
Not Found
Run this from the commandline and hope for the best
netsh dhcp server \\Server scope 100.10.9.1 add reservedip 100.10.12.10 000000000000 Machine Name nocomment
----------------------------------------
------------- 100.10.12.11 -------------
Found
Run this from the commandline and hope for the best
netsh dhcp server \\DHCPSERVER scope ?<ScopeID>? add reservedip 100.10.12.11 00-11-43-13-1f-f6 Machinea.company.com
----------------------------------------

because my sample files contains:

ClientInfo : 
    IP Address = 100.10.12.11.
    SubnetMask = 255.255.254.0.
    Client Hardware Address = 00-11-43-13-1f-f6.
    Name = Machinea.company.com.

So:

  (a) double check your input file
  (b) if you can't guarantee a consistent format, use the Replace(..) strategy on the data red
        as we did on the default data

( I'm not complaining or chiding, but in my last post I wrote:

 '        we could/should remove dashes once for all at init/when reading it for the found IPs
 '        but doing it here keeps all changes at one place; you can do the Replace() wherever
 '        it's necessary
)

The "executing", well, yes. As I can't test it here, I really hesitate to say something like: "use
the script on a input file with just 1 ClientInfo; look once again at the output; change the
"False" we talked about to "True"; cross your fingers and run the script".

You'll have to do your own risk assessment, of course.

Good luck!

(in reply to aburt)
 
 
Post #: 74
 
 RE: Pull information from a file based on search criteria - 12/11/2006 2:41:06 PM   
  aburt

 

Posts: 104
Score: 0
Joined: 12/4/2006
Status: offline
hey ehvbs,

While waiting on your response I took a stab at fixing the dashes and added this line to the do the real work function
dicParms( "<MAC_Address>"   ) = Replace (dicClients( sIP )( "Client Hardware Address" ), "-", "" )

Seems to be working.  Thanks for not busting my chops for all repeated posts I feel really guilty that I been so needy on this one.

When I saw the first post I knew that it was over my head but I have been trying to work through it, so again thank you for carrying me. 

But, I have learned oh so much from trying to disect this script even if there are still a few parts I dont quite understand.

I am actually testing this on a test box with the sample text file.  It works. Your a genious.     

Ok forgive me.

Also I added the following where SomeScript Error.


if objProc.ExitCode <> 0 then
  WScript.Echo "EXIT CODE: " & objProc.ExitCode
  WScript.Echo "ERROR: " & objProc.StdErr.ReadAll
end if

How can I change the range of ips to look for.

Currently we have
Dim aIPs        : aIPs   = Array( "1", "2", "3", "4" )

But I need the range of 1 to 255.

In other words the script should run through a range of ips I specify.  ie 100.10.12.1 to 100.10.12.255

(in reply to ehvbs)
 
 
Post #: 75
 
 RE: Pull information from a file based on search criteria - 12/11/2006 7:47:55 PM   
  ehvbs

 

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

(1) Your enhancement to the error handling looks ok to me. I do like that you
    used a component/concept (WshScriptExec object) from the script to look up
    its method/properties and on your own applied that knowledge to a problem.
    Of course, every solution may be improved:
        
        Perhaps objProc.StdOut should be taken into consideration too
       
        plain .ReadAll is dangerous because it will fail on a file of size 0, so
        maybe it should be guarded by an .AtEndOfStream test (yes I know, I'm
        guilty of quick & dirty work "goFS.OpenTextFile( strDHCPFile ).ReadAll"
        too)
   
        Using .Exec or .Run is a frequent task. Just look at
           http://www.visualbasicscript.com/m_40768/tm.htm
        So having a reusable function or class would be desirable
       
    If you are interested (and have a (not so) little bit of free time) we
    could start a new topic ".Exec/.Run command". How do you feel about that?
   
(2) The