Discussion Locked

Batch file

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How would i create a batch file that would ping servers and output the results to a log file

10 replies

by syscokid

Do this:

ping servername >logfile.txt

This will overwrite the contents of logfile.txt. If you want to append the results rather than overwrite, do this:

ping servername >>logfile.txt

If you craete the file logfile.txt ahead of time in Notepad, and put the following at the beginning:

.LOG

then every time you run the batch file, it will append the date and time after each output.

Hope this helps!

by syscokid

I just saw your additional comment. OK, to ping multiple servers, just keep listing them one after the other in the batch file:

ping server1 >>logfile.txt
ping server2 >>logfile.txt
ping server3 >>logfile.txt
ping server4 >>logfile.txt
etc.
In this case you MUST use >> to append each line to the file instead of >, otherwise each ping will overwrite the results of the previous ping in the log. When it was done, all that would be in the log would be the result of the last ping.

by huckleberryhound

Building on Answer #1 you could automate this by creating a one line batch file with just the ping command as listed above then use the Task Scheduler (located in the Control Panel) to run at regular intervals.

by tbragsda

How about this. Two files. One with all devices, and another that does the work. A output log file can be made, or this could be much more simple if output is only to screen.

HostTest.bat
::
:: Calls HostPing.bat for each Host, and test onlyif Y is in second col.
::
:: HOST Y/N Disc

If Exist HostLog.txt Del HostLog.txt
call HostPing.bat 10.1.3.30 Y VK router
call HostPing.bat 10.1.3.2 N NW Server
call HostPing.bat www.novell.com Y External server
call HostPing.bat InternalName Y External server

HostPing.bat
::
:: This is the 'working' batch file. It will run the Ping test for each
:: call from HostTest.bat that was set with Y for VAR %2
::

If %2==N goto Exit
Echo Testing - %1 >> HostLog.txt
Ping %1 | find /i "Packets" > HostTmp.txt
if errorlevel = 1 echo No Response from %1 >> HostLog.txt
type HostTmp.txt >> HostLog.txt
Echo. >> HostLog.txt
del HostTmp.txt

:Exit
Testing - 10.1.3.30
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Testing - www.novell.com
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Testing - InternalName
No Response from InternalName

by tbragsda

Lost all the tabs. It is much easyer to read, and motify with some tabs.

Also the business with the tmp file is only to make the log only have one line with some realy usable stats. You could choose to omit that sutff and the log would just be messey.

Whats the end goal anywho? Want it to page, send email on error? Or is the log enough?

This discussion is closed to new replies.