I am using the code below to list ip addresses from a list of computer names on list.txt, but they have to be pinging to list. How can I edit the code to show these IP addressess that do not ping also?
#echo off
Echo Pinging list...
set ComputerList=list.txt
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Results.csv
))
pause
list.txt contains the following:
and
Results.csv will populate
but it will not populate IP address for PCWINDATA103 because it is not pinging but I know an IP exists
Force a custom output in case findstr doesn't find Reply (take care of "tokens=3"):
...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3 delims=: " %%B in ('ping -4 -n 1 -l 1 %%A ^|findstr "Reply" ^|^|echo x x offline') do (
echo %%A,%%B
)
)
Related
I have a program to ping computers, check there registry, and tell me the results.
I am now trying to script it so that I don't have to know the ip address just the host name of the computer.
I found a script to give me the ip off of a hostname
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
I have tried to simply insert this into a biger loop that uses a file to give it the host names.
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do (
set host=%%a
set ip=%%c
echo "."
echo %ip% %host%
pause
) >>%2
)
I have also tried it like this,
for /f %%a in (%1) do (
set /p hostname=%%a
for /f "tokens=1,2 delims=[]" %%b in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
echo %ipaddress% %hostname%
)
Please any help on this would be greatly appreciated.
Thank you.
AFTER MUTCH HEAD BANGING
#echo off
SETLOCAL EnableDelayedExpansion
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping /4 /n 1 %%A ^| findstr "Pinging"') do echo "%%A %%b"
)
I have a list of host/url in a txt file (let's say URLList.txt) and I need to get the IP list of all of them.
There are a lot of url, so it will be pretty long to do that manually.
Basically I was wondering if, based on the input file list, I could get a result like this:
host1 IP1
host2 IP2
.
.
hostn IPn
For example
s12web 120.234.567.12
s34web 12.444.32.22
etc
Then I would put this output and copy it to my hosts file
Thanks a lot for your great help,
You can use this to do that, credit to #Lizz for the way to find the ip:
#echo off
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)
)
pause
And to echo it to a file you could use this:
#echo off
type nul>IPList.txt
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)>>IPList.txt
)
pause
As #aschipfl pointed out, another solution that's more elegant:
#echo off
>IPList.txt (
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)
)
)
I'm trying to ping against a list of websites and get the following output in a .csv, Hostname, success of ping, IP address. So the output might look like this:
Google.com, Successful (or failure), 123.456.789.012 (if failure N/A)
I have two scripts that can do these things seperatley, but can't seem to get them to work together.
Gets IP addresses:
#echo off
set ComputerList=C:\Scripts\pinglist.txt
Echo Computername,IP Address>>pingresult.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>pingresult.csv
))*
Gets hostname and ping status:
REM Set the "PingList" variable to the location of the text file you want to ping against
set PingList=C:\Scripts\pinglist.txt
REM Set the "Results" variable to the location where you want to pipe your output
set Results=C:\Scripts\pingresult.csv
echo COMPUTERNAME, STATUS >> %Results%
REM failed pings appened to file
for /f "delims=" %%a in (%PingList%) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a, failure ) >> %Results%
REM successful pings appened to file
for /f "delims=" %%a in (%PingList%) do ping -n 1 %%a >nul && (echo %%a, success ) >> %Results% || (echo %%a failed to respond)
Reformulated another answer; not optimized:
#ECHO OFF >NUL
#SETLOCAL enableextensions
echo COMPUTERNAME, STATUS, IP
for /F "usebackq delims=" %%i in ("files\30852528.txt") do (
set "_found="
for /F "delims=" %%G in ('
ping -4 -n 1 %%i^|findstr /i /C:"Ping request could not find host"') do (
echo %%i, "Could not find host", ""
set "_found=%%G"
)
if not defined _found (
for /F "tokens=3 delims=: " %%G in ('
ping -4 -n 1 %%i^|findstr /i "TTL="') do (
echo %%i, "ok", %%G
set "_found=%%G"
)
)
if not defined _found (
for /F "tokens=2 delims=[]" %%G in ('
ping -4 -n 1 %%i^|findstr /ib "Pinging"') do (
echo %%i, "Request timed out", %%G
set "_found=%%G"
)
)
if not defined _found (
echo unknown ? %%i
)
)
Output
==>D:\bat\SO\31525308.bat
COMPUTERNAME, STATUS, IP
foo.bar, "Could not find host", ""
google.com, "ok", 173.194.112.100
yahoo.com, "ok", 98.139.183.24
toyota.com, "ok", 95.101.1.91
bmw.com, "Request timed out", 160.46.244.131
==>
I am using a loop script and I was wondering how I can make the variable add 1 to each number as it goes here's what I have (this is just a snip):
for /f "tokens=1,2 delims=:" %%a in (C:\bot\userpass.txt) do (
set /a Number=%Number%+1
Echo Starting Bot #%Number%
START Client.exe %Config% %%a %%b %Server%
PING 1.1.1.1 -n 1 -w %DelayTime% >NUL
)
Only when it echos it doesn't display the correct number, how do I fix this?
You should enable delayed expansion and use !!:
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:" %%a in (C:\bot\userpass.txt) do (
set /a Number=!Number!+1
Echo Starting Bot #!Number!
START Client.exe !Config! %%a %%b !Server!
PING 1.1.1.1 -n 1 -w !DelayTime! >NUL
)
I currently have a batch file that reads a list of computer names and pings each of these and outputs the ones that reply to a csv file with the computer name and ip address.
I now need to edit this to also find out the user of the machine. I need to contact users which are online to arrange some work done to their computer. Their can be over a hundred machines in the batch file so to manually find out each user takes time. Is there a way to do this?
`IF EXIST C:\test\new.csv (del C:\test\new.csv)
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address>Final.csv
setlocal enabledelayedexpansion
echo please wait...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
)
)
findstr /V "IPAddress" final.csv >> C:\test\new.csv
echo identified machines for Install
start excel C:\test\new.csv
echo opened csv file`
The command I want to use to get the username is:
`wmic.exe /NODE: %%A COMPUTERSYSTEM GET USERNAME`
Thanks
Mark
Here is a function I wrote to do just what you are trying to do:
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
Here is my function for pinging. It returns a 0 if the ping succeeded and a 1 otherwise.
:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
for /l %%a in (1,1,255) do (
call:IsPingable 10.6.1.%%a && (
echo ping 10.6.1.%%a used )||( echo ping 10.6.1.%%a unused )
)
And here is for if you're pinging IP's and want to return the hostname as well:
:IsPingable2 comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~2=%name%
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
#echo off
setlocal enabledelayedexpansion
for /l %%a in (1,1,255) do (
call:IsPingable2 10.6.1.%%a host && (
echo ping !host! - 10.6.1.%%a used )||( echo ping !host! - 10.6.1.%%a unused )
)
I just posted these because they just might come in handy for this type of thing in the future. You can use the :IsPingable now though.
You would use it like this in your code:
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address,Logged In User>Final.csv
setlocal enabledelayedexpansion
echo please wait...
echo.
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|find "TTL="') do (
if not errorlevel 1 (
set IPadd=%%B
call :GetLoggedInUser %%B uname
echo %%A,!IPadd:~0,-1!,!uname!>>final.csv
)
)
)
echo identified machines for Install
start excel C:\test\final.csv
echo opened csv file
goto :eof
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
The below code will count the number of lines in two files sequentially and is set to the variables SalaryCount and TaxCount.
#ECHO OFF
echo Process started, please wait...
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%
Now if you need to output these values to a csv file, you could use the below code.
#ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv
The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath