Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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", "", 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…

688 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