简体   繁体   中英

make a batch script foward an IP

Hi so my coworker requested if I could make a script. I am sure what I want it to do and wrote some pseudo code in bash style now of course this not useable for him since he is on Windows. So I tried to implement in a.bat script now here is where my knowledge comes a bit short. What I need the script to do is to connect to a certain VPN-ip if that is not avaible the localsystem should foward it to another VPN so he doesn't need to worry about it. Either one of 2 should always be reachable. But they are never at the same time. This is for test tooling.

Pseudo bashcode

    while true 
  do
  From local if 
    10.10.1.15 avaible connect to it 
    else
    10.168.84.47 connect to it
  elseif
   try to connect to 10.10.1.15 again && verify that'
                else 
                    echo 'error device over VPN unavaible'  

My Attempted batch script, I am pretty sure that what I have now is not gonna work

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
    if "x%%b"=="xunreachable." goto :endloop
    if "x%%a"=="xReceived" if "x%%c"=="x1,"  set state=up
)
:endloop
echo.Link is !state!
ping -n 6 10.10.1.15 >nul: 2>nul:
goto :loop
endlocal


IF EXIST 10.10.1.15 (
    is reacheable connect
) ELSE (
    netsh interface portproxy add v4tov4 listenport=80 connectaddress=10.10.1.15 fowardaddress=10.168.84.47
)

So far as I know, you can just try to find "ttl=" to determine success/fail for a ping without worrying about all those tokens and different versions of cmd. I'm sure someone will correct me if I'm wrong.

untested

set ip=127.0.0.1

rem 2 pings waiting 900ms for a reply
ping -n 2 -w 900 %ip%|find /i "ttl=">nul || goto :fail

rem If we get here then the ping succeeded
rem do what you want here

goto :eof

:fail
rem If we get here then the ping failed.
rem do what you want here

goto :eof

The || operator basically means "if the previous command fails then do this".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM