
VBScript to check Web Proxy |
Post Reply
|
| Author | |
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
Topic: VBScript to check Web ProxyPosted: 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?
|
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
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.
|
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
Posted: 08 Feb 2010 at 07:14 |
|
This accesses a page that doesn't exist and returns the status code.
|
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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 |
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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 |
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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
|
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
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.
I don't believe this can be done with the COM scripting objects.
|
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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 |
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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 |
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
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.
|
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
Posted: 11 Feb 2010 at 22:42 |
|
JVIERRA you ROCK !!!!! :-) |
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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. |
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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.
|
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
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.
|
|
![]() |
|
mssbusybee
I'm new here
Joined: 08 Feb 2010 Online Status: Offline Posts: 13 |
Quote Reply
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.
|
|
![]() |
|
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6846 |
Quote Reply
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.
|
|
![]() |
|
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |