Batch ping website and get ip - batch-file

I would like to get the ip address from a site (eg. google.com gives me 173.194.66.103), and i do this by pinging the website.
For /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com^|find "Pinging"') do set ip=%%a
pause
This works greatly in CMD, but when i type it in a batch file it doesn't load. And when i try to close the window i get the error:
"The process tried to write to a nonexistent pipe.
^C^C^C^C^C^C^C^C> was unexpected at this time.
'a' is not recognized as an internal or external command, operable program or batch file"
I believe it is the ping part, which gives the same error in batch when run seperately.
ping -n 1 www.google.com

You code can be simplified to
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com') do set "ip=%%a"
There is no need for the find as the only line that will have a second token is the one with the square brackets.
But the problem persits. Why does it generate the indicated error?
The usual response is Your batch file is named ping.cmd, ping.bat, so, when you call ping from your batch file, the batch file is calling itself (or in your case and original code this will also happen with find.cmd or find.bat).
Rename the batch file (recomended) or change the command to call ping.exe (the same for find.exe) to avoid conflicts.

Try something like that :
#echo off
Title Getting The IP address
Setlocal EnableDelayedExpansion
set myServer=www.google.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
if "%%b" NEQ "" set ip=%%b
)
echo The IP address of !myServer! is !ip!
EndLocal
Pause

Related

Batch file that can read and process its own output in realtime?

