Separate IP from text in ping output? - batch-file

I'd like to be able to run a single batch file that would find the IP address of every device connected to my router, and then find their hostnames from it. What I have so far is below:
#echo off
set /a n=0
:repeat
set /a n+=1
echo 192.168.1.%n%
ping -n 1 -w 500 192.168.1.%n% | FIND /i "Reply">>devices.txt
if %n% lss 254 goto repeat
type devices.txt
goto :hostname
This finds all the possible addresses and writes them to a text file, devices.txt.
However, I now end up with the following:
Reply from 192.168.1.82: bytes=32 time=5ms TTL=255
The next part of the batch file goes:
:hostname
ping -a %ip% -l 1 -n 1 >> hostnames.txt
This pings the IPs found in devices.txt and returns the result in hostnames.txt - or at least, I'd like it to.
Somehow I need to separate the IP address from the other text when writing it to devices.txt, and then assign it as a variable so it can be used by the next part.
Is there a simple function I can use to do either of these things?

Try this:
#echo off
setlocal
set "p=ping -n 1 -w 500 192.168.1"
for /l %%a in (1,1,254) do (
for /f "Tokens=3 delims=: " %%b in (
'%p%.%%a^|Find /i "TTL="'
) do (
echo Pinging %%b
echo %%b>>devices.txt
for /f "tokens=2" %%c in (
'ping -a %%b -l 1 -n 1^|Find /i "pinging"') do (
echo %%c >>hostnames.txt
)
)
)
We start by creating a For /L loop that goes from 1 to 254 in increments of 1. Then we run a for loop on a ping command that takes the third token on the return line and puts it into a variable, %%b in this case. We then echo out pinging IP address contained in %%b and write it to devices.txt. Then, we run another ping to extract the second token which is the hostname of the computer we're pinging and write it out to hostnames.txt.

Related

batch script to check url status without ping command

I can't use ping command because it is blocked on my server.
I followed the anser from https://superuser.com/questions/1172790/bat-file-to-check-servers-are-up/1173006#answer-1173006. But it uses ping command in batch file.
Here is my code:
REM add urls in urls.txt
for /f %%a in (C:\Job-Batches\server_check\urls.txt) do (
echo Pinging %%a ...
ping -n 2 %%a | find "Reply" > NUL
if not errorlevel 1 (
REM ip is down
echo %%a is down
) else (
REM ip is up
echo %%a is up
)
)
Can someone suggest, if it can be done without ping...
Thanks in advance!

Continuous Ping with Timestamp to File

I am wanting to run a continuous ping on an IP and write the results with timestamp to a text file.
I have tried the below code saved as a .bat file. If I run it, it will ask me to input the IP address and then it makes the text file. But it does nothing else. If, I run it as admin, it will ping forever like it should, but it won't create or write to a text file. I am using windows 10 pro.
#echo off
set /p host=host Address:
set logfile=Log_%host%.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping)
For the life of me, I can't figure out why it is behaving this way.

Bat file output into one long string

Is there anyway to have the output from this bat file into one long string e.g.
11/03/2015, 9:36:24.28, Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), Minimum = 170ms, Maximum = 170ms, Average = 170ms
#ECHO off
set IPADDRESS=192.168.4.1
set INTERVAL=60
:PINGINTERVAL
echo %date% %time% >>CFConnection.txt
ping %IPADDRESS% -n 1 | FIND "loss" >>CFConnection.txt
ping %IPADDRESS% -n 1 | FIND "Minimum" >>CFConnection.txt
timeout %INTERVAL%
GOTO PINGINTERVAL
You could use set /P "=output"<NUL to write text to CFConnection.txt without a line break.
set /P "=output"<NUL
does pretty much the same thing as
echo output
but without a carriage return at the end.
You should also use findstr so you can match lines containing "loss" or "minimum" with one command, and so you're not trying to mash two ping results together into one dubious log entry of deceit and treachery. And finally, any time you want to capture the output of a command, use a for /f loop.
#echo off
setlocal
set "IP=192.168.4.1"
set "interval=60"
:loop
>>CFConnection.txt (
(
set /P "=%date% %time%, "<NUL
for /f "delims=" %%I in ('ping -n 1 %IP% ^| findstr /i "loss minimum"') do (
set /P "=%%I "<NUL
)
echo;
)
)
timeout %interval%
goto loop

How to ping multiple servers and return IP address and Hostnames using batch script?

