Batch script for loop & nslookup

主宰稳场 提交于 2019-12-12 04:07:26

问题


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.

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.


回答1:


@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.

do ( must be on the same physical line as its for

%i should be %%i

%j should be %%j

I've removed the usebackq and changed the quotes from backticks to single-quotes as it's not necessary to use usebackq here.


(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.

Since my setup differs radically from yours, I can do but primitive verification.




回答2:


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.

@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%
)



回答3:


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%
    )
)



回答4:


@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.

Thank you all, this is working well now!



来源:https://stackoverflow.com/questions/35950161/batch-script-for-loop-nslookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!