I am trying to write a script that will do several things. I will provide a list.txt file that will have computer names. Batch file will issue a ping command against these computers to determine if it is responding or not. If it is responding, it will get the IP addresss and set a Variable to that IP address (variable is IP). For testing purposes, I have a Echo command that will echo this IP back. It succeeds and displays the correct IP address. I then want to take that variable to a For /f statement to break down the IP to the first two octets. It is to place this value in the variable IP2Octs. For instance if the computer's address is 192.168.1.4, IP2OCTS will be 192.168 Ultimately, I will be using this value to compare with a certain location's network schemea. My goal is for this batch to determine if each computer in this list is local or at a different location.
Now the problem I am running into is When I echo !IP!, it returns the correct IP. However, how do I use that !IP! in the following snippet?
for /f "tokens=1-4 delims=. " %%a in ("!ip!") do (
echo test1 "!ip!"
echo test %ip%
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)
Full Code Below
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- @Echo off
SetLocal EnableDelayedExpansion
mkdir Current
mkdir %date:~10,4%%date:~4,2%%date:~7,2%
Set LocNet=10.98
Echo Starting....
for /f %%z in (List.txt) DO (
ping -n 1 -w 3000 %%z >> Ping.txt
if !errorlevel! EQU 0 (
Echo Yup-- %%z is alive.
for /f "tokens=3" %%y in ('ping %%z ^| find "Pinging"') do (
set ip=%%y
set ip=!ip:[=!
set ip=!ip:]=!
echo %%a,%ip% >> local.txt
Rem Test IP Variable
Echo Address !ip!
Echo address !ip!
Rem Both of these echoes return the correct IP address for each computer in the list file
Rem My intention of the section is to take the IP from each computer (from the !IP! variable break down the first two octets for network
identification
Rem for /f "tokens=1-2 delims=. " %%a in (%IP%) do set IP2Octs=%%a.%%b
Rem Tried to get above line to work, but would not
for /f "tokens=1-4 delims=. " %%a in ("!ip!") do (
echo test1 "!ip!"
echo test %ip%
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)
Rem When Run returns %a,%b
Echo %%a,%%b
Rem When run this returns the correct IP
Echo !ip!
)
echo .
) ELSE (
Echo Nope. %%z not alive.
echo %%z >> DeadList.txt
)
)
EndLocal
Both Address are correct
++++++++++++++++++++++++++
Here is a screen shot:
Below is what is supposed to be echoed back:
- Address (2) - Both are correct
- test1 - supposed to be 10.98.7.250 (no quotes)
- test - supposed to be 10.98.7.250
- %a,%b - supposed to be 10.98
- 10.98.7.250 - correct.
All these are just me testing the variables and trying to understand how to set the variables. I am hoping that someone can tell me what I am doing wrong and get these two octets into a variable. If I need to provide anything more, please let me know.