News:

Jätä aivosi narikkaan, ja astu peremmälle :moi:
Apachefoorumi ei vastaa narikkaan jätetyistä aatteista.

Main Menu

W7 - VPN-yhteyden valvominen ilman erillista ohjelmaa

Started by Lexa, Wed 26.11.2014 14:39:21 (UTC+0200)

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Lexa

Alla oleva toimii kopioimalla esimerkiksi notepadiin, ja tallentamalla .bat tiedostoksi. Esimerkiksi VPN.bat
Ensimmäisessä osiossa on muokattava osuus, aikarajat, yhteyden nimi, testattava IP-osoite, jne.

Koodi on vielä kehitystyössä, saatan muokata vielä - parannella tätä ja lisätä toiminnallisuuttakin. :think:
Mm. Ajatuksissa on lisätä tähän jonkinlainen logitoiminto, ja ehkä sähköposti-ilmoitukset katkoista.

Tarkoitus tällä on siis valvoa että VPN-yhteys pysyy päällä, ja jos ei pysy niin korjata tilanne automaattisesti.
Kätevä esimerkiksi jos pitää saada VPN yhteys kahden koneen välille, joista toinen käyttää mobiiliyhteyttä. Mobiiliyhteyksiin ei saa otettua yhteyttä ulkopuolelta, vaan yhteys pitää avata siitä koneesta käsin jossa mobiiliyhteyttä käytetään - Sen jälkeen yhteys toimii kumpaankin suuntaan. Tällä pikkukoodilla saa tämän yhteyden avattua ja tarvittaessa korjattua automaattisesti.

Ehdotuksia ja kysymyksiä otetaan vastaan :)


@ECHO OFF

REM = VARIABLES - PLEASE EDIT ACCORDING TO YOUR OWN SETUP =

REM MAXDELAY: Waiting period after each successful test cycle. Default is 10 minutes ( 600 )
REM RECONDELAY: Waiting period after each failed test cycle, before next reconnect attempt. Default is 2 minutes ( 120 )
REM VPNNAME: The preconfigured name of the VPN connection to be monitored, can be found from your control panel
REM VPNUSER: Username to logon on to the preconfigured VPN connection
REM VPNPASS: Password for the username entered as VPNUSER.
REM VPNIP: The IP-address to ping when testing configured VPN.
REM VPNIP: Works best if this is another computer within the same VPN connected network.
REM PINGRESPONSE: The expected response from a successful PING
REM RASDIALRESPONSE: The expected response from RASDIAL after successful connection

REM NOTICE! The script was written for Windows 7, in Finnish!
REM NOTICE! For other languages, you will need to edit the expected keywords
REM NOTICE! from responses after PING and RASDIAL accordingly.
REM NOTICE! For English Win 7 you may use "Successfully" for RASDIALR
REM NOTICE! Use "Reply" for PING.

SET maxdelay=600
SET recondelay=120
SET vpnname=VPN-yhteys
SET vpnuser=Username
SET vpnpass=Password
SET vpnip=192.168.10.36
SET pingresponse=Vastaus
SET rasdialr=Suorittaminen

REM = ACTUAL TEST CYCLE, DO NOT EDIT BELOW THIS LINE =

CLS

REM Make sure the connection we are trying to monitor can actually be monitored.
REM Is it active at the moment?
REM If no active connection with the configured name can be found,
REM skip testing and move on to try and activate the connection
REM If active connection can be found, move on to testing the connection

:start

rasdial | find /I "%vpnname%" > nul
if errorlevel 1 goto vpndown
if errorlevel 0 goto pingtest

REM Ping the test IP-address, to make sure the connection is active and actually works.
REM If no response is received, move on to try and reactivate the connection.
REM If a successful response is received, skip reactivation and move on the wait for the next cycle.

:pingtest

ping %vpnip% -n 1 | find /I "%pingresponse%" >nul
if errorlevel 1 goto vpndown
if errorlevel 0 goto vpnup

REM If VPN connection was not found, was not active, or no response was received
REM for the test ping, close the active connection if there is any.
REM After closing the connection, reopen connection and try to logon again.

:vpndown

echo %time% VPN connection is not active, or not responding.
echo %time% Closing active VPN connections using the tunnel %vpnname%.
rasdial "%vpnname%" /d >nul

echo %time% Trying to reopen the tunnel %vpnname%.
rasdial "%vpnname%" "%vpnuser%" "%vpnpass%" | find /I "%rasdialr%" > nul
if errorlevel 1 (
echo %time% Connection could not be reopened. The host may be busy.
echo %time% Waiting for %recondelay% seconds before attempting again.
timeout /t %recondelay% /nobreak > NUL
goto start
) else (
echo %time% Connection succesfully reopened.
echo %time% Waiting for %maxdelay% seconds before running the next test cycle.
timeout /t %maxdelay% /nobreak > NUL
goto start
)

REM If VPN connection was found, and the test ping received a successful response, delay next test cycle.

:vpnup

echo %time% VPN connection is active and responding.
echo %time% Waiting for %maxdelay% seconds before running the next test cycle.
timeout /t %maxdelay% /nobreak > NUL
goto start

REM This script will run until interrupted by user, it will not terminate on it's own.
REM Written, tested and wholeheartedly abused daily By LexA since 2014

:endloop

goto start

"Lexa on koko Apache foorumin nettipoliisien PÄÄLLIKKÖ!" -Arto Lauri

The future unknown, but is there ever time to find out...?

Like what I do? Buy me a beer!

Tule mukaan Apachefoorumin Discord-kanavalle!

Lexa

Pro-vinkki. Jos tahdot käyttää tätä juuri siihen mihin minäkin tätä käytän, eli varmistaaksesi VPN yhteyden koneeseen joka on kaukana poissa, ja valitettavasti mokkulayhteyden varassa - niin kannattaa asentaa kummallekin koneelle dropbox, lykätä tämä skripti dropboxin kotikansioon, ja etäkoneessa ajoittaa tämä skripti ajettavaksi aina käynnistyksen yhteydessä. ( Ohjeet automatisoituun kirjautumiseen ym. luon myöhemmin erikseen ). Kun skripti ajetaan automaattisesti, ja suoraan Dropboxin sisältä - voit kotoa käsin muokata skriptiä tarvittaessa, ja muutokset siirtyvät livenä etäkoneelle Dropboxin kautta, ja ladataan kun seuraavan kerran tiedosto suoritetaan.
"Lexa on koko Apache foorumin nettipoliisien PÄÄLLIKKÖ!" -Arto Lauri

The future unknown, but is there ever time to find out...?

Like what I do? Buy me a beer!

Tule mukaan Apachefoorumin Discord-kanavalle!

Quick Reply

Warning: this topic has not been posted in for at least 356 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Verification:
Please leave this box empty:

What is the most visible color in our forum?:
What is the name of this forum?:
Shortcuts: ALT+S post or ALT+P preview