With a lot of help from another user I was able cut characters out of a single output from CMD. I then added my next part of code from where he left me with variable _3octet. My code is scanning all the IP addresses associated with the gateway the computer is connected to and snags only the ones that have a reply. From there I get a list of IP's that have replied.
Reply from 192.168.1.67: bytes=32 time=288ms TTL=64
Reply from 192.168.1.72: bytes=32 time<1ms TTL=128
Reply from 192.168.1.73: bytes=32 time=16ms TTL=64
Reply from 192.168.1.75: bytes=32 time<1ms TTL=128
Reply from 192.168.1.76: bytes=32 time=155ms TTL=64
Reply from 192.168.1.88: bytes=32 time=1ms TTL=255
From there I want to grab only the IP addresses, so cutting away everything except for "192.168.1.#", to be used in a future part of the program. Similar to the begining part of the code with the _3octet variable.
Here is my code.
@echo off
setlocal
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
rem split on : and get 2nd token
for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
rem we have " 192.168.42.78"
rem split on . and get 4 tokens (octets)
for /f "tokens=1-4 delims=." %%c in ("%%b") do (
set _o1=%%c
set _o2=%%d
set _o3=%%e
set _o4=%%f
rem strip leading space from first octet
set _3octet=!_o1:~1!.!_o2!.!_o3!.
echo !_3octet!
)
)
)
cd C:\Windows
for /l %%i IN (1,1,254) do ping -n 1 !_3octet!%%i | for /f delims^=^:^ tokens^=1 %%g in (find /i "bytes=32") do (
for /f "tokens=1-4 delims=." %%j in ("%%g") do (
set _o1=%%j
set _o2=%%k
set _o3=%%l
set _o4=%%m
set _4octet=!_o1:~11!.!_o2!.!_o3!.!_o4!
echo !_4octet!
)
)
endlocal
I'm getting : was unexpected at this time. as an output multiple times. Which tells me that it is trying to ping every address 1-254 and not the ones that come up as a reply. Needless to say my code isn't working as I hoped.
I was planning on using that final list of IP addresses in this code: shutdown -r -m \\192.168.1.1 -t 10 in order to shut down the computers connected to the gateway.
If this is at all confusing let me know. Thank you.