Is there a way to get my adapter ipv4 address for my active internet connection via CLI in windows? [duplicate]

I'm attempting to have cmd copy only a specific set of character from a command output. I've been able to select the line that the phrase exists in but from there I am lost. Here is my command so far.

ipconfig | findstr /i "ipv4"

I'm sure you are familiar with the ipconfig command and the findstr command. Basically I'm isolating the IPv4 address of the specific computer. This is what the above command.

IPv4 Address. . . . . . . . . . . : 192.168.1.75

I'd like to isolate "192.168.1." and have that saved to a variable to be used much later in a command. Say that I set "192.168.1." to variable a.

The next command I wish to run scans all IP addresses from 192.168.1.1 to 192.168.1.254 and save whichever are being used. As this, soon to be program, will be run on different computers at various locations the "192.168.1." will be different and I'm looking to isolate whatever cmd puts in it's location. So the aforementioned variable a could be but is not limited to, 192.168.10., 10.10.10., or even 1.1.1. depending on the situation. Here is the code I will be using.

FOR /L %i IN (1,1,254) DO ping -n 1 (the 'a' variable)%i | FIND /i "bytes=">>c:\ipaddresses.txt

or if seeing what the variable will look like helps.

FOR /L %i IN (1,1,254) DO ping -n 1 192.168.1.%i | FIND /i "bytes=">>c:\ipaddresses.txt

I am using a 64 bit windows 10 pro machine.


Solution 1:

I'd like to isolate "192.168.1." and have that saved to a variable

Use the following batch file (test.cmd):

@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!
      )
    )
  )
rem add additional commands here
endlocal

Notes:

  • The value you want is saved in _3octet
  • Remove the echo which is there for debugging
  • Replace rem add additional commands here with your "network ping"

Example usage and output:

F:\test>ipconfig | findstr /i "ipv4"
   IPv4 Address. . . . . . . . . . . : 192.168.42.78

F:\test>test
192.168.42.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /f - Loop command against the results of another command.
  • ipconfig - Configure IP (Internet Protocol configuration)
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • setlocal - Set options to control the visibility of environment variables in a batch file.
  • variables - Extract part of a variable (substring).