How to ping a server only once from within a batch file? - 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:
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?

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
with file name abc.bat

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

For bash (OSX) ping google.com -c 1 (incase search brought you here)

if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)

Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error

I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!

From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1

i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4

The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t

Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".

Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).

The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address

Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

Related

CMD Command only works when entered manually but not when used in file

I wanted to use
ping -n 1 8.8.8.8 | findstr /r /c:"[0-9] *ms"
to check if a internet connection is possible, before going forward in my code. But it does not work, when used in a file.
When i enter this command in a new command prompt manually it works as expected. When it is used in a file it gets 'kinda stuck' at this line and does nothing, as if it would wait for something that never happens. Any ideas on what this problem is and/or how to approach it? Thanks
I would advise for this task that the first line of your batch-file looks like this:
#%SystemRoot%\System32\ping.exe -n 1 8.8.8.8 1>NUL || GoTo :EOF
This should end the script at the first line if the ping command line is unsuccessful.
You may additionally want to consider using a timeout function. If you open a Command Prompt window, type ping /? and press the ENTER key, you should note that it has a -w option, which will accept a parameter in milliseconds.

Unable to launch multiple programs via a batch file

Am trying to create a batch file, that would launch multiple programs. But unfortunately, things don't seem to work.
Kindly, find below my requirement:
Open InfluxDB server
Launch Grafana application.
Commands used in the batch:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
Start.cmd
timeout 5
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
grafana-server.exe
The above script, launches InfluxDB. But doesn't moves further.
Could you please suggest me, on how to proceed?
You need to use the call keyword to have control returned to the caller after invoking another batch script:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
call start.cmd
...
Should start.cmd run InfluxDB synchronously (i.e. not in the background) you need to launch it in a separate window:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start "InfluxDB" cmd /c start.cmd
...
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start InfluxDB
ping -n 6 127.0.0.1 > nul
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
start grafana-server
Edit the "start InfluxDB" and "start grafana-server" to be the correct exe names, without .exe

Batch file ping once every minute

I got this bat file from Stackoverflow that someone had posted back in 2014 but it will not output any info to the filename?
The file is created but without any info.....
can anyone please help, trying to record the ping output every 1 minute on a windows 7 machine.
#ECHO off
set IPADDRESS=192.168.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1 >> filename.txt
timeout %INTERVAL%
GOTO PINGINTERVAL
Your batch file is called ping.bat or ping.cmd and is calling itself. Rename the batch file or replace ping %IPADDRESS% ... with ping.exe %IPADDRESS% ...
The "echo off" is ok. This will only omit output from the script, not from the "ping" app.
I think the output is created in an unexpected location. Add the following lines at the beginning of the script to verify the current folder.
#echo %cd%
pause
Or simply specify the full path like so:
>> d:\fullpath\filename.txt

Batch file for PuTTY/PSFTP file transfer automation

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.
cd C:\Program Files (x86)\PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:\
put log.sh
bye
The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.
You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:
cd "C:\Program Files (x86)\PuTTY"
psftp -b "C:\path\to\script\script.txt"
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b
EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>#<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:
cd "C:\Program Files (x86)\PuTTY"
psftp -b script.txt <user>#<IP> -pw <PW>
And remove the open, <user> and <PW> lines from your script.txt.
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw
What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.
If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:
(
echo cd ...
echo lcd ...
echo put log.sh
) | psftp <user>#<IP> -pw <PW>
Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.
set DSKTOPDIR="D:\test"
set IPADDRESS="23.23.3.23"
>%DSKTOPDIR%\script.ftp ECHO cd %PAY_REP%
>>%DSKTOPDIR%\script.ftp ECHO mget *.report
>>%DSKTOPDIR%\script.ftp ECHO bye
:: run PSFTP Commands
psftp <domain>#%IPADDRESS% -b %DSKTOPDIR%\script.ftp
Set values using set commands before above lines.
I believe this helps you.
Referre psfpt setup for below link https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter6.html

dos batch script using psexec shows 'help' text every time it loops

Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.
The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have #echo enabled, not that it would cause this anyway.
hoping for a quick fix, but if my code is needed to get an answer ill post it.
Posting the code anyway...
#echo on
setlocal EnableDelayedExpansion
set /p ipAddress="enter ip address: "
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul
if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)
endlocal
deploy_mir.bat code
#ECHO OFF
echo "Mir Agent deployment to: %1"
rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"
copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete
rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat
Desired cmd line result of running deploy_mir.bat
C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
t: was deleted successfully.
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.
C:\DOCUME~1\socuser2\MIR>
Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:
My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.
Code Specific Notes:
#echo is enabled, but you say it is not in your question.
!errorlevel! == 0 should be !errorlevel! EQU 0
Some General Notes:
In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.
I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.
psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat

Resources