InspectURL

M. Gallant 11/07/2000

The ability to create objects from classes in the Java API can be used to extend the capability of Windows scripts. Since the MS Java API is resident on any PC with IE4+ installed, developers can leverage the capability contained therein.
The following script instantiates a Java object which contains a method:
InspectUrl.rawUrlToFile(String urlspec, String path, boolean writerawurl)

which analyzes a web-page url and generates a file with the results. Optionally, the raw URL content is written to a file. To use the Java class, compile the Java source code, and place the resultant InspectUrl.class class file in the classpath (for example: C:\Winnt\Java\Classes)

inspecturl.vbs

'********************************************************************************** ' File: inspecturl.vbs (WSH for VBscript) ' Author: (c) Michel I. Gallant 11/07/2000 ' ' Retrieves web-based url content and analyzes for tags. ' Requires Java class "InspectURL" on classpath. ' Calls Java method InspectURL.rawUrlToFile(String url, String scriptpath, boolean writerawurl) ' ' Writes text file "$$inspecturl.txt" to current script directory. ' Optionally writes "$$rawurl.txt" to current script directory. ' Opens $$inspecturl.txt using notepad.exe. '*********************************************************************************** Option Explicit Const ttext = "InspectURL -- M. Gallant" Const genrawurlfile = true Dim WshShell, fso, oJinspect, ptext DIM url, scriptpath, inspectresult, again ptext = "InspectURL: Captures web-page html content, " & _ "numbers lines and analyzes content for tags." & vbCrLf & _ "Generates file $$inspecturl.txt in script directory and opens with notepad.exe." & vbCrLf & _ "Optionally generates file $$rawurl.txt in script directory." & _ vbCrLf & vbCrLf & "Enter a valid URL:" set WshShell = WScript.CreateObject("WScript.Shell") set oJinspect = GetObject("java:InspectUrl") 'Instantiate Java object via moniker. Set fso = CreateObject("Scripting.FileSystemObject") scriptpath = fso.GetParentFolderName(WSCript.ScriptFullName) & "\" Do While True url = InputBox(ptext, ttext) If url="" then WScript.Quit url = Trim(url) If NOT Instr(url, "http://")<>0 Then ' prepend http:// if not supplied with url. url = "http://" & url End If inspectresult = oJinspect.rawUrlToFile(url, scriptpath, genrawurlfile) ' call the Java method. If Instr(inspectresult, "successful")<>0 Then ' Is Java call successful, display with notepad. WshShell.Run "notepad " & scriptpath & "$$inspecturl.txt", 1, true End If again = WshShell.Popup(inspectresult & vbCrLf & vbCrLf & "Continue?",0, _ ttext, vbInformation + vbYesNo) If again = vbNo Then Exit Do Loop '------------------------------- End Script ---------------------------------------------

InspectUrl.java (Java class)

/* class InspectUrl (1) Reads url into ByteArrayOutputStream (raw as received data) (2) Writes ByteArrayOutputStream -->ByteArrayInputStream (3) Optionally writes raw url to file "$$rawurl.txt" (4) Reads and analyzes url content; adds line numbers and stats on certain strings (malstrings). (5) Writes analyzed url (adjusted EOL characters) to file "$$inspecturl.txt" M. Gallant 11/07/2000 */ import java.io.*; import java.net.*; import java.util.Date; import java.text.DateFormat; public final class InspectUrl { static final String newline = System.getProperty("line.separator") ; private static final String malstrings [] = { "<script", "<applet", "<embeds", "<object", "classid", "clsid", "window.location", "location.replace", "http-equiv=\"refresh\"", ".hta", ".vbs", "settimeout(" } ; private static StringBuffer malstats [] = new StringBuffer[malstrings.length] ; public InspectUrl() { // constructor } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java InspectUrl <url> "); return; } InspectUrl inurl = new InspectUrl() ; // instantiate System.out.println(inurl.rawUrlToFile(args[0], "", true)); } public final String rawUrlToFile(String urlspec, String path, boolean writerawurl) { for(int j=0; j<malstrings.length; j++) malstats[j] = new StringBuffer(malstrings[j] + ": " ) ; String rawurlfile = path + "$$rawurl.txt" ; String statsoutfile = path + "$$inspecturl.txt" ; String today = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(new Date()); FileOutputStream rawurlout = null; URL url = null; BufferedInputStream buffin = null; BufferedOutputStream buffout = null ; LineNumberReader linbuffread = null; BufferedWriter buffwrite = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024) ; try { // get raw URL content into BytteArrayOutputStream as quickly as possible. url = new URL(urlspec) ; buffin = new BufferedInputStream(url.openStream(), 512) ; while (true) { int datum = buffin.read() ; if (datum == -1) break ; baos.write(datum) ; } buffin.close() ; byte[] ba = baos.toByteArray() ; if(writerawurl) { rawurlout = new FileOutputStream(rawurlfile) ; rawurlout.write(ba) ; // write entire raw URL contents from byte array. rawurlout.close() ; } ByteArrayInputStream bais = new ByteArrayInputStream(ba) ; linbuffread =new LineNumberReader(new InputStreamReader(bais)) ; buffwrite = new BufferedWriter(new FileWriter(statsoutfile)) ; StringBuffer urlbuff = new StringBuffer(1024) ; StringBuffer headbuff = new StringBuffer(256) ; StringBuffer statsbuff = null; String aline = ""; String lcaline = "" ; // lowercase version for string search. int linenumber = 0; while ((aline = linbuffread.readLine()) != null) { linenumber = linbuffread.getLineNumber() ; urlbuff.append(linenumber + ": " + aline + newline) ; // the numbered contents. lcaline = aline.toLowerCase(); for (int i=0; i<malstrings.length; i++){ //acquire statistics. if(lcaline.indexOf(malstrings[i])>=0) malstats[i].append(linenumber+ ", ") ; } } headbuff.append("*************** URL Analyzer: ******************* " + newline) ; headbuff.append("Author: Michel I. Gallant 11/07/2000" + newline+newline) ; headbuff.append("--- Statistics: " + urlspec + " (" + today + ") -----" + newline) ; headbuff.append("[String] [line numbers]" + newline) ; for (int i=0; i<malstats.length; i++) headbuff.append(malstats[i].toString() + newline) ; headbuff.append("----------------------------------------" + newline + newline) ; buffwrite.write(headbuff.toString()) ; buffwrite.write(urlbuff.toString()) ; linbuffread.close() ; buffwrite.close() ; return "Analysis on " + urlspec + " successful." ; } catch (MalformedURLException mue) { return "Analysis on " + urlspec + " failed (Incorrect URL specification)." ; } catch (IOException ieo) { return "Analysis on " + urlspec + " failed (I/O problem)." ; } finally { try { if (buffin != null) buffin.close(); } catch (IOException e) {} try { if (buffout != null) buffout.close(); } catch (IOException e) {} try { if (linbuffread != null) linbuffread.close(); } catch (IOException e) {} try { if (buffwrite != null) buffwrite.close(); } catch (IOException e) {} } } }