Solved

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

Posted on 2011-02-21
5
296 Views
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.

Thanks

Brian
0
Comment
Question by:sysadmin101010
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
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:


testConnection.bat:

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

Open in new window


c:\>c:\temp\testConnection.bat 1.2.3.4
OFFLINE

c:\>c:\temp\testConnection.bat 192.168.1.2
ONLINE

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.

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

Open in new window

0
 
LVL 3

Accepted Solution

by:
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", "http://ss64.com/nt/if.html", False
  .send
  WScript.Quit(.status)
End With

Open in new window

Script2.vbs provides a 15 minute delay

 
WScript.Sleep(900000)

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
:CheckService
CScript Script1.vbs
If "%ErrorLevel%" NEQ "200" ( Goto CloseTunnel )
If "%Tunnel%" EQU "0" (Goto OpenTunnel)

:Delay
CScript Script2.vbs
Goto CheckService

:OpenTunnel
REM Commands to Open Tunnel here

Set Tunnel=1
Goto Delay

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

Set Tunnel=0
Goto Delay

Open in new window

0
 
LVL 10

Assisted Solution

by:wls3
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
    param(
        # Set defaults, only to be overwritten if input parameters are added to the function call
        [Parameter(Mandatory=$true)]
        [string]
        $outputfile,
        
        [Parameter(Mandatory=$true)]
        [string]
        $url,
        
        [Parameter(Mandatory=$true)]
        [string]
        $pattern
    )
    
    # 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
    if($patternMatch)
    {
        # Replace with real path to vpn .exe and custom arguments
        Start-Process -FilePath "cmd.exe" -argumentlist "Put your arguments here"
    }
    else
    {
        # 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:

http://learningpcs.blogspot.com/2010/06/powershell-start-process-argumentlist.html

0
 
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.
0

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
A brief introduction to what I consider to be the best editor for PowerShell.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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