简体   繁体   中英

Get IP address on Windows 7 via command line

I have a list of aliases defined in a command file (aliases.cmd) that I load whenver I run a command prompt (cmd.exe /k aliases.cmd). Using doskey, I have several aliases defined and I am trying to create one to show only my IP address. I came across this question with a lot of good ways to get an IP in a batch file but none of them seem to work via doskey. For example, I have this alias defined:

doskey ip=ipconfig | findstr /R /C:"IPv4 Address"

When I run it via the command prompt (excluding the doskey portion), it works and only returns the IPv4 address. However, via doskey and the "ip" keyword, it does not work and returns the regular output of "ipconfig".

Is there any way to get an IP address (and only the IP) on Windows via a doskey alias?

So after some study, the only way I could get this to work is the following.

doskey ip=ip.cmd

And ip.cmd has the following.

@echo off
ipconfig | findstr /R /C:"IPv4 Address"

I believe your problem was that you did not escape the pipe character, as with: doskey ip=ipconfig ^| findstr /R /C:"IPv4 Address" doskey ip=ipconfig ^| findstr /R /C:"IPv4 Address"

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