07-23-2023, 11:13 AM
I have a lot of internet connection downtimes recently. So I'd like to make a list of each time I had issues to send it to my ISP. In order to make it a bit less tedious, I figured out, it would be nice to make a kind of report and send it in as a log file. I stumbled upon a few solutions, but the best I found was on this site:
I have adjusted this code a bit, but I ran into one issue. If I try using `if not %errorlevel% 0 do (...)`, `if %errorlevel% 1 do (...)` and try to run .bat file, Windows command processor window just pops up and disappears. Without this condition the batch file runs just fine. But it does not do what it is made for. I want to make 2 files, one with plain ping log, the other one only with timeouts and *destination host not reachable* timestamps. It's my first time scripting for Windows. I am used to code in C++ back in the days. Instead of host_ip I got IP address of first connection to the outside network.
@echo off
set host=host_ip
set logfile=Ping_test.log
set logfile_fail=Ping_test_fail.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A>>%logfile_fail%)
echo %errorlevel% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)
__EDIT:__
Thank you for helpful advices. I have tried to implement them as well as continue searching for solution on my own. And after a lot of time and frustration it is done. I had to change a few lines (included them below) according to how `for /F` command instructions are executed (in another command process). But in the end I got my arguably first actually useful bit of code done and to be honest it made me realize how useful batch scripting is. ;)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ^& call echo %%^^errorlevel%%^>error.level') do (
set /p elv=<"error.level"
if not !elv!==0 (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile_fail%)
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo elv %elv% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)
So once again
**Huge thanks for helping me out!**
[To see links please register here]
by Joost Kuin.I have adjusted this code a bit, but I ran into one issue. If I try using `if not %errorlevel% 0 do (...)`, `if %errorlevel% 1 do (...)` and try to run .bat file, Windows command processor window just pops up and disappears. Without this condition the batch file runs just fine. But it does not do what it is made for. I want to make 2 files, one with plain ping log, the other one only with timeouts and *destination host not reachable* timestamps. It's my first time scripting for Windows. I am used to code in C++ back in the days. Instead of host_ip I got IP address of first connection to the outside network.
@echo off
set host=host_ip
set logfile=Ping_test.log
set logfile_fail=Ping_test_fail.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A>>%logfile_fail%)
echo %errorlevel% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)
__EDIT:__
Thank you for helpful advices. I have tried to implement them as well as continue searching for solution on my own. And after a lot of time and frustration it is done. I had to change a few lines (included them below) according to how `for /F` command instructions are executed (in another command process). But in the end I got my arguably first actually useful bit of code done and to be honest it made me realize how useful batch scripting is. ;)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ^& call echo %%^^errorlevel%%^>error.level') do (
set /p elv=<"error.level"
if not !elv!==0 (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile_fail%)
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo elv %elv% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)
So once again
**Huge thanks for helping me out!**