Test for internet intermittency - windows

I was wondering if there is a way to write a cmd script or something for Windows that will allow me to tell when my internet connection has severed.
In Ubuntu, I would have written a shell script to ping Google once per second and if it is impossible record the time etc etc.
Is it possible to do a similar thing in Windows?

There may be a better way to monitor the connection status. But here is a Windows batch implementation of the logic you specified.
#echo off
setlocal enableDelayedExpansion
set log="internetStatus.txt"
set "status="
>>%log% echo %date% %time%: Begin monitoring
for /l %%A in () do (
>nul ping -n 2 -w 1000 google.com && (
if "%status%"=="FAIL" (
>>%log% echo %date% %time%: internet connection succeeded
set "status=OK"
)
) || (
if "%status%"=="OK" (
>>%log% echo %date% %time%: Internet connection to google.com failed
set "status=FAIL"
)
)
)

Related

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".

Cannot use PStool for more than 7 PCs over LAN Network

Below is the batch file command I am currently using to shutdown the remote computers through LAN Network.
Here is what it does.
I have specified the remote computers IP address in text file named
list.txt
I have added an IP as 0.0.0.0 at the bottom of all the remote
computer IPs.
The below batch file will check if the computers are available over
LAN.
If the computer is available it will shutdown the remote PC else it
will pass on to next IP.
When the batch file reads 0.0.0.0 at last it will self shutdown the
master computer.
My problem is I cannot run this batch for more than 7 remote computers. If I add more than 7 remote PC IP in list.txt the batch file hangs and action does not complete. Please let me know if i made any mistake in the code or How i can fix this issue.
I want to run this batch file for minimum of 12 remote PCs
#echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in (C:\Users\calcopm\Desktop\list.txt) do (
SET IP =%%a
SET C=0
IF %%a equ 0.0.0.0 (
shutdown /s
) ELSE (
ping -n 1 %%a | find "TTL=" >NUL: && SET C=1
IF !C! equ 1 (
psshutdown \\%%a
) else (
ECHO REMOTE %%a IS NOT REACHABLE
)
)
)
I changed my script as below and converted from BAT to EXE using an application
#echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in (C:\Users\calcopm\Desktop\list.txt) do (
IF %%a equ 0.0.0.0 (
shutdown /s
) ELSE (
ping -n 1 -w 100
IF errorlevel 1 (
ECHO REMOTE %%a IS NOT REACHABLE
) else (
psshutdown \\%%a
)
)
)
Still I was facing the same problem. As I was running the scripts using exe file(converted using BAT to EXE), I executed using the BAT file it was fine.Then I realised that the BAt to EXE converter had some issues which was affecting the EXE file inturn.
Then I converted BAt to EXE with different application and IT WORKED LIKE A CHARM.
I solved the issue ATLAST!!!!!!!!!!!!! phew!!!!!

Computer script for auto-reboot on network loss

