Testing if an Proxy and/or Website is online

by WinstonTheCat at 2012-11-18 10:00:27

I have been searching for scripts on doing this but I have not been able to get them to work. I would like to test a list of proxies for ones that are online (so that they could be checked later) and if this works with websites too.
What I am planning for it to do is put a list of proxies into an array then check each one if it is online(able to be connected too), storing them into a text file.
And if there is a way to test if a website is online or offline too that would be helpful.
by DonJ at 2012-11-20 09:00:06
Jeff and I wrote an example like this for our PowerShell book at SAPIEN Press (http://www.sapien.com/books/), and you can download the code examples for free. Basically, you launch a Web request, and then check the result. If it’s HTTP200, then the server is online and responding. Anything else and it isn’t. Anything you can hit with HTTP should be do-able with that technique.

Proxies might be harder since they’re not meant to respond to direct Web requests, but it’s the same theory. You might try it with one and see what happens.
by WinstonTheCat at 2012-11-20 09:30:29
Thanks DonJ, I’ll try this out. I’ll come back to tell the result.

Edit: This script "Ping-WebServer.ps1" works for what I wanted, with a website at least.
My only problem is when a website it not online, it prints an error which I don’t want.
Script:
Function Ping-WebServer {
Param([string]$url)
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
If ($response.StatusCode -eq "OK") {
$True
} else {
$False
}
}
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$sites = gc "$dp0\sites.txt"
$a = 0
$sites | ForEach-Object {
$a = $a + 1
$site = $sites[$a]
$ans = Ping-WebServer "http://$site"
if($ans-eq’True’){write-host "$site Site Online"-foregroundcolor green}
else{write-host "$site Site Offline"-foregroundcolor red}
}
read-host ":Finished"


Output with a random list of websites
elg.no Site Online
anotherdayanotherword.com Site Online
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (404) Not Found."
At C:\Users\Winston\Documents\SAPIEN PS Scripts\Ping-WebServer.ps1:4 ch
ar:34
+ $response = $request.GetResponse <<<< ()
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

applecanceledchristmas.com Site Offline
arewestilliniraq.com Site Online
classnamer.com Site Online
confuses.me Site Online
d-e-f-i-n-i-t-e-l-y.com Site Online
dingitsup.com Site Online
downforeveryoneorjustme.com Site Online
drawminos.com Site Online
eliotsucks.com Site Online
evilincarnate.net Site Online
flippingtypical.com Site Online
hackthegibson.net Site Online
hasthelargehadroncolliderdestroyedtheworldyet.com Site Online
hasthelhcdestroyedtheearth.com Site Online
Exception calling "GetResponse" with "0" argument(s): "The remote name could no
t be resolved: ‘hillaryismomjeans.com’"
At C:\Users\Winston\Documents\SAPIEN PS Scripts\Ping-WebServer.ps1:4 ch
ar:34
+ $response = $request.GetResponse <<<< ()
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

hillaryismomjeans.com Site Offline
howtobakeapotato.com Site Online
by WinstonTheCat at 2012-11-20 11:18:52
I don’t think I’ll be able to do this with proxies but it works just fine with websites "Ping-WebServer.ps1"
My only issue is when a server is offline, it responds with an error which I don’t want printed, although it is right.

Script Edited to what I want
Function Ping-WebServer {
Param([string]$url)
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
If ($response.StatusCode -eq "OK") {
$True
} else {
$False
}
}
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$sites = gc "$dp0\sites.txt"
$a = 0
$sites | ForEach-Object {
$a = $a + 1
$site = $sites[$a]
$ans = Ping-WebServer "http://$site"
if($ans-eq’True’){write-host "$site Site Online"-foregroundcolor green}
else{write-host "$site Site Offline"-foregroundcolor red}
}
read-host ":Finished"


This is the output, using a random list of websites as a test.
ulv.no Site Online
elg.no Site Online
anotherdayanotherword.com Site Online
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (404) Not Found."
At C:\Users\Winston\Documents\SAPIEN PS Scripts\Ping-WebServer.ps1:4 ch
ar:34
+ $response = $request.GetResponse <<<< ()
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

applecanceledchristmas.com Site Offline
arewestilliniraq.com Site Online
classnamer.com Site Online
confuses.me Site Online
d-e-f-i-n-i-t-e-l-y.com Site Online
dingitsup.com Site Online
downforeveryoneorjustme.com Site Online
drawminos.com Site Online
eliotsucks.com Site Online
evilincarnate.net Site Online
flippingtypical.com Site Online
hackthegibson.net Site Online
hasthelargehadroncolliderdestroyedtheworldyet.com Site Online
hasthelhcdestroyedtheearth.com Site Online
Exception calling "GetResponse" with "0" argument(s): "The remote name could no
t be resolved: ‘hillaryismomjeans.com’"
At C:\Users\Winston\Documents\SAPIEN PS Scripts\Ping-WebServer.ps1:4 ch
ar:34
+ $response = $request.GetResponse <<<< ()
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

hillaryismomjeans.com Site Offline
howtobakeapotato.com Site Online
howtocutapineapple.com Site Online