So I have to use batch only for this. Basically, the server HOSTNAMES are all listed in a txt file. I used the following code to ping all the servers and display their results in a txtfile.
For /f %%i in (testservers.txt) do ping -n 1 %%i >>pingtest.txt
The above pinged all the servers. Now, I want to output the IP addresses and the HOST Names in a separate file. How can I do this?
I know that I can run a for loop searching for words like "TTL" and then look for the 3rd token (for the IP) and words like "PINGING" for the second token(HOSTNAME). But I am having errors and cant display it properly. The reason why I want to output IPs and Hostnames in a different file is to make a list of the DOWN and UP servers.
Help will be appreciated. :)
EDIT: Just so it isn't confusing, wanted to let you guys know there are 3 different files, testservers.txt has the HOSTNAMES in it, pingtest.txt has ping results, and result.txt will have the IPs along with Hostnames with their current status as DOWN or UP.
Well, it's unfortunate that you didn't post your own code too, so that it could be corrected.
Anyway, here's my own solution to this:
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)
The outer loop iterates through the hosts and the inner loop parses the ping output. The first two if statements handle the two possible cases of IP address resolution:
The host name is the host IP address.
The host IP address can be resolved from its name.
If the host IP address cannot be resolved, the address is set to "ADDRESS N/A".
Hope this helps.
Parsing pingtest.txt for each HOST name and result with batch is difficult because the name and result are on different lines.
It is much easier to test the result (the returned error code) of each PING command directly instead of redirecting to a file. It is also more efficient to enclose the entire construct in parens and redirect the final output just once.
>result.txt (
for /f %%i in (testservers.txt) do ping -n 1 %%i >nul && echo %%i UP||echo %%i DOWN
)
I worked on the code given earlier by Eitan-T and reworked to output to CSV file. Found the results in earlier code weren't always giving correct values as well so i've improved it.
testservers.txt
SOMESERVER
DUDSERVER
results.csv
HOSTNAME LONGNAME IPADDRESS STATE
SOMESERVER SOMESERVER.DOMAIN.SUF 10.1.1.1 UP
DUDSERVER UNRESOLVED UNRESOLVED DOWN
pingtest.bat
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.csv
>nul copy nul %OUTPUT_FILE%
echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
set SERVER_ADDRESS_I=UNRESOLVED
set SERVER_ADDRESS_L=UNRESOLVED
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS_L=%%y
if %%x==Pinging set SERVER_ADDRESS_I=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE!
echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE%
)
the problem with ping is if the host is not alive often your local machine will return an answer that the pinged host is not available, thus the errorcode of ping will be 0 and your code will run in error because not recognizing the down state.
better do it this way
ping -n 4 %1 | findstr TTL
if %errorlevel%==0 (goto :eof) else (goto :error)
this way you look for a typical string ttl which is always in the well done ping result and check error on this findstr instead of irritating ping
overall this looks like this:
#echo off
SetLocal
set log=path/to/logfile.txt
set check=path/to/checkfile.txt
:start
echo. some echo date >>%log%
:check
for /f %%r in (%check%) do (call :ping %%r)
goto :eof
:ping
ping -n 4 %1 | findstr TTL
if %errorlevel%==0 (goto :eof) else (goto :error)
:error
echo. some errormessage to >>%log%
echo. some blat to mail?
:eof
echo. some good message to >>%log%
Try this
$servers = Get-Content test.txt
$reg=""
foreach ($server in $servers)
{
$reg=$reg+$server+"`t"+([System.Net.Dns]::GetHostAddresses($server) | foreach {echo $_.IPAddressToString})+"`n"
}
$reg >reg.csv
#echo off
set workdir={your working dir. for example - C:\work }
set iplist=%workdir%\IP-list.txt
setlocal enabledelayedexpansion
set OUTPUT_FILE=%workdir%\result.csv
>nul copy nul %OUTPUT_FILE%
echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE%
for /f %%i in (%iplist%) do (
set SERVER_ADDRESS_I=UNRESOLVED
set SERVER_ADDRESS_L=UNRESOLVED
for /f "tokens=1,2,3" %%x in ('ping -a -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS_L=%%y
if %%x==Pinging set SERVER_ADDRESS_I=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE!
echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE%
)
This worked great I just add the -a option to ping to resolve the
hostname. Thanks https://stackoverflow.com/users/4447323/wombat
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.csv
>nul copy nul %OUTPUT_FILE%
echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
set SERVER_ADDRESS_I=UNRESOLVED
set SERVER_ADDRESS_L=UNRESOLVED
for /f "tokens=1,2,3" %%x in ('ping -n 1 -a %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS_L=%%y
if %%x==Pinging set SERVER_ADDRESS_I=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE!
echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE! >>%OUTPUT_FILE%
)
This works for spanish operation system.
Script accepts two parameters:
a file with the list of IP or domains
output file
script.bat listofurls.txt output.txt
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=%2
>nul copy nul %OUTPUT_FILE%
for /f %%i in (%1) do (
set SERVER_ADDRESS=No se pudo resolver el host
for /f "tokens=1,2,3,4,5" %%v in ('ping -a -n 1 %%i ^&^& echo SERVER_IS_UP')
do (
if %%v==Haciendo set SERVER_ADDRESS=%%z
if %%v==Respuesta set SERVER_ADDRESS=%%x
if %%v==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE!
)

batch file - getting ip from ping command

i have this command.
ping -n 1 piratelufi.com | find /i "reply from"
i want to get the ip inside the ping response. i thought of using %var:~0,10% but i have no idea how to redirect the output to echo.
i even tried to use pipe
ping -n 1 piratelufi.com | find /i "reply from" | echo %1
but the %1 variable does not represent the output of the first two commands. i even tried to use &1 but i failed. What is the 'variable' needed to echo out the output from the first two commands?
Check out the FOR command it's really cool:
FOR /f "tokens=1,3 delims=: " %%A IN ('ping -n 1 piratelufi.com') DO IF %%A==Reply ECHO IP IS %%B
Try the following:
for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 piratelufi.com ^|find /i "pinging"') do echo IP=%f

Resources