Jump to content

Script In Vbscript


Recommended Posts

Hey Guys,

I work in one of the company and I am trying write a code in vbscript to locate a destination using IP Address  from the datacenter. Any could help me to write a code please?

Link to comment
Share on other sites


Here is my code I am working on but I am not able to get valid output when running the program

<job><runtime> <description>This script searches the destination using IP addresses and dst_host         in the firewall databases.</description> <named name="run"  helpstring="Firewall to search" type="string" /> <named name="search"  helpstring="IP address to search for" type="string" />                helpstring="dst_host to search for" type="string"/> <example>fw-ip-search.wsf /search:"%136.165.238.%"</example> <example>fw-ip-search.wsf /run:internet /search:"%136.165.238.%"</example> <example>fw-dst_host-search.wsf/search:"%dmp-ll02-1%"</example> <example>fw-dst_host-search.wsf/run:internet / search:"%dmp-ll02-1%"</example></runtime><script id="fw-search" language="vbscript">' Declare the variables that we are using.Dim strMF, strProvider, strCpu, strCmd, strSearch, strTextX, strTextY, strWDDim arrSN, arrLine, dtmNow, dtmformat, objArgs, objSh, objFS, objMF, objOF, strFileConst DEFAULT_TEST_SWITCH = 0Const ForWriting = 2Const ForReading = 1'Specify file computers namestrFile = "c:\Scripts\Computers.txt"' Set some defaults.' Master firewall list file.' <firewall name>;<status>;<location/vlan>;<IP Block>strMF = "firewall_relationships.txt"' What database provider/library to use.  Jet is only 32bit 'strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"strProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"'strProvider = "Provider=Microsoft.ACE.OLEDB.14.0;"' Create a WshShell objectSet objSh = CreateObject("Wscript.Shell")' What Architecture are we running in x86, AMD64, or IA64?strCpu = objSh.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")If strCpu = "AMD64" And InStr(1,Wscript.FullName,"system32",1) > 0 Then strCmd = Replace(Wscript.FullName,"system32","SysWOW64",1,-1,1) strCmd = strCmd & " " & chr(34) & Wscript.ScriptFullName & chr(34) If Wscript.Arguments.Count > 0 Then  For i = 0 to Wscript.Arguments.Count - 1   strCmd = strCmd & " " & chr(34) & Wscript.Arguments(i) & chr(34)  Next End If objSh.Run strCmd, 0, FALSE Wscript.QuitEnd If' Get the current working directory that the script was ran from.strWD = objSh.CurrentDirectory & "\"' Get the current date and time into YYYYMMDDHHMMSS format.dtmNow = Nowdtmformat = ((((100*Year(dtmNow) + Month(dtmNow))*100 + Day(dtmNow))*100 _  + Hour(dtmNow))*100 + Minute(dtmNow))*100 + Second(dtmNow)' Allows the user to provide the search string on the command line.strSearch = "Please Enter the dst_host to search for:  " & chr(10) & _ " you can use % as a wildcard and [ ] to match any single char."Set objArgs = Wscript.Arguments.NamedIf objArgs.Exists("search") Then strTextX = objArgs.Item("search")Else strTextX = InputBox(strSearch,"dst_host Search") If strTextX = "" Then  wscript.Quit End IfEnd IfstrSearch = "Please Enter the IP address to search for:  " & chr(13) & _ " you can use % as a wildcard and [ ] to match any single char."Set objArgs = Wscript.Arguments.NamedIf objArgs.Exists("search") Then strTextY = objArgs.Item("search")Else strTextY = InputBox(strSearch,"IP Address Search") If strTextY = "" Then  wscript.Quit End IfEnd If' Create file system object to open files.Set objFS = CreateObject("Scripting.FileSystemObject")' Output file:  <script name>_YYYYMMDDHHMMSS.logarrSN = Split(Wscript.ScriptName,".")If (objFS.FileExists(strWD & arrSN(0) & "_" & dtmformat & ".log")) Then Set objOF = objFS.OpenTextFile(strWD & arrSN(0) & "_" & dtmformat & _ ".log", ForWriting, True)Else Set objOF = objFS.CreateTextFile(strWD & arrSN(0) & "_" & dtmformat & _ ".log")End If' Write out a header line.' To get a double quote to print out you have to escape it with another'  double quote.' Write out a header line.' To get a double quote to print out you have to escape it with another'  double quote.objOF.Write("""Firewall"",""destination dst_host"",""rule name"",""")objOF.WriteLine("protocol/services"",""source IP""")' Write out the search text.objOF.Write("""query"",""" & strTextX & """,""search text"",""")objOF.WriteLine(""",""" & strTextX & """")objOF.Write("""Firewall"",""destination IP"",""rule name"",""")objOF.WriteLine("protocol/services"",""source IP""")' Write out the search text.objOF.Write("""query"",""" & strTextY & """,""search text"",""")objOF.WriteLine(""",""" & strTextY & """")' This block allows the user to run this script for just one'  firewall database.'  cscript fw-ip-search.wsf /run:internetIf objArgs.Exists("run") Then SearchDB, objArgs.Item("run"),strTextX, strTextYElse ' Open a text file as read only if it exists. If (not (objFS.FileExists(strWD & strMF))) Then  Wscript.Echo "Cannot locate the Firewall Relationships file."  Wscript.Quit End If Set objMF = objFS.OpenTextFile(strWD & strMF, ForReading) ' Remove the header line in the file. objMF.SkipLine ' Reading though each line of the Master firewall list and take action on ' each firewall that is active. Do until objMF.AtEndOfStream  arrLine = Split(objMF.ReadLine)  ' If the second field is "on" then process that firewall.  If arrLine(1 and 2) = "on" Then   SearchDB, arrLine(0), strTextX, strTextY               'else If arrLine(2) = "on" Then   'SearchDB, arrLine(0), strTextX, strTextY                End If Loop'clean up objMF.closeEnd IfobjOF.closeSub SearchDB(strDBd, strBaseN, strSearchText) ' searches the dst_ip, src_ip and dst_host of a database. Dim objAccessDB, objRules Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adUseClient = 3 Const adCmdText = &H0001  ' Create objects to open a MS Access db. Set objAccessDB = CreateObject("ADODB.Connection") Set objRules = CreateObject("ADODB.Recordset") ' Open the MS Access db. objAccessDB.Open strProvider & "Data Source=" & strDBd & _  "fw_" & strBaseN & ".mdb" ' This is the statement that searches the database. objRules.Open "SELECT * FROM devices WHERE dst_ip Like '" & _  strSearchText & "' OR src_ip Like '" & _                strSearchText & "'OR dports Like '" &_  strSearchText & "'", _  objAccessDB, adOpenStatic, adLockOptimistic        ' If no rows were found exit the procdure. If objRules.RecordCount < 1 Then  Exit Sub End If objRules.MoveFirst Do Until objRules.EOF  If ((objRules.Fields.Item("dports") <> "")) Then  ' chr(34) is a double quote (")  objOF.Write(chr(34) & strBaseN & chr(34) & "," & chr(34) & _   objRules.Fields.Item("dst_ip") & chr(34) & "," & chr(34) & _   objRules.Fields.Item("dst_host") & chr(34) & "," & chr(34) & _   objRules.Fields.Item("dports") & chr(34) & "," & chr(34))  If IsNull(objRules.Fields.Item("src_ip")) Then   objOF.Write("any" & chr(34))  Else   objOF.Write(objRules.Fields.Item("src_ip") & chr(34))  End If  objOF.WriteLine  End If  objRules.MoveNext Loop objRules.Close objAccessDB.CloseEnd Sub</script></job>
Edited by Yzöwl
code moved into code box
Link to comment
Share on other sites

Perhaps this script from code project may help as well.

http://www.codeproject.com/Tips/191892/How-to-find-current-location-based-on-the-IP-addre

 

~DP

I would be interested to know what exactly it may help for. :unsure:

 

The OP has posted a (seemingly extremely complex :ph34r:) non working VBS script, with a very vague description of the intended goal, but that seemingly has nothing connected with geolocalization.

 It seems more like a lookup table (involving an Access database :w00t:) to use a specific route through a given firewall/connection, while the referenced Javascript is a (poor) script to show the provenance of the visitor on a web page, making use of a third-party resource that needs registration of the domain making use of the (now obsolete) service:

http://dev.maxmind.com/geoip/legacy/javascript/

 

@cldaha01

Your posts miss the "Standard Litany":

http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/problem-report-standard-litany.html

and - maybe - you are risking to slip on a chocolate covered banana:

http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/put-down-the-chocolate-covered-banana.html

 

Please post a description of your environment, what is your actual GOAL, etc., etc. this way willing helping member are more likely to suggest a valid solution or at least point you in the right direction.

 

jaclaz

Link to comment
Share on other sites

The reason I have not help is because it a WSH and using VBScript as the working engine. Plus there examples as to how the script should be used.

Example of usage from the script.

<example>fw-ip-search.wsf /search:"%136.165.238.%"</example><example>fw-ip-search.wsf /run:internet /search:"%136.165.238.%"</example><example>fw-dst_host-search.wsf/search:"%dmp-ll02-1%"</example><example>fw-dst_host-search.wsf/run:internet / search:"%dmp-ll02-1%"</example>
Link to comment
Share on other sites

Thanks Jaclaz for suggestions! :yes:

I am trying to access the firewall rules such as  destination ports and source Ip address  from the datacenter made in Microsoft access, given Ip address and destination host. I made one using only Ip address but its not accessing all information so I am trying to make one that inputs ip address and destination host and outputs  the information with dports and source IP address in text file.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...