FAQ FAQ  Forum Search   Register Register  Login Login

VBScript to check Web Proxy

 Post Reply Post Reply
Author
  Topic Search Topic Search  Topic Options Topic Options
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Topic: VBScript to check Web Proxy
    Posted: 08 Feb 2010 at 04:51
Hi
 
I have totally four boxes out of which one is management box which is at the IP address of 10.0.0.1 and  three web-proxy each listening on port 8080
 
Management Box = 10.0.0.10
 
Web-Proxy Box1= 10.0.0.1:8080
Web-Proxy Box2= 10.0.0.2:8080
Web-Proxy Box3= 10.0.0.3:8080
 
Now I want to Write a vbscript which runs on my Management box so that it checks to see if a particular web-page loads via the proxy box, In a way I am testing to see if the proxy is up and functioning, it the web-page is working via proxy box 1 then it should check box 2 and then box3. 
 
If the web-page is not loading via any one of the box I need to have it send an email to a particular address, for which I already have a code written and it is working fine.
 
This is where I have got my self with the code.
 
Dim objIE
Set objIE = WScript.CreateObject ("InternetExplorer.Application")
objIE.Toolbar = false
objIE.statusbar=false
objIE.Navigate "http://www.google.com"
 If Err.Number <> 0 Then
  WScript.Echo "Error number is      = " & Err.Number
  WScript.Echo "Error Description is = " & Err.Description
  WScript.Echo "The web-page is not loading"
  
 Else
  WScript.Echo "Error number is      = " & Err.Number
  WScript.Echo "Error Description is = " & Err.Description
  WScript.Echo "The web-page is loading perfectly fine"
 End If
 
The trouble is I have not integrated Proxy usage on this as well as the code does not give the script failed even when the page is not loading.  Kindly help me in this senario as to what I should do?
 
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 08 Feb 2010 at 07:03
That is correct.  You will not get an error as none has occurred.  YOU will have to read the page to see what error it may have produced.
 
You can get the error programmically via the XMLHttpRequest object.
 
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 08 Feb 2010 at 07:14
This accesses a page that doesn't exist and returns the status code.
 

sURL="http://www.scriptinganswers.com/test.aspx"
Set oXml=CreateObject("Microsoft.XMLHTTP")
oXml.Open "Get", sURL, False
oXml.Send ""
wscript.echo "getting " & sURL
WScript.Echo "Page Status:" & oXml.status
 
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 09 Feb 2010 at 00:56
On the outset I would like to thank you for helping me however when I tried running the code given above
 
[CODE]
sURL="http://www.google.com"
Set oXml=CreateObject("Microsoft.XMLHTTP")
oXml.Open "Get", sURL, False
oXml.Send ""
wscript.echo "getting " & sURL
WScript.Echo "Page Status:" & oXml.status
[\CODE]
 
