Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:

ping www.google.de -t

and displays the ping, so it would look like this:

Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms

My problem is, that this will result in this when I execute this command as script:

Screenshot showing output on running the batch file.

My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.

I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.

So how to write the batch file to start this command only once and display the ping result?

like image 939
Peter Avatar asked Aug 03 '14 17:08

Peter


People also ask

How do I only ping once?

How can I execute ping command only for N number of packets and terminate the output automatically? Answer: Use ping option -c to specify the number of packets. After sending N number of packets, ping command will terminate automatically as explained below.


2 Answers

Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:

  • ping - latest Microsoft documentation for this Windows command
  • ping - Windows XP documentation for this Windows command

Explanation for option -t given by Microsoft:

Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.

You may want to use:

@%SystemRoot%\system32\ping.exe -n 1 www.google.de

Or to check first if a server is available:

@echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer

echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF

:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
like image 173
Mofi Avatar answered Oct 19 '22 12:10

Mofi


I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.

my batch file contains below code only

ping 172.31.29.1 -t

with file name as ping.bat enter image description here

with file name abc.bat

enter image description here

like image 31
Dhirendra Khanka Avatar answered Oct 19 '22 13:10

Dhirendra Khanka