我需要检查vpn连接时,是在线/使用批处理文件。
--实际上我使用了以下代码:
PING -n 5 www.google.com|FIND /I "TTL">NUL
IF NOT "%ERRORLEVEL%"=="1" (
Echo "Vpn Connection Is Up"
)
IF "%ERRORLEVEL%"=="1" (
Echo "Vpn Connection Is Down"
)有时ping给出一个非响应或一个一般的失败响应。
我想知道有一个比ping命令更好的批处理代码替代方案。
我尝试过此替代代码,但当联机/启动时不识别vpn,因为适配器总是在我的pc中安装。
第一备选代码:
ipconfig|find /i "VPN" && GOTO startAPP || GOTO connectVPN第二个备选代码:
ipconfig|find /i "VPN"
if %errorlevle%==1 goto:connectVPN
echo Starting APP !
exit/b
:connectVPN
echo connecting to VPN第三种代码:
:start
cls
rasdial | find /I "%vpnname%" > nul
if errorlevel 1 goto vpndown
if errorlevel 0 goto vpnup您能推荐一个替代的命令,让ping集成到我的批处理代码中吗?
当vpn连接在线时,可供选择的代码不识别,如果存在安装在我的pc上的附件,它就会识别,但我需要知道vpn何时联机/启动(准备在互联网上运行)。
发布于 2019-02-19 14:17:28
另一种方法是使用rasdial命令,它用于VPN连接。特别是,使用:
@echo off
rasdial | findstr /ic:"No" >nul
if not %errorlevel% EQU 0 (
echo Found a VPN connection!
goto :found
) else (
echo We didn't find any VPN connections!
goto :notfound
)
:found
rem Do something if a VPN connection is found/online:
:notfound
rem Do something if a VPN connection isn't found/online:发布于 2019-02-19 05:49:48
最好的方法是使用netsh wlan show networks。此命令将显示当前连接的网络适配器。我们可以使用find /I "VPN"来查看您的虚拟专用网目前是连接的。%ERRORLEVEL%将允许您提取是/否。
Is-VPN-Up.Bat:
netsh wlan show networks | FIND "VPN" /I /C >NUL
IF Not "%ERRORLEVEL%"=="1" (
Echo "Vpn Connection Is Up"
) ELSE (
Echo "Vpn Connection Is Down"
)发布于 2019-02-19 05:56:01
我认为,如果将powershell集成到批处理(或powershell本身)中,则可以查看连接是否已启动。
@echo off
for /f "tokens=2" %%i in ('powershell "Get-NetAdapter | select Name,Status" ^| findstr /i "Wifi"') do (
if /i "%%i"=="up" echo VPN %%i
if /i "%%i"=="down" echo VPN %%i
)你也可以让它点击google,首先,如果发现了问题,打开网关,如果有问题,检查连接,几乎如下所示:
@echo off
:start
ping -n 2 www.google.com | findstr /i "ttl">nul
if not "%errorlevel%"=="0" (
echo "Oops something is wrong, let's test gateway"
goto testgate
) else (
echo "Internet Still reachable"
timeout 5 /nobreak>nul
goto start
)
:testgate
ping -n 2 10.3.1.1 | findstr /i "ttl">nul
if not "%errorlevel%"=="0" (
echo "Hmm, gateway also not reachable, will test VPN"
for /f "tokens=2" %%i in ('powershell "Get-NetAdapter | select Name,Status" ^| findstr /i "VPN"') do (
if /i "%%i"=="up" (
echo "Adapter connection %%i, will retry internet ping now"
goto start
) else (
echo Adapter connection Down or Disconnected
echo let's start something up
pause
goto start
goto :eof
)
)
) else (
echo "Gateway Connection Is Up"
goto start
)https://stackoverflow.com/questions/54756365
复制相似问题