windows 循环和 nslookup 的批处理脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35950161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch script for loop & nslookup
提问by Jace Nimz
I've seen this question posted before, but none of the solutions address my issue. I'm simply trying to iterate through a text file that contains host names. When I try the same command (omitting the extra percent signs) from the command line, it will work once or twice then then give the error noted below. Running it as a batch file, the batch file exits without doing anything. This one has really stumped me.
我之前看过这个问题,但没有一个解决方案能解决我的问题。我只是试图遍历包含主机名的文本文件。当我从命令行尝试相同的命令(省略额外的百分号)时,它会工作一两次,然后给出下面指出的错误。将它作为批处理文件运行,批处理文件退出而不做任何事情。这个真的把我难住了。
Here's the code in my batch file:
这是我的批处理文件中的代码:
@echo off
set OUTPUTFILE=Results/Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do
FOR /F "usebackq skip=3 delims=: tokens=2" %j in (`nslookup %i`)
do @echo %%i %%j >> %OUTPUTFILE%
At a command line I get:
在命令行我得到:
i`) was unexpected at this time.
i`) 在这个时候出乎意料。
When I run it at a command line I am taking out the additional percent signs needs when it runs in a batch file. I'm also using absolute paths at the command line, to ensure it this isn't an issue with the environment variables I've set.
当我在命令行中运行它时,我将在批处理文件中运行时取出额外的百分号。我还在命令行中使用了绝对路径,以确保这不是我设置的环境变量的问题。
回答by Magoo
@echo off
set OUTPUTFILE=Results\Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do (
FOR /F "skip=3 delims=: tokens=2" %%j in ('echo(^|nslookup %%i') do @echo %%i %%j >> %OUTPUTFILE%
)
Problems:
/
is a switch in winbatch; \
is a directory-level-separator.
问题:
/
是winbatch中的一个开关;\
是目录级分隔符。
do (
must be on the same physical line as its for
do (
必须与其在同一条物理线上 for
%i
should be %%i
%i
应该 %%i
%j
should be %%j
%j
应该 %%j
I've removed the usebackq
and changed the quotes from backticks to single-quotes as it's not necessary to use usebackq
here.
我已经删除了usebackq
并将引号从反引号更改为单引号,因为这里没有必要使用usebackq
。
(minor fixes added)
(添加了小修复)
The "flashing cursor" is caused by nslookup
requesting information from the keyboard. Adding echo(
supplies a newline to terminate nslookup
and ^|
is an escaped-pipe which directs the output of that echo
to the input of nslookup
.
“闪烁的光标”是由nslookup
键盘请求信息引起的。添加echo(
提供了一个换行符来终止,nslookup
并且^|
是一个转义管道,它将其输出定向echo
到 的输入nslookup
。
Since my setup differs radically from yours, I can do but primitive verification.
由于我的设置与您的完全不同,我只能进行原始验证。
回答by M A
I modified Magoo's answer to output to a CSV file. It will output two columns "#, Input, Output" where you will have two rows for each successful nslookup call, one with the hostname, and one with the IP. I also modified the delims to delimit on ":" and " " so that the output doesn't have a bunch of extra spaces. I wanted to contain each query on a single row with columns "Input, Hostname, IP" but I got frustrated so I just added a counter in the loop and outputted it as the first column for each separate query and figured this was good enough.
我修改了 Magoo 的答案以输出到 CSV 文件。它将输出两列“#, Input, Output”,其中每个成功的 nslookup 调用都有两行,一行是主机名,另一行是 IP。我还修改了 delims 以分隔“:”和“”,以便输出没有一堆额外的空格。我想在一行中包含每个查询,其中包含“输入、主机名、IP”列,但我感到沮丧,所以我只是在循环中添加了一个计数器并将其作为每个单独查询的第一列输出,并认为这已经足够了。
@echo OFF
setlocal enabledelayedexpansion enableextensions
set me=%~n0
set parent=%~dp0
set outputfile=%parent%nslookup_results.csv
set inputfile=%parent%nslookup_input.txt
@echo #,Input,Output >> %outputfile%
FOR /F %%G in (%inputfile%) do (
set /a resultcount+=1
FOR /F "skip=3 tokens=2 delims=: " %%J in ('echo(^|nslookup %%G') do @echo !resultcount!,%%G,%%J >> %outputfile%
)
回答by Jace Nimz
@Magoo had the best answer for my needs. The batch file works as intended once I fixed a major issue: Kids, DON'T name your batch file with a windows internal command name. I had called the batch file "nlookup.bat" which caused MAJOR brain damage; a lesson I'll not soon forget.
@Magoo 为我的需求提供了最佳答案。一旦我解决了一个主要问题,批处理文件就会按预期工作:孩子们,不要使用 Windows 内部命令名称命名您的批处理文件。我将批处理文件称为“nlookup.bat”,它导致了严重的脑损伤;我不会很快忘记的一课。
Thank you all, this is working well now!
谢谢大家,现在运行良好!
回答by npocmaka
try like this:
试试这样:
@echo off
set "OUTPUTFILE=Results\Results.txt"
set "lookup=HostNames.txt"
FOR /F %%i in (%lookup%) do (
FOR /F "usebackq skip=3 delims=: tokens=2" %%j in (`nslookup %%i`) do (
@echo %%i %%j >> %OUTPUTFILE%
)
)