简体   繁体   中英

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

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

您可以使用以下说明来实现您的目标:

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

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 azip binary download , so you do not even need admin rights to use them (just the ability to get an exe on the machine).

You can use ipconfig | cscript /Nologo script.js 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM