Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2011-02-21
Medium Priority
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 252 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", "http://ss64.com/nt/if.html", 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 248 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 71

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

916 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