Batch file not saving result in logs - batch-file

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.

Related

In a batch file how to write to a file and see also what is going on with a ping test?

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

Read webs from a file and ping them with a loop (?)

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"

How to parse text from an output in Batch

So i was tasked to a match a batch script that would ping an ip then print if a response was there or not and what I am attempting to do is to first ping, then save it on a text file,then use an if statement so if the string contains the text Request was timed out it would display that the IP is unavailable.
This is my example code
#echo off
cls
echo Executing ping....
ping 123.456.789 >> output.txt
pause
echo Here are the results:
findstr "Request" output.txt
if %finstr% == "Request" {
echo IP does exist
}
else {
echo IP does exist
}
I was trying to use findstr to get the string but that would involve saving the string and comparing it but I have no idea how to do that. You get the jist of it. Thanks and all help is appreciated very much.
Post scriptum: the if statement is what i think it looks like since i don't ever do batch programming i am a newbie
I'd suggest that there's a far better approach; ERRORLEVEL.
#echo off
ping -n 1 192.168.1.1 > NUL
if ERRORLEVEL 1 (
echo Host is down.
) else (
echo Host is up.
)
If ping succeeds, ERRORLEVEL will be zero. If it fails, ERRORLEVEL will be 1 (or possibly greater if it fails for other reasons, I suppose... if ERRORLEVEL 1 tests for ERRORLEVEL >= 1).

redirecting .exe output within batch script to txt file

i am trying to redirect output from an exe(commanline exe) which is being called in from batch file to log file. In this script IP addresses of hostnames provided in input.txt are being redirected to result.txt. i am trying to run .exe within same batch script to place those IPs in maintenance mode in my monitoring tool. Script runs fine and performs the action as expected but it fails to capture the output from .exe. please help.
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (input.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo !SERVER_ADDRESS::=!>>%OUTPUT_FILE%
)
start c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"
Output from .exe if i run it directly from command prompt:
PS C:\> .\MaintenanceMode.exe ON C:\result.txt username
Not an IP!!
Reading IPs from File: C:\result.txt
Valid Arguments
System put into MM Host# 10.*.*.* Status# Success
System put into MM Host# 10.*.*.* Status# Success
You are redirecting the output of the START command, but not the exe.
If you want to use START and redirect the output, then you most execute a new CMD.EXE session and escape the redirection so it occurs within the new session:
start cmd /c c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% ^>^> "c:\result2.txt"
But why are you using START? It would be so much simpler if you simply execute your exe directly:
c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"

How do I skip a computer in a batch file that is unreachable?

#echo off
:begin
for /f %%a in (computerlist.txt) do (
setlocal
psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
endlocal
)
when the script is running, when it finds a machine to be unreachable I want it to skip it.
How do i write the script to skip an unreachable computer and continue to the next one in the txt file?
How about a simple test to see whether a machine is pingable?
#echo off
setlocal
:begin
for /f %%a in (computerlist.txt) do (
ping -n 1 %%a >NUL 2>NUL
if %errorlevel%==0 (
psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
) else echo Skipping unreachable host %%a
)
endlocal
Actually, windows ping.exe doesn't return meaningful errorlevels, (huh?)
It ALWAYS returns a "0" errorlevel, unless the IP-Protocol stack itself is hosed.
WHAT YOU HAVE TO DO IS: pipe the output of your ping to a txt-file, and "find" for TTL=
ie
ping 10.10.10.10. >pingtest.txt
findstr /C:"TTL=" pingtest.txt >nul
if %ERRORLEVEL% equ 0 (do whatever) else (echo skipping unreachable host "whatever")
Then use Find.exe or Findstr.exe to look for TTL= in your output file
(note find, and findstr both use "0" errorlevel when they "find" what your searched on)
1. Ping can fail for a whole lot of reasons, but "TTL=" is always part of a successful ping
2. I always ping at least -n 3 times, because occasionally the first one or two might have problems, and I want to give it more possible chances to succeed before I skip it.
3. For this to work, you have to use the FOR loop method above, just raw psexec.exe has not current means to test/skip targets called from a text file.
4. If you need to use network resource in your psexec session then you need to run the "-h" option so that you get the elevated token, which allows you to map drives
I HOPE THIS HELPS !

Resources