I have a batch file which runs a python script. The python script after completion sends a number like shown below:
I need my batch file to read its own output as it runs and then use this (the number 3 shown in figure below to do some processing
Is this possible with batch?
EDITED FOR CLARITY:
I want to have a script which uses "net use" command.
The output from this specific command is as shown below... which will be printed to cmd window in which script is running ! Now I want my script to read this output ..like if a certain address is found in the already mapped drives list it will do something..like unmap the drive
I don't understand quite well what your purpose is, what has to do python to do with capture the net use command output?
As a rule of thumb, to capture any command output, you must use a for loop (i.e. here capturing clipboard content using powershell
rem get clipboard content into variable
set "psCmd=powershell -Command "add-type -an system.windows.forms; [System.Windows.Forms.Clipboard]::GetText()""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "clipContent=%%#"
So, in order to grab the output of net use
#echo off
for /f "skip=6 tokens=2,3" %%a in ('net use') do (
echo/%%a | find ":">NUL && echo/Drive letter %%a - Remote name %%b
)
exit/B
But, I suggest that you better use wmic (following two are similar)
#echo off
for /f "useback skip=1 tokens=1,2" %%a in (`"wmic path Win32_LogicalDisk Where DriveType="4" get DeviceID, ProviderName"`) do (
echo/%%a | find ":">NUL && echo/Drive letter %%a - Remote name %%b
)
exit/B
or
#echo off
for /f "useback skip=1 tokens=1,2" %%a in (`"wmic path Win32_MappedLogicalDisk get DeviceID, ProviderName"`) do (
echo/%%a | find ":">NUL && echo/Drive letter %%a - Remote name %%b
)
exit/B
Since, you may add the /node:"some_other_computer_name" switch to wmic and may list mapped drives from any computer on the net, i.e
#echo off
for /f "useback skip=1 tokens=1,2" %%a in (`"wmic /node:"hp-ml110" path Win32_MappedLogicalDisk Where DriveType="4" get DeviceID, ProviderName"`) do (
echo/%%a | find ":">NUL && echo/Drive letter %%a - Remote name %%b
)
exit/B
Hope it helps.

empty log file when running batch ftp?

I'm running a batch file that runs a ftp connection.
Everything is working;
my only problem is that I want to see the result of the ftp session.
I have done this:
#echo off
(
for /f "usebackq delims=" %%a in ("IP.txt") do (
ping -n 1 %%a |find "TTL=" >nul
if errorlevel 1 (echo %%a failed) else (echo %%a passed
start c:\ftp -s:test.txt %aa)
)>%%a.txt
)>output.log
but this only creates me an empty txt file.
If I do the same command not in a batch file - it's working
What could be the problem?
Thanks

is not recognized as an internal or external command

I want to run a batch file but I have a problem. The content of test.bat is :
echo on
for /f "tokens=2 delims= " %%i in ('ping -n 1 proxy ^| find /I /N "Pinging"') do set USED_PROXY_SERVER=%%i
#echo Your are using the following Proxy=%USED_PROXY_SERVER%
It told me
'ping -n 1 proxy ^| find /I /N "Pinging"' is not recognized as an internal or external command
The ping and find command can be used in command line.
Thanks a lot.
works perfectly on my machine - so, your environment must be screwed up.
try
set path=%path%;c:\windows;c:\windows\system32
before you run your batch file
Or, to be sure that the windows commands are being run
set path=c:\windows;c:\windows\system32;%path%
Reason :
the System variables : ComSpec has been modified.

Pinging Multiple Computers IF Statement

I'm trying to create a little batch file that checks multiple PCs read from a text file. For any PCs it finds are pingable, it writes a line in a "results" text file saying so. Here's what I've got:
#Echo off
set file=C:\logs\registercheck.txt
date /t >%file%
FOR /F %%I IN (C:\work\regnames.txt) DO (ping /n 1 %%I | ping /n 1 %%I | IF errorlevel 1 goto :nextreg | echo %%I is still on and has not been powered off! >>%file% | :nextreg)
PAUSE
So...when I run the file, I get multiple lines of "goto was unexpected at this time" and the only thing written in my output text file is the date. What am I doing wrong?
Thank you!
#Echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=C:\logs\registercheck.txt"
set "inputFile=C:\work\regnames.txt"
>>"%logFile%" date /t
for /f "usebackq delims=" %%i in ("%inputFile%") do (
ping -n 1 %%i >nul 2>nul
if not errorlevel 1 (
>>"%logFile%" echo(%%i is still on and has not been powered off!
)
)
You have two errors.
The first is that to put all the commands in a single line, the separator is not the pipe character (|) but the ampersand (&)
The second is that inside the do code block of the for command, if one goto is executed, the for command is finished, independently of where the label is placed. And labels inside for code blocks usually generate errors (depends of its position).
If instead of the previous code, you want a single line loop, it can be written as
for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul & if not errorlevel 1 >>"%logFile%" echo(%%i is still on and has not been powered off! )
or
for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul && >>"%logFile%" echo(%%i is still on and has not been powered off! )
that makes use of the && construct. It is intended as a shortcut for the if not errorlevel 1 .... If the command at the left of the && does not raise an errorlevel, then the command on the right side is executed.
This for the batch sintax. Now the ping. There is a difference in how ping command behaves depending of the ip version. It is not the same to ping an ipv4 address than to ping an ipv6 address. If needed you can grab from here a subrotine to handle the differences.

how do i get a batch file to accept input from a txt file?

I would like to run commands in a batch file on multiple computers.
For example:
#echo off
ping %1
pause
exit
If this batch file were called pingme.bat, and I type pingme.bat yahoo.com, then it would ping yahoo.com. My problem is I want the batch file to accept input from a text file.
For example, pingme.bat computers.txt would read the names of computers listed in the computers.txt file, and do whatever command I specified to be done to them.
%1 accepts the input I specify when I type the batch file. Now I would like the batch file to read the lines from that input text file and use them as arguments to the command.
The lines in the text are in list form, not using commas or anything.
One way to do so would be to place the URLS in a text file like so:
www.google.com
www.yahoo.com
Then run the following batch
for /f %%a in (%1) do (
echo Pinging %%a...
ping %%a
)
and run it from cmd as pingme.bat URLs.txt
Alternatively, you may specify the text file's name within the batch, and run it without the parameter
for /f %%a in (URLs.txt) do (
echo Pinging %%a...
ping %%a
)
Here's another approach
This particular batch will pull from the list, and write to output.txt if the ping was successful
#ECHO OFF
SET output=output.txt
IF EXIST "%output%" DEL "%output%"
FOR /f %%a IN (URLs.txt) DO (
CALL :ping %%a
)
GOTO :EOF
:ping
ping -n 1 %1 | find "Approximate round trip" >NUL || ECHO %1>>"%output%"
Hopefully that sets you in the right direction.
You may use a FOR loop - save this as pingme.bat:
FOR /F "tokens=*" %%L IN (%1) DO (
ping %%L
pause
)
and call it with the text file as parameter pingme.bat computers.txt.
To find IP addresses of multiple URL's in text file and to get the output in text file:
FOR /F "tokens=*" %%L IN (%1) DO (
nslookup %%L >> output.txt
pause
)
Save the script as "ping.bat" and call ping.bat URL.txt from the command prompt.

Resources