Solved

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

Posted on 2011-02-21
5
299 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
[X]
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
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 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.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

623 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