so I'm currently working on a batch file which scans the network. what I have done is created a text file which has just the IP of every computer on the network that responded to a ping request, and the output of the ping request is saved to a file called IPAdresses.txt
::==========================================================================
setlocal enabledelayedexpansion
#echo off
::File reset and variables
echo. > %~dp0Logs\IPAddresses2.txt
SET fl=%~dp0Logs\IPAddresses.txt
::This command does the ping request and saves just the replies to %~dp0Logs\IPAddresses.txt.
::Every returned ping will make a line in the file that looks like this "Reply from 192.168.1.1: bytes=32 time=2ms TTL=64".
FOR /L %%i IN (1,1,254) DO ping -n 1 192.168.1.%%i | FIND /i “Reply” >> %fl%
::This command Parse the "Reply from 192.168.1.1: bytes=32 time=2ms TTL=64" line in the file and saves the output to %~dp0Logs\IPAddresses2.txt.
for /f "usebackq tokens=3" %%t in (`findstr /b /c:Reply from %fl% `) do (
echo %%t >> %~dp0Logs\IPAddresses2.txt
)
::-----------THE ISSUE-----------
::The issue is the parsing. The last command will only parse "192.168.1.1: " this is because it only takes the 3 Token (or the third set of characters in the string) in the line. I need to get rid of the ": " from "192.168.1.1: ". To be clear i want to remove the semicolon and the following space, at the end of the IP address.
::i have tried to put every line into a variable and cut the tailing characters but i cannot get it to work.
::Or is there a way to alter the FOR command to do this??
I have been searching for two days now and trying all sorts of work arounds, but no luck.
I need just the IP's in a text files for further functions, and network wide NET commands.
Any help would be appreciated
Thank You.
Never too late to answer, eh? This gets you most of the way there. There are probably cleaner ways of doing this, but I tried to follow your code as much as I could.
setlocal enabledelayedexpansion
#echo off
SET fl=%~dp0Logs\IPAddresses.txt
set net=10.2.10.
set start=71
set end=75
FOR /L %%i IN (%start%,1,%end%) DO ping -n 1 %net%%%i | findstr /i Reply >> %fl%
for /f "usebackq tokens=3" %%t in (`findstr /b /c:Reply from %fl% `) do (
set ip=%%t
set ip=!ip:~0,-1!
echo !ip!
)
Related
Specifically here, I am trying to pass the output of a ping command that is piped to a Find "Reply" to a variable. From there, I'd like to take that variable and check the IP against a list of IP's to determine which subnet that IP is on. I'm thinking I can use some iteration of FOR to then split off just the IP, which I can then use from there to check against my list.
I was hoping that I could just use the FOR with the ping -4 -n 1 %WHATTOPING% | Find "Reply" as my parameter, but I don't think that is going to work.
Please let me know if my question needs any clearing up! Thank you!
So far my code is:
#echo off
CLS
:start
SET /p WHATTOPING= What would you like to ping?
ping -4 -n 1 %WHATTOPING% | Find "Reply"
for /f "delims=" %%A in ('ping -4 -n 1 %WHATTOPING%') do set "var=%%A"
echo %var%
PAUSE
CLS
goto start
for /f "delims=" %%A in ('ping -4 -n 1 %WHATTOPING% ^| Find "Reply"') do set "var=%%A"
should work for you, but without a(n obfuscated) sample of the data you are generating with the ping and the value you wish to extract from that data, it can be but a hint.
The critical point is that you need to escape the | with a caret ^ in order to tell cmd that the pipe is part of the single-quoted command, not of the for that is interpreting the result of that command.
Reply is language dependent (in a German Windows, it would be Antwort). (Besides that, it's not reliable within the same network: you can get something like Reply from <localhost>: destination not reachable.)
For an "international" solution, get the address from the header line (it's enclosed in []):
for /f "tokens=2 delims=[]" %%a in ('ping -4 -n 1 www.google.de ^|find "["') do set "IP=%%a"
echo %IP%
I have a text file, that has some text with this syntax:
mytextfile.txt:
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
And so on....
And I'd like to be able to find username and password strings by pointing the websiteurl, so let's say I tell the batch file,
find websiteurl3, it should print the username3 and password3
and I was able to write a "FOR LOOP" but I am not sure how to use the syntax of the loop, as my code finds only the last line always, here is what I have:
FOR /F "tokens=2,3 delims= " %%A IN (URL.txt) DO IF EXIST URL.txt (set WEBUserName1=%%A) && (SET WEBUserPass1=%%B)
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
I know the loop is looking on the "URL.txt" for the tokens 2 and 3, and then sets the variables accordingly, what I'd like to know, is how I can use the loop, or if needed, any other command to be able to:
Find the "URL.txt file
Then find the specific string, in this case, the first word of the specified string line
Then find tokens 2 and 3 of that string line.
And I'd like to be able to find username and password strings
Use the following batch file.
test.cmd:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3" %%a in ('type mytextfile.txt ^| findstr "%1"') do (
echo Username:"%%a"
echo Password:"%%b"
pause
exit
)
endlocal
Notes:
Pass the website URL string as a parameter to the batch file.
findstr is used to find the matching line from the file, so we only need to parse a single line using for /f.
Example usage and output:
F:\test>type mytextfile.txt
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
F:\test>test websiteurl3
Username:"username3"
Password:"password3"
Press any key to continue . . .
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
findstr - Search for strings in files.
for /f - Loop command against the results of another command.
type - Display the contents of one or more text files.
bad way, but this too work:
#echo off
setlocal
set srch_site=websiteurl2
FOR /F "tokens=1,2,3 delims= " %%A IN (URL.txt) DO (
IF EXIST URL.txt (
if "%%A"=="%srch_site%" (
(set WEBUserName1=%%B) && (SET WEBUserPass1=%%C)&goto:stdout
)
)
)
:stdout
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
May I offer you a different approach?
The code below get all usernames and passwords and store they in two vectors:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-3" %%A in (URL.txt) do set "User[%%A]=%%B" & set "Pass[%%A]=%%C"
After that, you may test if the username and password of a certain site was given this way:
set "site=websiteurl3"
if defined User[%site%] echo The site "%site%" have username and password
... or display their values this way:
echo Username:"!User[%site%]!"
echo Password:"!Pass[%site%]!"
You may review a detailed explanation of array management in Batch files at this post.
You get the last line, because you process the whole file. Filter your textfile and process only that one line:
set "site=websiteurl2"
FOR /F "tokens=2,3 delims= " %%A IN ('find "%site%" URL.txt') DO (
set "usr=%%A"
set "pwd=%%B"
)
echo %usr%, %pwd%
I have txt files that contain several lines and I need to create a log out of them to store in a log the following information:
File Name
Last modified
Count of lines containing the word "valid"
I've put together a .bat file but it splits the output in two lines.
type nul > FilesReceived.txt & for %f in (*.log) do (
find /c "valid" %f & echo(%~tf)>> LogsReceived.txt
)
With type nul I clear the contents of the FilesReceived.txt file. Then I loop through the files of type log.
Then I count lines that contain the word valid with find /c and I also echo the last modified time stamp.
However the output looks like:
---------- transaction_20160505_1005A.log: 6492
10/06/2016 04:37 p.m.
I don't know what's generating those dashes. Ultimately I'd like to have one line per log file as follows:
transaction_20012B.log: 6492 10/06/2016 04:37 p.m.
Hope you guys can help me.
Thanks,
Bruce
find prints the dashes if it processes a file. It doesn't, when processing from STDIN (type file.ext /c |find "string" prints the count only).
There is a trick to write without linefeed: <nul set /p"=Hello"
If you can live with another order, it's quite easy to assemble it::
#echo off
for %%f in (*.bat) do (
<nul set /p "=%%f %%~tf "
type %%f|find /c "echo"
)
If you want to keep your order it's a little bit more complicated: you can't force find to write without linefeed, so you have to use a trick (another for):
#echo off
(for %%f in (*.txt) do (
<nul set /p "=%%f: "
for /f %%i in ('type %%f^|find /c "valid"') do (<nul set /p "=%%i ")
echo %%~tf
))>LogsReceived.txt
You may get the output of find command via another for and put it at any place you wish:
#echo off
(for %%f in (*.log) do (
for /F %%c in ('find /c "valid" ^< %%f') do echo %%f: %%c %%~tf
)) > LogsReceived.txt
I have multiple TraceRT log files containing 30 hops. I'm only looking for similar IP (ex. 192.168.1) and would like to log it on one file with:
1) Successful: %IP% found in %Filename%
2) Fail: Specified IP not found in %Filename%
I'm trying to use:
rem************************************************************
:START
# ECHO OFF
rem US date
set YEAR=%DATE:~10,4%
set MONTH=%DATE:~4,2%
set DAY=%DATE:~7,2%
rem US hour
set HOUR=%TIME:~0,2%
set MIN=%TIME:~3,2%
set SEC=%TIME:~6,2%
set HUNDREDS=%TIME:~9,2%
set HOURMIN=%HOUR%%MIN%
rem Make sure that hour has two digits
IF %HOUR% GEQ 10 goto twoh
set HOUR1=%TIME:~1,1%
set TWOHOUR=0%HOUR1%
goto fulltid
:twoh
set TWOHOUR=%HOUR%
:fulltid
set FULLTIME=%TWOHOUR%'%MIN%'%SEC%'%HUNDREDS%
set FTIME=%TWOHOUR%:%MIN%:%SEC%
#echo off & setLocal EnableDELAYedeXpansion
findstr /m "192.168.1" *.txt > FILENAME
echo on
for /f "tokens=*" %%a in (*.txt ^| find "192.168.1") do (
IF %%a neq %%b (
echo Suscessful: %%a %FILENAME% >> Log%YEAR%%MONTH%%DAY%.txt
) ELSE (
echo Fail: Specified IP not found in %FILENAME% >> Log%YEAR%%MONTH%%DAY%.txt
)
)
goto START
rem************************************************************
You have specified an invalid pipe | find. You cannot pipe (a) text file(s) into a command.
Either provide the file(s) as argument(s) to find, or use redirection (this works for a single file only but not for multiple ones nor */? patterns though).
You are using for /f not correctly.
It looks as if you wanted to parse the output of find. To accomplish that, but you must enclose the command within single-quotes '. Type for /? and see the help text for more details.
The following line of code should work:
for /f "tokens=*" %%a in ('find "192.168.1" *.txt') do (
To get current date and time, I strongly recommend to read variables %DATE% and %TIME% once only and within a single line! Otherwise you might run into problems, especially concerning the fractional seconds, which might not be equal between consecutive expansions.
To ensure %HOUR% to have two digits, you simply need to use set HOUR=0%HOUR% then set HOUR=%HOUR:~-2%.
Since a one-digit %HOUR% is prefixed by a space here, you can have it even simpler (thanks for your comment, #Stephan!) by just replacing the space by a zero: set HOUR=%HOUR: =0%.
I have a .bat that I use to quickly query basic information from servers. After it gets the FQDN from DNS, I need to insert a "-r" (minus quotes) after the servername, but before the ".domain.com". The area that it will be added to the script is below -
for /f "delims=[] tokens=2" %%b in ('ping %servername% -n 1 ^| findstr "["') do (set thisip=%%b)
for /f "tokens=2" %%a in ('nslookup %thisip% ^| find /i "Name: "') do (set fqdnstat=%%a)
so how can I take the FQDN, which is set to fqdnstat, and modify it from -
server.domain.com
to server-r.domain.com ?
Edit - I guess I didn't really explain very well. I just need to insert text into a line of text, before a period. I need to take the following name: server.domain.com and edit it to read server-r.domain.com, using a command. The rest of the script above is context for the issue. The fqdnstat is the variable that I use to for the Fully Qualified Domain Name.
I am afraid I don't really understand your concern, but this Batch file may help you:
#echo off
set fqdnstat=server.domain.com
echo Before: "%fqdnstat%"
for /F "tokens=1* delims=." %%a in ("%fqdnstat%") do set "fqdnstat=%%a-r.%%b"
echo After: "%fqdnstat%"
set servername=%servername:.domain.com=-r.domain.com%
Presumably above the two lines you've put, but I'm not sure what the goal is, so maybe not.