However I got this error :-(
 
C:\>"C:\Documents and Settings\mssbusybee\Desktop\test.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
C:\Documents and Settings\mssbusybee\Desktop\test.vbs(4, 1) msxml3.dll: Access is denied.


Edited by mssbusybee - 09 Feb 2010 at 06:52
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 09 Feb 2010 at 04:37
After banging my head a lot I was finally able to write this small piece of Code as shown below.
 
In this code I will test to see that if Google page is loading, provided it loads I am just logging it on C Drive on the file called log.txt,  If it does not load it will shoot out an email.
 
Now I want to know how I could include proxy address and port which it should use to access the Google Page, Right now it picks it out of the IE > Tools > Internet Options > Connection Settings
 
Dim objIE, objFSO, objTS
Set objIE = CreateObject("InternetExplorer.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objIE.Navigate("http://www.google.com")
Do While objIE.Busy
WScript.Sleep 500
Loop
 If "Google" = objIE.Document.Title Then
  Set objTS = objFSO.OpenTextFile("C:\Log.txt",8,True)
  objTS.WriteLine Now & ":- I was able to access Google Web page via the Proxy"
  objTS.Close
 Else
  Set objEmail = CreateObject("CDO.Message")
  objEmail.From = "Someone@somedomain.com"
  objEmail.To = "someoneelse@someotherdomain.no"
  objEmail.Subject = "HTTP Proxy In Trouble"
  objEmail.Textbody = "This mail is purely for test to confirm the HTTP path is working"
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.0.0.50"
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  objEmail.Configuration.Fields.Update
  objEmail.Send
 End If


Edited by mssbusybee - 09 Feb 2010 at 23:52
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 09 Feb 2010 at 04:38
I have to admit that I took most of the code from Script Sharing section and some from Google :D
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 09 Feb 2010 at 07:46
What you are trying to do won't work.  It is not possible to do this using IE.  You must use the HTML object.  You could also use the NET WEbCLient from PowerShell. 
 
If you gewt permisions failures from the object load then you have a broken DCOM or MSXML subsystem which would have to be fixed first.
 
PowerShell will let you select the Proxy and use credentials on teh proxy.
 

$WebClient = new-object System.Net.WebClient
$WebClient.Proxy = new-object System.Net.WebProxy("
http://192.168.1.1:8080")
$WebClient.Proxy.Credentials = Get-Credential mydom/myaccount
$WebClient.Headers.Add("user-agent", "PowerShell")
$WebClient.OpenRead("
http://www.google.com/")
 
I don't believe this can be done with the COM scripting objects.
 
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 11 Feb 2010 at 05:50

Thanks to all your help Jvierra

I am not a scripting Guru; However I found this piece of Code on the link http://services.scala.com/training/?p=27, which taught me how to set Proxy addresses by using VBScripting and I have modified it to check on my Proxy servers, Other two links which helped me are http://www.neilstuff.com/winhttp/#WaitForResponse_Method which taught me how to set connection time limits and http://www.808.dk/?code-simplewinhttprequest which taught me how to check for errors.

Although I still cannot completely understand Asynchronous loading of webpage when we have the third parameter as "FALSE"

Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

WinHttpReq.SetTimeouts 15000,15000,15000,15000

WinHttpReq.SetProxy 2,"<ProxyIP>:8080"

WinHttpReq.Open "GET", "http://google.com", False

WinHttpReq.SetRequestHeader "Cache-Control", "no-cache"

WinHttpReq.SetRequestHeader "Pragma", "no-cache"

On Error Resume Next

WinHttpReq.Send

If Err.Number = 0 Then

If WinHttpReq.Status = "200" Then

'WScript.Echo WinHttpReq.ResponseText

WScript.Echo "Google Page Accessible"

Else

WScript.Echo "HTTP " & WinHttpReq.Status & " " & WinHttpReq.StatusText

End If

Else

WScript.Echo "Error " & " " & Err.Number & " " & Err.Source & " " & Err.Description

End If

I want to know will there be any problem because of running this script?

Do you know what will happen to the connection which gets established to the Google webpage? Will they die immediately after the script gets completed or will they continue to exist through my proxy server?



Edited by mssbusybee - 11 Feb 2010 at 06:07
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 11 Feb 2010 at 06:32

Now this is the code I am going to use to check all the three HTTP Proxy server, Kindly let me know if I will face any issue with this.

On Error Resume Next
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttpReq.SetTimeouts 30000,9000,15000,15000
For i = 1 To 3
  If i = 1 Then
    WinHttpReq.SetProxy 2,"10.0.0.1:8080"
  End If
  If i = 2 Then
    WinHttpReq.SetProxy 2,"10.0.0.2:8080"
  End If
  If i = 3 Then
    WinHttpReq.SetProxy 2,"10.0.0.3:8080"
  End If
  
  WinHttpReq.Open "GET", "http://google.com", False
  WinHttpReq.SetRequestHeader "Cache-Control", "no-cache"
  WinHttpReq.SetRequestHeader "Pragma", "no-cache"
  WinHttpReq.Send
   If Err.Number = 0 Then
      If WinHttpReq.Status = "200" Then
    WScript.Echo "Google page worked"
      Else
    WScript.Echo "HTTP " & WinHttpReq.Status & " " & WinHttpReq.StatusText
      End If
   Else
      WScript.Echo "Error " & " " & Err.Number & " " & Err.Source & " " & Err.Description
   End If
  Err.Clear     
Next
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 11 Feb 2010 at 08:59
Yes - that is teh VBScript COM equivalent of the WebCLient class.  It is harder to use but should work for you.
 
It makes no permanent changes.  What you are doing is just looking at the web through different requested proxy addresses.
 
If the machines you are on are set to proxy at teh machine level I am not sure what this will do.  It may not work at all or it may.  Still it should cause no issues.
 
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 11 Feb 2010 at 22:42

JVIERRA you ROCK !!!!! :-)

Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 12 Feb 2010 at 04:20

I have both IE and Mozilla installed on my server and both the browsers are manually configured with proxy to reach to the internet.  The code which I have posted above still works flawlessly. 

Now I am working on emailing part, Once done I will post it up here.
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 16 Feb 2010 at 05:21

Hi I am back with the code.

I have added one more IP to the list of Proxies which is 10.0.0.4.  Just rename the Mail Server IP with your mailserver and change the from and TO address on the mails.  WOLA you are done.

' This piece of code will check to see if it is able to access the Google's page via the four proxies listed.  It doesn't matter if it is accessible
' or not it will log an entry into the file C:\WebPage.txt. For this to happen all For loop should be changed from For i = 1 to 4 from present value.
' Apart from logging into the file C:\WebPage.txt it is simultaneously populating all the array's defined.
'
'We are then creating a file called as Proxy.txt on the Desktop give proper path for this file Proxy.txt, and populating all the values which we
'acquired above.  Then we run a for loop one last time to check to see if any value of arrArray is above Zero.  If it is present then it means to say
'that some proxy was not able to process the http traffic and sent out an e-mail.
On Error Resume Next        'This line is used to make sure that we don't exit out on errors.
Set fso = CreateObject("Scripting.FileSystemObject")    'We are creating a File System Object.
Set objLOG = fso.OpenTextFile("C:\WebPage.txt",8,True)    'We are creating a logfile called Webpage.txt, 8 stands for appending, True means create the file if it is not present already
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")   'Here we are creating an object for HTTP
WinHttpReq.SetTimeouts 30000,9000,15000,15000     '30Sec for DNS Time out, 9 Sec for ConnectTimeout, 15 Sec for SendTimeout, 15 Sec for ReceiveTimeout.
Dim arrArray(3)         'This array holds just the index number of proxies which are being checked.
Dim ErrStatus(3)        'This array prints ErrStatus in the mail.
Dim ErrStatusText(3)        'This array prints ErrStatusText in the mail.
Dim ErrNumber(3)        'This array prints ErrNumber in the mail.
Dim ErrSource(3)        'This array prints ErrSource in the mail.
Dim ErrDescription(3)        'This array prints ErrDescription in the mail e.g. Operation Timed out.
j=0          'This value of J is going to Flip Flop so that all the array value will either have Zero or some good value
For i = 1 To 4         'The For loop is for looping through the number of proxy to be checked.
   If i = 1 Then
 WinHttpReq.SetProxy 2,"10.0.0.1:8080"     'This is the FIRST HTTP proxy which we will be checking, number 2 is to set the proxy as the next value.
   End If
   If i = 2 Then
 WinHttpReq.SetProxy 2,"10.0.0.2:8080"     'This is the SECOND HTTP proxy which we will be checking
   End If
   If i = 3 Then
 WinHttpReq.SetProxy 2,"10.0.0.3:8080"     'This is the THIRD HTTP proxy which we will be checking
   End If
   If i = 4 Then 
 WinHttpReq.SetProxy 2,"10.0.0.4:8080"     'This is the Fourth HTTP proxy which we will be checking
   End If
 WinHttpReq.Open "GET", "http://google.com", False   'Here we are trying to load the Google Page, False=Opens the HTTP connection in Synchronous mode.
 WinHttpReq.SetRequestHeader "Cache-Control", "no-cache"   'This is here not to load any cached page or something.
 WinHttpReq.SetRequestHeader "Pragma", "no-cache"   'This is here not to load any cached page or something.
 WinHttpReq.Send        'Here we are sending all the defined parameter to the proxy.
    If Err.Number = 0 Then      'Here we are checking to see if the Err.Number is ZERO.
  If WinHttpReq.Status = "200" Then    'Here we are checking to see that the Status is 200, which means to say that the page loaded properly.
     ErrStatus(i-1) = WinHttpReq.Status    'Here we are populating the ErrStatus when web-page loads properly will be 0.
     ErrStatusText(i-1) = WinHttpReq.StatusText   'We are populating the ErrStatusText when web-page loads properly will be 0.
     objLOG.WriteLine Now & ":- Google Accessible via the Proxy"  'We are logging to the file WebPage.txt
  Else
     j = 1       'The Value of J is Flipped.
     ErrStatus(i-1) = WinHttpReq.Status    'We are fillup up ErrStatus array with the error status.
     ErrStatusText(i-1) = WinHttpReq.StatusText   'Same as above 
     objLOG.WriteLine Now & ":- Google NOT ACCESSIBLE via the Proxy with ErrNumber = 0" 'We are logging to the file WebPage.txt
  End If
    Else
  j = 1        'The Value of J is Flipped.
  ErrNumber(i-1) = Err.Number     'ErrArray is populated.
  ErrSource(i-1) = Err.Source     'ErrSource is populated.
  ErrDescription(i-1) = Err.Description    'ErrDescription is populated. 
  ErrStatus(i-1) = 0      'ErrStatus is populated here.
  ErrStatusText(i-1) = 0      'ErrStatusText is populated here.
  objLOG.WriteLine Now & ":- Google NOT ACCESSIBLE via the Proxy" 'We are logging to the file WebPage.txt
    End If
   
  If j = 1 Then
     arrArray(i-1) = i      'Here element values of arrArray is populated to a value greater than zero i.e. equal to i.
  Else
     arrArray(i-1) = 0      'Here element values of arrArray is populated to Zero if proxy for this loop works.
     ErrNumber(i-1) = 0      'When no Error occurs ErrNumber is given a value of Zero.
     ErrSource(i-1) = 0      'When no Error occurs ErrSource is given a value of Zero.
     ErrDescription(i-1) = 0      'When no Error occurs ErrDescription is given a value of Zero.
  End If 
     j = 0       'The Value of J is flipped back to Zero for second Proxy checking.
  Err.Clear       'Here we are resetting the Error back to Zero.
Next          'Here we are going to increase the value of I by one and check for the second proxy.
objLOG.Close
Set objOutputFile = fso.OpenTextFile("C:\Proxy.txt",2,True)   'Here we are creating a text file called Proxy.txt, in Write Mode and creating it if it is not there already.
objOutputFile.WriteLine("CODE RED, CODE RED, CODE RED !!!!!!!")  
objOutputFile.WriteLine("Time: "&  Now)      'We are adding in the Time Stamp
objOutputFile.Writeline("============================")
For i = 1 To 4         'In this For loop we will populate with all our findings.
  If i = 1 Then
    objOutputFile.WriteLine("Proxy 1 = 10.0.0.1:8080")
  End If
  If i = 2 Then
    objOutputFile.WriteLine("Proxy 2 = 10.0.0.2:8080")
  End If
  If i = 3 Then
    objOutputFile.WriteLine("Proxy 3 = 10.0.0.3:8080")
  End If
  If i = 4 Then
    objOutputFile.WriteLine("Proxy 4 = 10.0.0.4:8080")
  End If
objOutputFile.WriteLine("Err Http Status is = " & ErrStatus(i-1))
objOutputFile.WriteLine("Err StatusText is = " & ErrStatusText(i-1))
objOutputFile.WriteLine("Err Number is = " & ErrNumber(i-1))
objOutputFile.WriteLine("Err Source is = " & ErrSource(i-1))
objOutputFile.WriteLine("Err Description is = " & ErrDescription(i-1))
 If ErrDescription(i-1) = 0 Then
  objOutputFile.Writeline(" ")
 End If
Next
objOutputFile.Close        'Here we are closing the file to be opened up in READ MODE.

j=0          'Here we are initilizing the value of J to Zero.
For i = 1 To 4
 If 0 < arrArray(i-1) Then      'If any one of the proxies failed one of the elements of arrArray will have a positive value.
  j = 1        'Here we flip the value of J to 1
 End If
Next
If j = 1 Then         'Here we are sending the email.
 Set objInputFile = fso.OpenTextFile("C:\Proxy.txt",1,True)  'Here we are opening the Proxy.txt in Read Only mode.
  Set objEmail = CreateObject("CDO.Message")   'Here we are creating the Mail Object.
   objEmail.From = "HTTPTest@somedomain.com"  'All others are self explainatory
   objEmail.To = "mssbusybee@somedomain.com"
   objEmail.Subject = "vbs bounce"
   objEmail.Textbody = objInputFile.ReadAll  'I am reading out all that is on the text file Proxy.txt as the body of the email.
   objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "MAIL Server IP"
   objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   objEmail.Configuration.Fields.Update
   objEmail.Send
 objInputFile.Close
End If
 

 

 

 

Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 16 Feb 2010 at 06:48
I am sorry but I am not quite sure what the purpose of this code is. 
 
Is it causing errors? 
If so what is the exact error?
 
PS.  It's spelled:  VoilĂ  avec un accent.
 
Back to Top
mssbusybee View Drop Down
I'm new here
I'm new here


Joined: 08 Feb 2010
Online Status: Offline
Posts: 13
  Quote mssbusybee Quote  Post ReplyReply bullet Posted: 21 Feb 2010 at 03:04
There is nothing wrong with the code given above,  I have pasted the fully working code, which will check four proxy and then send out an email with the findings.
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 21 Feb 2010 at 07:48
Oh - I am sorry.  I misunderstood.
 
It's quite a piece of code.  Can you document why someone might want to do this.  I don't really understand what it's purpose is. 
 
Usually we set the proxy via Group Policy or deom a file on a web server in the local subnet.  This is automatic and can be done based on subnet association or other association.. Iti is a function built into nearly all browsers and is usable with Unix and other OS web servers and hosts.
 
I am  guessing that  there is some other reason for doing this that I can't think of so I am curious as to what it would be.
 
If you want this code to be available for a long periosd of time you should put it into the Script Sharing forum.  Topics are deleted here periodically to manage space.  THe Script Sharing forum is never pruned in this way.
 
You might also attach the code as a fie so it can be eaily downloaded.  COpying and pasting from this forum is nearly impossible with some browsers wihtout losing all of the line feeds.  I cannot copy code from here without running it through a custom formatter to recover the line feeds.
 
The code looks well structured.  I can mke a couple of standard criticisms though.
 
Try not to start your code with "On Error Resume Next"  This tends to creating code that runs wild and can cause unexpected things to happen.  It is also very hard to debug.  In Error required that you handle every potential error in your code.   You are not handling any errors at all as far as I can see.  Errors that occur will never be detected.
 
While not a requirement you should look into using subroutines as they can make coding easier and can be reused in future scripts as long as they are correctly generalized. The following are excellent candidates for subroutining:
 
1. Mail send section.
2. Proxy checking code inside of loop.
 
Subroutining would eliminate the need for the array checking loop.
 
Restructuring the code would help you to see wasy to think about code that make arriving at  a solution much easier.
 
In any case your code is very good as it does seem to do what you wanted as longt as you fix teh On Error issue.  Basically you need to remove the On Error statement altogether.  Any error you get indicated that the script should not attempt to finish.  If there is some line of code that you expect an error on and need to suppress it then place the On Error before the line and undo it after the line. Example:
 
On Error Resume Next
Set objOutputFile = fso.OpenTextFile("C:\Proxy.txt",2,True)
If Err.Number = 5 Then
     MsgBox "Bad erro 5 has happened we will quit now"
     WScript.Quit &H80011001 ' make a fancy hex returncode.
ElseIf Err.Number <> 0 Then
     WScript.StdErr.WriteLine " An error occured but we are going to ignore it"
End If
On Error GoTo 0
 
The last line turns error handling back on.
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down