Skip to content
Advertisement

Get just an IP address only from `ipconfig` using native windows command

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:>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement