Over the past few days I've found myself delving back into VBScript a bit, at the request of one of my co-workers. I'm by no means an expert at vbs, but I can work my way around it pretty well and accomplish most tasks.
My co-worker has a pretty mundane task that he has to perform fairly frequently, as do most of us. He's the network guru on our team and has to periodically review all of the ACL's on our routers. While reviewing them he must correlate each host IP with an asset in our asset management database or note that the host IP is not listed in the AM-DB. He came to me a few weeks ago and asked if I could script that for him, hence my recent forray back into vbs. :)
The philosophy behind the script wasn't too complex... Read the input file line-by-line, see if the current line contains the string "host IP#", if it does then query the AM-DB for the host info & insert that into the output file along with the current line, if not then just insert the current line as-is into the output file. The end result is a new ACL that has in-line comments for each host IP listed.
To demonstrate that I'm far from an expert at vbs, I'll openly admit that this is the first time I've used regular expressions in any of my scripts.
But damn, if used correctly, they're freakin' cool!
So, here's a quick how2 for using RegExp in VBScript to query for an IP number in a string:
-
dim objRegEx, strInput, strIP, Match, Matches
-
strInput = "Did you know that the publicly registered IP for MSN is 207.68.172.246?"
-
Set objRegEx = New RegExp
-
objRegEx.Global = True
-
objRegEx.IgnoreCase = True
-
objRegEx.Pattern = "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
-
Set Matches = objRegEx.Execute(LCase(strInput))
-
For Each Match in Matches
-
strIP = Replace(LCase(Match.value), "host ", "")
-
WScript.Echo("The IP is: " & strIP)
-
Next
One of the hardest things to do with regular expressions, is to figure out the syntax of the Pattern you're looking for. There are a few sites that deal with nothing but figuring out regular expressions (regular-expressions.info, RegExLib.com, etc.). To help understand it a little better, let's breakdown line 6 above:
- [0-9] - this looks for any digit between 0 and 9.
- {1,3} - this tells it that there must be a minimum of 1 digit and a max of 3 digits that meet the requirements set in the [ ].
- . - looks for a period after the set of 1 to 3 digits
- [0-9]{1,3} - looks for the next set of 1 to 3 digits
- . - looks for a period after the set of 1 to 3 digits
- and so on...
I also used RegExp to check for the correct input when starting the script. For instance, my script can only read from text files (.txt) so I added the following function to verify this before trying anything else:
-
' Verifies that the input file ends with ".txt" or else echoes help info
-
Function verifyInput(strInput)
-
Set objRegEx = New RegExp
-
objRegEx.Global = True
-
objRegEx.IgnoreCase = True
-
objRegEx.Pattern = ".txt$"
-
Dim retVal
-
retVal = objRegEx.Test(strInput)
-
If retVal Then
-
' .txt was found - proceed with script
-
Else
-
WScript.Echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
-
WScript.Echo "Help & syntax information goes here."
-
WScript.Echo ()
-
WScript.Echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~"
-
WScript.Echo ()
-
WScript.Quit
-
End If
-
End Function
Well, I hope that helps someone out there in the great big www. 
A few resources: