我需要将命令及其输出redirect到Windows CLI中的文本文件。 例如,我使用FOR
循环在子网上运行nslookup
命令,
for /L %i IN (1,1,254) DO nslookup 192.168.1.%i >> nslookup.txt
但是,这只会redirect命令的输出。
有没有办法将命令以及输出redirect到文本文件? 请不要告诉我有关剪辑,并select所有/复制命令。
您可以使用“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
将命令链接在一起,以便在nslookup
之前运行ECHO
。
如果你想使用一个批处理文件,它变得更清晰一点:
@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
这工作。 但我相信有更聪明的方法来做到这一点。