A Script to run a program based on the state of a website

Posted on 2011-02-21
Last Modified: 2012-05-11
Platform Windows XP, - 7:

Scripting language, I'm open to suggestions, but was thinking about using Windows Power Shell

Need a script that basically checks the state of a website on the internet. It will be running all the time in the background, checking every 15 minutes or so. If the site is up, then I want it to run openvpn and create a tunnel to a ip address defined in the openvpn config.

If the website is down, terminate the tunnel.

New to scripting, thanks in advance.

If you can recommend any good beginner books on Windows Powershell that would be a plus.


Question by:sysadmin101010
LVL 16

Expert Comment

ID: 34948794
The tricky part is deciding how to check if the website is online.

When you check the state of a website, that can mean a few things.  Is successfully "pinging" the server IP good enough, or do you need to check that you can actually successfully fetch a page using HTTP from the web site?

ping is easy to use in a batch file.  Like this:


@ping -n 1 -w 1000 %1 >nul:
	@echo ONLINE
) else (
	@echo OFFLINE

Open in new window



Open in new window

Now you can put whatever you want in the IF branches.

If you like this approach, we can put it into a 15-minute loop.

@ping -n 1 -w 1000 %1 >nul:
	@echo ONLINE
) else (
	@echo OFFLINE
@sleep 900
@goto loop

Open in new window


Accepted Solution

sam0x01 earned 63 total points
ID: 34952816
For http checking, these scripts checks a page on the site and have an area to close the vpn connection

Script1.vbs checks the site and exits with the status code (or the error code if there is an error code)

' vbscript to check a sites page and exit with status
With CreateObject("MSXML2.XMLHTTP")
  .open "HEAD", "", False
End With

Open in new window

Script2.vbs provides a 15 minute delay


Open in new window

Script3.cmd controls the other scripts and has areas to add the connect and disconnect the openvpn tunnel. It also ensure you dont redisconnect or reopen the tunnel each 15 minutes.

It will only disconnect the tunnel if the tunnel was connected and the site page is unavailable. It will only connect the tunnel on startup after checking the page status or after it has been disconnected, and the page returns online.

Set Tunnel=0
CScript Script1.vbs
If "%ErrorLevel%" NEQ "200" ( Goto CloseTunnel )
If "%Tunnel%" EQU "0" (Goto OpenTunnel)

CScript Script2.vbs
Goto CheckService

REM Commands to Open Tunnel here

Set Tunnel=1
Goto Delay

If "%Tunnel%" EQU "0" (Goto Delay)
REM commands to close tunnel inserted here

Set Tunnel=0
Goto Delay

Open in new window

LVL 10

Assisted Solution

wls3 earned 62 total points
ID: 35014955
Here is a sample script (that will need minor modifcation to work) which downloads a fixed page from the website to a local file.  This file is searched for a pattern and gets deleted after the check.  Depending on whether a match was located, it either launches the process or closes the VPN.  I didn't put any specifics for the last part because I don't know your paths.  

Here's the code:
function TestWebsiteState
    # Configure input parameters
        # Set defaults, only to be overwritten if input parameters are added to the function call
    # Initialize WebClient object
    $webclient = New-Object -TypeName System.Net.WebClient
    # Download file with WebClient object
    $webclient.DownloadFile($url, $outputfile)
    # Test file for anticipated pattern in temp file
    $patternMatch = Select-String -Pattern $pattern -Path $outputfile
    # Clean up - delete file
    Remove-Item -Path $outputfile -Force
    # Using previous test results, start new process or close VPN if string not found
        # Replace with real path to vpn .exe and custom arguments
        Start-Process -FilePath "cmd.exe" -argumentlist "Put your arguments here"
        # Replace with real path to vpn .exe
        Stop-Process -Name "cmd.exe"

Open in new window

Check out the article I put up to focus on how to use the -ArgumentList property in Powershell:

LVL 70

Expert Comment

by:Chris Dent
ID: 35687957
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question