2

Let say this is an output of Windows ipconfig command.

c:\>ipconfig

Windows IP Configuration

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.1.10
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

c:\>

In Linux OS, I can easily get just an IP Address using grep and cut command.

user@linux:~$ cat ip  
c:\>ipconfig

Windows IP Configuration

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.1.10
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

c:\>
user@linux:~$ 

user@linux:~$ cat ip | grep IPv                 
   IPv4 Address. . . . . . . . . . . : 192.168.1.10
user@linux:~$ 

user@linux:~$ cat ip | grep IPv | cut -d ':' -f 2
 192.168.1.10
user@linux:~$ 

However, in Windows this is the best I can get using findstr command. Is there a way whereby we can cut just the IP portion out of this output?

c:\>ipconfig | findstr IPv4
   IPv4 Address. . . . . . . . . . . : 192.168.1.10

c:\>

What I'm expecting is something like this using native windows command only

c:\>ipconfig | <some command here just to get an IP Address only>
   192.168.1.10
c:\>
jww
  • 97,681
  • 90
  • 411
  • 885
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Mar 09 '18 at 12:16

4 Answers4

2

I wasn't able to get the ipconfig command to work the way I wanted which was to isolate the IP of the specific network adapter I needed.

I needed "Ethernet" but you can choose which ever you want like "Wi-Fi" for example.

To achieve this, I used netsh instead (Note: the OP asked about ipconfig specifically but this achieves the same result so I thought I'd share):

for /f "tokens=3 delims=: " %i  in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do echo Your IP Address is: %i

For an explanation of how this works and how to customize it, see my related article here: https://stackoverflow.com/a/59004409/2684661

Joshua Dyck
  • 2,113
  • 20
  • 25
0

you can use following instruction to achieve your goal:

for /f "tokens=2 delims=:" %i  in ('ipconfig ^| findstr "IPv4" ^| findstr [0-9]') do echo %i
Renee
  • 31
  • 4
-1

If I need to do something like this, I use the GnuWin32 utilities.

Just run the installer, and you're ready to go. (May need to adapt %PATH%.)


I'm pretty sure you could hack up a batch-file based solution using findstr and possibly a for loop tokenization but I wouldn't bother.

The GnuWin32 utils offer a zip binary download, so you do not even need admin rights to use them (just the ability to get an exe on the machine).

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • Thanks @Martin Ba Is there a way to do this via native windows command? I've just updated my question –  Mar 09 '18 at 08:33
  • @Sabrina - added to answer. That's all I'll say for a "native" solution :-) – Martin Ba Mar 09 '18 at 08:38
-1

You can use ipconfig | cscript /Nologo script.js with a file script.js containing:

var lines = WScript.Stdin.ReadAll().split('\n');

for(var i = 0; i < lines.length; ++i) {
        var line = lines[i];

        if(line.match(/IPv4 Address/)) {
                WScript.echo(line.replace(/ *IPv4 Address[ .]*: /, ''));
        }
}

Note that there may be several network adapters, causing multiple IPs to be printed.

jjrv
  • 4,211
  • 2
  • 40
  • 54