new here and to batch files - Ping issues

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dabe1
Posts: 1
Joined: 20 Dec 2012 09:32

new here and to batch files - Ping issues

#1 Post by dabe1 » 20 Dec 2012 09:38

Im trying to create a batch file that will ping an IP and write fails to a file with a timestamp. I have one that works on my win 7 machine, but when I try running it on server 2003 it writes all replys to the file...any suggestions on this?

@setlocal enabledelayedexpansion
@echo Input IP to ping
@set /p hostIP=
@echo Input Logfile name
@set /p logname=
@Title "%logname%"
@echo Showing Failed Pings to %hostIP% >> "%logname%".txt
@echo %date% %time% >> "%logname%".txt
:loop
set pingline=1
for /f "delims=" %%A in ('ping -n 1 %hostIP%') do (
if !pingline! equ 2 (
set logline=!date! !time! "%%A"
echo !logline! | find "TTL=">nul || (
set logline=!logline:"=!
echo !logline! >> "%logname%".txt
)
)
set /a pingline+=1
)
goto loop

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: new here and to batch files

#2 Post by foxidrive » 20 Dec 2012 15:08

Try this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /p "hostIP=Input IP to ping: "
set /p "logname=Input Logfile name: "
Title "%logname%"
>>"%logname%".txt echo Showing Failed Pings to %hostIP%
>>"%logname%".txt echo %date% %time%
:loop
ping -n 1 %hostIP% || >>"%logname%".txt echo !date! !time! - error
goto loop


or this if you want the same type of display in the log file:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /p "hostIP=Input IP to ping: "
set /p "logname=Input Logfile name: "
Title "%logname%"
>>"%logname%".txt echo Showing Failed Pings to %hostIP%
>>"%logname%".txt echo %date% %time%

:loop
for /f "skip=2 delims=" %%A in ('ping -n 1 %hostIP%') do (
set "logline=!date! !time! %%A"
set "var=!logline:TTL=!"
if "!var!" EQU "!logline!" echo !logline! >> "%logname%".txt
echo "!logline!"
goto loop
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: new here and to batch files - Ping issues

#3 Post by foxidrive » 20 Dec 2012 15:53

If sever 2003 still plays up then paste a command line like this, and the output from Server 2003, in a reply:

Code: Select all

d:\ABC>ping localhost -n 1

Pinging 192.168.1.103 with 32 bytes of data:
Reply from 192.168.1.103: bytes=32 time<1ms TTL=128

Ping statistics for 192.168.1.103:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

d:\ABC>

Post Reply