繁体   English   中英

在Windows CLI中重定向命令以及输出到文本文件?

[英]Redirect command as well as output to text file in Windows CLI?

我需要将命令及其输出重定向到Windows CLI中的文本文件。 例如,我正在使用FOR循环在子网上运行nslookup命令,

for /L %i IN (1,1,254) DO nslookup 192.168.1.%i >> nslookup.txt

但是,这仅重定向命令的输出。

有没有办法将命令和输出都重定向到文本文件? 请不要告诉我有关剪辑的信息,请选择全部/复制命令。

您可以使用“ cmd / c”继续执行命令,以启动新的命令提示符,并重定向命令提示符的输出:

cmd /c for /L %i IN (1,1,254) DO nslookup 192.168.1.%i > nslookup.txt

请注意,由于cmd的输出将发送到nslookup.txt,因此您只需要使用一个大于(>)的字符。 遗憾的是,这错过了错误输出,因此您看不到每个失败地址的***请求未知的超时。

FOR循环是正确的,它听起来就像你已经得到你想要的输出,因此,所有你需要做的就是ECHO运行它之前的命令:

for /L %i IN (1,1,254) DO ECHO nslookup 192.168.1.%i&nslookup 192.168.1.%i >> nslookup.txt

&将命令链接在一起,因此ECHOnslookup之前运行。

如果要使用批处理文件,它将变得更加清晰:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET Outfile=nslookup.txt

REM Log the date/time.
ECHO %DATE% - %TIME%>%Outfile%

FOR /L %%i IN (1,1,254) DO (
    SET Command=nslookup 192.168.1.%%i

    REM Print the command being run.
    ECHO !Command!>>%Outfile%

    REM Run the command.
    !Command!>>%Outfile%
)

ENDLOCAL

用于/ L%i IN(1,1,254)DO(@echo nslookup 192.168.1。%i&nslookup 192.168.1。%i)>> nslookup.txt

这可行。 我仍然相信有更聪明的方法可以做到这一点。

暂无
暂无

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

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