I am working with a IP Camera network and need a script to run on each individual PC that will perform an auto-reboot when network loss happens. I would like to be able to have the PC ping the servers IP every 5 minutes and upon loss of connectivity the PC will reboot. Each PC has a Camera viewer but periodically looses network connection with the NVR. I found almost the same issue/solution here: http://www.cam-it.org/index.php?topic=2786.0
However the script provided didn't work for me. Below is the script I found and tried but didn't function the way I needed.
#Echo off
REM Put REM in front of Echo off to view the file output
REM ---------------------------------------------------------
REM WATCHDOG.CMD
REM Restarts PC after 3 unsuccessful attempts to PING the
REM POE switch
REM --------------------------------------------------------
SET COUNT=C:\Temp\WATCHDOG.txt
SET POESWITCH=192.168.1.253
SET ERRFLG=0
IF EXIST "%COUNT%" (
SET /P ERRFLG= <%COUNT%
)
IF %ERRFLG% GTR 2 (
Echo Restarting PC in 60 seconds. Run SHUTDOWN -a to abort.
DEL %COUNT%
SHUTDOWN -r -t 60 -f
GOTO :EOF
)
PING -n 1 %POESWITCH%|findstr /I /C:"timed out" /C:"unreachable" /C:"general failure"
if %ERRORLEVEL% == 1 Goto Done
SET /a ERRFLG +=1
ECHO %ERRFLG% > %COUNT%
:Done
(http://www.cam-it.org/index.php?topic=2786.0)
Any suggestions would be greatly appreciated.
Thanks,
Jordan
Add the reboot command and it should work for you to test a URL/IP address every 300 seconds
#echo off
set ip=www.google.com
:loop
ping -n 2 %ip% |find "TTL=" >nul || echo reboot command here
ping -n 300 localhost >nul
goto :loop

plink automated batch script

How would I create a simple batch script(windows) to close then current plink session if it times out and reconnect automatically?
something like this:
if "plink.exe" == "false" (
"plink command to connect to SSH Server."
)
or maybe
if "plink.exe" == "false" ( "batch to open new plink instance"
)
Here you go.
#echo off
setlocal
:: modify this line as appropriate
set plink_args=-P 22 -i c:\path\to\private.ppk user#host
set errors=0
:loop
:: if "find" exits with a non-zero status, plink.exe isn't running.
( tasklist /fi "IMAGENAME eq plink.exe" | find /i "plink.exe" >NUL && (
set errors=0
) ) || (
start "" plink.exe %plink_args%
set /a "errors+=1"
)
if %errors% geq 5 (
echo Unable to connect %errors% times in a row. Stopping.
goto :EOF
)
:: pause for 10 seconds (-n seconds + 1)
ping -n 11 0.0.0.0 >NUL
goto loop
You know, if you have root access on the ssh server, you could modify sshd_config and have the server send no-op packets every few minutes to prevent connections from timing out due to inactivity. Here's an example snippet of my sshd_config:
# noop anti-idle for 12 hours (10 minutes * 72)
ClientAliveInterval 600
ClientAliveCountMax 72
Add that to your sshd_config and restart the ssh daemon. That might save you from having to do something so hackish on the client-side.

Script to start traceroute if continuous ping fails, output to log

I want to continuously ping my home public IP address, and if the ping fails automatically do a traceroute to see where it's failing.
I've been trying to follow the comments made here:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/efc97c66-60a6-4fd7-8be4-4b454d040ce5
Windows compatible would be preferable, bat or vbs would be best.
From anywhere on the internet I will lose my connection to my home network. From work I have started a ping and when it drops I've done a traceroute and it fails before it gets to my IP.
I need a log file to prove that it is not my modem, or router, or computer.
#echo off
set Address=google.com
:Loop
PING -n 5 127.0.0.1>nul
echo Pinging %Address%
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL >> C:\pingtest\logfile.log
if %ERRORLEVEL% EQU 0 goto :Loop
echo Trace route %Address% at %date% %time% >> C:\pingtest\logfile.log
tracert %Address% >> C:\pingtest\logfile.log
goto Loop
This is what I ended up going with, if anyone else ever needs this. Essentially the "Ping -n 127.0.0.1>Nul" is to add a 5 second counter so that it only pinged the destination every 5 seconds, 5 can be changed to whatever value is needed.
Windows 7 has this problem where a ping may result with something like "reply from 192.168.1.5: Destination host unreachable". So instead of erroring out it gets a reply from itself and not the error level 1.
Instead of looking for Error Level 1 I choose to look for no result for TTL with "%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL"
Anyway, I'm sure the other answers here were very similar and may have worked, so I am ranking them up, but marking this as the answer.
Thanks all!
#echo off
set Address=www.google.com
set LogDir=C:\pingtest
md %LogDir%
%SystemRoot%\explorer.exe "%LogDir%"
echo PingTest script to monitor network connection. Control-C to exit.
echo Tests connection by pinging %Address%. Logs to %LogDir%\logfile.log.
echo %date% %time% Initial tracert (trace route) to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
:Loop
REM 5 second delay
PING -n 5 -w 1 127.0.0.1>nul
echo %date% %time% Pinging %Address%
echo %date% %time% Pinging %Address% >> %LogDir%\logfile.log
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL
if %ERRORLEVEL% EQU 0 goto :Loop
echo %date% %time% PING ERROR - Tracing route to %Address%
echo %date% %time% PING ERROR - Tracing route to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
goto Loop
You could make a simple batch file that tries a ping and if it fails does a tracert, eg:
setlocal
set host=www.bigpond.com
set logfile=nettest.log
echo %date% %time%>>%logfile%
ping %host%>>%logfile%
if ERRORLEVEL 1 tracert %host%>>%logfile
endlocal
There's plenty of scope for refinement here.
Then create a scheduled task that runs it every five minutes or whatever suits you.
Alternatively you could include a loop with a 'sleep' in it. There's a poor man's sleep at Sleeping in a batch file that uses:
choice /d y /t 5 > nul
:LOOP
FOR /F "usebackq tokens=1" %%F IN (`ping localhost -n 1 -w 1 ^| find "Request"`) DO (
IF "%%F"=="Request" (
tracert localhost
)
)>>log.txt
FOR /F "usebackq tokens=1-4 delims=:." %%G IN (`echo %time%`) DO IF %G%H GTR 1400 GOTO:EOF
GOTO LOOP
Basically, this states do ping, if it finds a line that has an instance of the word Request (which only appears if you can't ping the address) perform a tracert. The -n and -w switches in PING tell it to jump only once and timeout after 1 second of not getting a response. This is perfectly fine if you are pinging your localhost. The second FOR statement is to have a stopping point. Change the 1400 to a time you wish for the script to stop (in military time of course).
I have just been looking for the same thing to investigate why a VPN keeps dropping on a wired connection, used one of the batch file suggestions above which was great.
Also found a nice little Java App which packages it for you here
Internet Connectivity Monitor
Simple to use and does the job :-)

Resources