Batch- if command and “check internet connection”

 ̄綄美尐妖づ 提交于 2019-12-12 04:23:26

问题


I have problems with my router when downloading through steam and other programs, such as the internet losing connection with the router. I cant plug my pc to my router using cable so i made a solution: and the other one \Every X secounds disconect and connect to internet

But the problem is i want to make it more efficient so i would like a command that does this:

:a \Check connection \if connected then goto a \if noconnection disconect and connect to internet

i get problems with check connection command as its does not move on to the if

Please help and thanks for your time


回答1:


This will ping www.google.com and if there is a response then goto :a, if not connected then it will goto :Disconnected.

The findstr will look for the TTL (Time to live) in the ping output. Great little trick that can be applied to multiple situations.

ping -n 1 www.google.com | findstr TTL && goto a
ping -n 1 www.google.com | findstr TTL || goto Disconnected

:a
REM Your connected script here

:Disconnected
REM Your disconnect / reconnect script here

You could also condense this to the below. As the script will continue if it does not findstr TTL or skip to :a if it does

ping -n 1 www.google.com | findstr TTL && goto a
REM Your disconnect / reconnect script here

:a
REM Your connected script here



回答2:


This is what I made:

@echo off 
:b
cls
:a
color 0A
ping -n 1 192.168.0.1 | findstr TTL && goto a
ping -n 1 192.168.0.1 | findstr TTL || goto Disconnected

:Disconnected
color 0C 
echo.
echo        //_No_internet_connection_\\ 
echo.
echo              //_Disconnecting_\\
netsh wlan disconnect
timeout /t 1
netsh wlan connect virginmedia8960851
echo.
echo             //_Connecting_\\
echo.
timeout /t 6
goto b


来源:https://stackoverflow.com/questions/38086401/batch-if-command-and-check-internet-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!