@echo off REM ======================================== REM TeamViewer VPN - PLC Access Setup Script REM For Windows REM ======================================== echo. echo ======================================== echo TeamViewer VPN - PLC Access Setup echo ======================================== echo. REM Check if running as administrator net session >nul 2>&1 if %errorLevel% neq 0 ( echo ERROR: This script must be run as Administrator! echo Right-click the script and select "Run as administrator" pause exit /b 1 ) REM ======================================== REM Configuration Variables - CUSTOMIZE THESE REM ======================================== echo Enter your configuration details: echo. set /p PLC_NETWORK="Enter PLC Network (e.g., 192.168.10.0): " set /p PLC_SUBNET_MASK="Enter Subnet Mask (e.g., 255.255.255.0): " set /p REMOTE_VPN_IP="Enter Remote Gateway VPN IP (e.g., 7.254.0.2): " echo. echo Configuration Summary: echo ---------------------- echo PLC Network: %PLC_NETWORK% echo Subnet Mask: %PLC_SUBNET_MASK% echo Remote VPN IP: %REMOTE_VPN_IP% echo. set /p CONFIRM="Is this correct? (Y/N): " if /i not "%CONFIRM%"=="Y" ( echo Setup cancelled. pause exit /b 0 ) REM ======================================== REM Step 1: Check TeamViewer Installation REM ======================================== echo. echo [Step 1/5] Checking TeamViewer installation... where teamviewer.exe >nul 2>&1 if %errorLevel% neq 0 ( echo WARNING: TeamViewer not found in PATH echo Please ensure TeamViewer is installed. echo. set /p TV_PATH="Enter full path to TeamViewer.exe (or press Enter to skip): " if not "!TV_PATH!"=="" ( if exist "!TV_PATH!" ( echo TeamViewer found at: !TV_PATH! ) else ( echo ERROR: TeamViewer not found at specified path. ) ) ) else ( echo TeamViewer found in PATH: OK ) REM ======================================== REM Step 2: Check Network Connectivity REM ======================================== echo. echo [Step 2/5] Checking network connectivity... echo Testing internet connection... ping -n 1 8.8.8.8 >nul 2>&1 if %errorLevel% neq 0 ( echo WARNING: No internet connection detected! echo TeamViewer requires internet to establish VPN. ) else ( echo Internet connection: OK ) REM ======================================== REM Step 3: Display Current Routes REM ======================================== echo. echo [Step 3/5] Current network routes: echo. route PRINT | findstr /C:"192.168" echo. REM ======================================== REM Step 4: Add Static Route to PLC Network REM ======================================== echo. echo [Step 4/5] Adding static route to PLC network... REM Check if route already exists route PRINT | findstr /C:"%PLC_NETWORK%" >nul 2>&1 if %errorLevel% equ 0 ( echo WARNING: Route to %PLC_NETWORK% already exists! echo. route PRINT | findstr /C:"%PLC_NETWORK%" echo. set /p DELETE_ROUTE="Do you want to delete existing route and recreate? (Y/N): " if /i "!DELETE_ROUTE!"=="Y" ( echo Deleting existing route... route DELETE %PLC_NETWORK% timeout /t 2 >nul ) else ( echo Keeping existing route. Skipping route creation. goto :skip_route ) ) echo Adding route: %PLC_NETWORK% MASK %PLC_SUBNET_MASK% via %REMOTE_VPN_IP% route ADD %PLC_NETWORK% MASK %PLC_SUBNET_MASK% %REMOTE_VPN_IP% if %errorLevel% equ 0 ( echo Route added successfully! echo. set /p MAKE_PERSISTENT="Make this route persistent (survive reboot)? (Y/N): " if /i "!MAKE_PERSISTENT!"=="Y" ( echo Adding persistent route... route DELETE %PLC_NETWORK% route ADD %PLC_NETWORK% MASK %PLC_SUBNET_MASK% %REMOTE_VPN_IP% -p echo Persistent route added! ) ) else ( echo ERROR: Failed to add route! echo This may happen if: echo - TeamViewer VPN is not connected echo - VPN IP is incorrect echo - Route already exists ) :skip_route REM ======================================== REM Step 5: Verify Configuration REM ======================================== echo. echo [Step 5/5] Verifying configuration... echo. echo TeamViewer VPN Adapter Status: ipconfig | findstr /C:"TeamViewer" if %errorLevel% neq 0 ( echo WARNING: TeamViewer VPN adapter not found! echo Make sure TeamViewer VPN connection is established. ) echo. echo Current routes to PLC network: route PRINT | findstr /C:"%PLC_NETWORK%" REM ======================================== REM Display Connection Test Instructions REM ======================================== echo. echo ======================================== echo Setup Complete! echo ======================================== echo. echo Next Steps: echo ----------- echo 1. Ensure TeamViewer VPN connection is active echo 2. Test connectivity: echo ping %REMOTE_VPN_IP% echo ping %PLC_NETWORK:~0,-1%100 (or your PLC IP) echo. echo 3. Open TIA Portal and try to connect to PLC echo. echo To remove the route later, run: echo route DELETE %PLC_NETWORK% echo. REM ======================================== REM Optional: Test Connectivity Now REM ======================================== set /p TEST_NOW="Do you want to test connectivity now? (Y/N): " if /i "%TEST_NOW%"=="Y" ( echo. echo Testing connection to remote VPN gateway... ping -n 4 %REMOTE_VPN_IP% echo. set /p PLC_IP="Enter PLC IP to test (e.g., 192.168.10.100): " if not "!PLC_IP!"=="" ( echo Testing connection to PLC... ping -n 4 !PLC_IP! echo. echo Testing S7 communication port (102)... powershell -Command "& {Test-NetConnection -ComputerName !PLC_IP! -Port 102 -InformationLevel Detailed}" ) ) echo. echo ======================================== echo Script finished! echo ======================================== echo. pause