简体   繁体   English

通过命令行在Windows 7上获取IP地址

[英]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). 我有一份在命令文件(aliases.cmd)中定义的别名列表,每当我运行命令提示符(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. 使用doskey,我定义了几个别名,并且试图创建一个仅显示我的IP地址的别名。 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. 我碰上了这个问题,有很多很好的方式来获得在批处理文件中的IP,但他们都不通过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. 当我通过命令提示符(不包括doskey部分)运行它时,它可以工作并且仅返回IPv4地址。 However, via doskey and the "ip" keyword, it does not work and returns the regular output of "ipconfig". 但是,通过doskey和“ ip”关键字,它不起作用,并返回“ ipconfig”的常规输出。

Is there any way to get an IP address (and only the IP) on Windows via a doskey alias? 是否可以通过doskey别名在Windows上获取IP地址(仅IP)?

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. ip.cmd具有以下内容。

@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" doskey ip=ipconfig ^| findstr /R /C:"IPv4 Address"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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