I have a big batch file with PING and Iperf tests and it works, everything is written in a .txt file, I only want to see what is going on in the PING test for example, apparently >> command only writes to the text file. My solution is to send one time the PING without the >> to write to the file and another one with the >> to write to the file but this takes a lot of time for the purpose of the batch file.
Can anyone help me with a simpler solution?
thanks
here is part of the code:
ECHO.
(
ECHO Test started on %DATE% %TIME%
C:\Windows\System32\ping.exe %SERVER% | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
echo.
echo TEST de PING OK ! next test iPERF
) else (
echo TEST de PING NOK
ECHO Done
PAUSE
EXIT
)
) >> "%LOGFILE%.client.log"
There is a special device con, which is essentially your command line window.
(
echo this goes to file
>con echo this goes to screen explicitely
echo this goes to file too
)>file.log
im making an script to ping different websites. I want to read addresses from a file, ping them one time, and write the results on another text file.
echo off
cls
echo "TEST" > %cd%\out.txt
time >> %cd%\out.txt
ping -n 1 google.com>> %cd%\out.txt
pause>null
I dont know how to make the loop for pinging and also, how to read line by line from the file. Can anyone help me?
Thanks.
Something along the lines of the following should be what you want:
#echo off& setlocal
echo google.com>sites.txt
echo yahoo.com>>sites.txt
set cd=.
for /f %%w in (sites.txt) do call :NextSite %%w
exit /b
:NextSite
echo. >>%cd%\out.txt
echo. >>%cd%\out.txt
echo. | time | find "current" >>%cd%\out.txt
ping -n 1 %1 >>%cd%\out.txt
The logic starts at the line with the "for" statement; the preceding lines merely set up a test environment. You will have your own %cd% setting and filename to replace "sites.txt"
I have a script that simply pings a box, and if the box replies, it changes a variable called "newstate" to up. Otherwise it gets set to "down". For some reason, when I run it on my machine, newstate never gets changed to "up" even though it's online and replies to pings.
I'm probably missing something very small, can you please help? (I also put in a variable to log status when it changes, but I didn't implement it yet because I can't get the switching to even be recognized). Ultimate goal is to log when status changes from up to down or vice versa.
EDIT - Changed %newstate% to !newstate!, still not working.
#setlocal enableextensions enabledelayedexpansion
#echo off
title loop test
set system=192.168.0.155
set oldstate=up
set newstate=down
:loop
echo.
echo ...........
echo pre check
echo old state is %oldstate%
echo new state is !newstate!
echo ...........
echo.
:::::: CONNECTIVITY CHECK ::::::
for /f "tokens=5,7" %%a in ('ping -n 1 -4 !system!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set newstate=up
)
echo new state is !newstate!
if !newstate! neq up set newstate=down
set log=%time% - %sytem% - !newstate!
if %oldstate% neq !newstate! set oldstate=!newstate!
echo.
echo ...........
echo post check
echo old state is %oldstate%
echo new state is !newstate!
echo ...........
echo.
:::::: DELAY LOOP ::::::
ping -n 10 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
Well , it worked for me, but since i work with a spanish copy of windows i had to change the Received literal and use recibidos. Not sure about your local configuration.
It is usual better to check for the presence of the TTL= value into the ping response. If it is found, the target is up, else, it is down
ping -n 1 192.168.0.155 | find "TTL=" >nul
if errorlevel 1 (
set "newstate=down"
) else (
set "newstate=up"
)
So, with a little simplification,
#echo off
setlocal enableextensions disabledelayedexpansion
title loop test
set "system=192.168.1.1"
set "oldState=down"
set "newState=up"
:loop
ping -4 -n 1 %system% | find "TTL=" >nul && set "newState=up" || set "newState=down"
if %newState%==%oldState% (
echo %time% - %system% still %oldState%
) else (
echo %time% - %system% goes %newState%
set "oldState=%newState%"
)
ping -n 10 127.0.0.1 >nul 2>nul
goto loop
Where the ping line should be read as "ping , find ttl= in the output, if found set newState to up, else set newstate to down"
EDITED -
This was the usual way of testing for ping response. In IPv4. But when pinging in IPv6, the TTL field is not shown, so we can not check it.
The original reason for testing the TTL= in the output of the ping command was the way packet counts and errorlevels are handled. If a packet is lost, errorlevel is set. If no packet lost, no errorlevel. But, pinging a non active machine in the same subnet generates a 'unreachable' response and no packet lost, so, no errorlevel and packet count reflects 0 packet lost. And, ...
Also, almost all the information in the ping output is localized, making it difficult to check the result of the ping command and make it consistent across different OS localizations.
But some things have changed in IPv6 ping. If requested machine is not reachable, packets are lost and errorlevel is set. In same subnet or not.
And, if some packets are lost, but at least one packet is not lost, errorlevel is not set.
So, when working with IPv4, the test for TTL= presence in ping outout is a "reliable" way to check the state of the remote machine.
And, when working with IPv6, errorlevel reflects the success of failure of the process.
And, please, note that i'm not a network guru. This is what i have seen when testing. If i'm wrong, please, comment.
Use !newstate! to get variable value, instead of %newstate%. This is the way of getting delayed variables values.
Keep using "setlocal enabledelayedexpansion".
The problem is in this statement
if "x%%a"=="xReceived" if "x%%b"=="x1," set newstate=up
which means if firstcondiion and secondcondition set ...
So - i'd insert a debug line
echo +%%a+%%b+
just before this if statement, and observe results. The + is just there as an obvious character to make seeing Spaces easier.
Remember - if is case-sensitive, so if you need case-insensitive, use if /i
Also - observe Spaces as a quoted-string in an if incudes any spaces in the match.
There is no point in delayedexpansion in this instance, since you are not attempting to read a values that changes within (a parenthesised block of statements). Under these circumstances, %var%==!var!
I'm pretty new to this forum so i first want to thank you for providing me with solutions even before i became a member :).
So I have this code:
for %%a in ("%PBpath%") do (
move "network location 1 files" "network location 2" >NUL
if ERRORLEVEL 0 (echo Diagram %%~na.pdf was successfuly archived) else ( echo Diagram %%~na.pdf was not archived )
ECHO.%errorlevel%
)
The problem is that I can't get the errorlevel different than 0. Even when the files that are to be copied are missing from location, i still get the successfuly archived message echoed. I searched the forum for similar questions, but i couldn't make it work for some reason.
Is there something different between the copy and the ping command (the ping command returns the correct exit code in the errorlevel), because i can't get it with either copy or move...
Thanks!
Andrew
The strange thing about the IF ERRORLEVEL statement is that it doesn't act like you expect- it returns TRUE if the errorlevel is equal to OR GREATER THAN the number specified.
A failure in MOVE sets errorlevel to 1 (I just checked) which is greater than 0. Therefore the first clause in the IF statement will always be used. The easiest way to fix your script is to reverse the conditions in the IF statement:
if ERRORLEVEL 1 (echo file was not archived) else (echo file was successfully archived)
Just use %ERRORLEVEL% variable instead of ERRORLEVEL function
If one wants to use the ERRORLEVEL function, Superbob's answer address' this (though I would recommend the form if NOT ERRORLEVEL 1 (echo file was successfully archived) else (echo file was not archived) instead).
But if one wants to use the %ERRORLEVEL% variable method instead, Delayed Expansion needs to be turned on. The OP's code above, with the recommended changes, is below:
setlocal enabledelayedexpansion
for %%a in ("%PBpath%") do (
move "network location 1 files" "network location 2" >NUL
if !ERRORLEVEL! equ 0 (
echo Diagram %%~na.pdf was successfully archived
) else (
echo Diagram %%~na.pdf was not archived)
)
I am using below batch file to query a list of IPs and then save it to LOG.txt.
#echo off
cls
for /f "tokens=*" %%x in (IP.txt) do (
echo Checking %%x
ping -n 1 %%x > nul
if not errorlevel 1 (
echo %%x >> LOG.txt
)
)
But I am seeing only first enrty of IP.txt in LOG.txt
Although while running batch file I am seeing
Checking 1.2.3.4
Checking 1.2.3.5
etc..
So its implying that batch file is reading the IP.txt line by line.
Can anyone help to make this batch file such that output in LOG.txt is working as expected.
What Andriy M is alluding to in his comment is your code will only write the IP address if PING was successful.
Because of your IF statement, the IP address will not be written if there was an error. PING will generate an error if there was a timeout, or if PING could not find the host.
You need to change your logic if you want all addresses to be written.