从txt回显IP位置

时间:2014-07-25 18:05:34

标签: batch-file ping nslookup

我目前正在尝试ping一个主机名列表,最好是一次一个,然后在该主机名上使用nslookup。如果nslookup中的主机名与首次使用的主机名匹配,那么我想使用IP来检查包含该位置的另一个文件(称为home.txt)。

到目前为止我所拥有的:

@Echo Off
If '%1'=='' GOTO Syntax
Echo Running Script and Saving Results to Results.CSV
Echo Script Run %date% %time% >> Results.csv
For /F %%i in (%1) do Call :StartPing %%i
Goto :eof

:StartPing
PING %1 -n 1        | FIND /i "TTL" > nul && goto Success
PING %1 -n 1        | FIND /i "timed" > nul && goto Timedout
PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg

:Success
for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a
set IPAddress=%Address::=%
echo %1, %IPAddress%,%Hostname%
echo %1, %IPAddress%,%Hostname% >> Results.csv

NSLOOKUP %IPAddress% | FIND /i "Name" = "%Hostname%" goto home

:home
echo %IPAddress% home.txt

Goto :EOF

:Timedout
Echo %1, Request timed out.
Echo %1, Request timed out. >> Results.csv

:ErrorMsg
Echo %1, Ping request could not find host.
Echo %1, Ping request could not find host. >> Results.csv
goto :eof

:Syntax
echo . . .
goto :eof

home.txt可能包含的示例:

10.102.6.43 = 2J

IE,只是一堆映射到办公地点的IP范围。

理想情况下,脚本应该弹出一个弹出框,显示IP地址的位置,或者只是在屏幕上回显它。

有什么想法吗?

做了一些改变并测试了一些东西,这是我到目前为止所做的。

@echo Off
@cls
if '%1'=='' GOTO Syntax
echo Running Script and Saving Results to Results.CSV
echo Script Run %date% %time% >> Results.csv
for /F %%i in (%1) do Call :StartPing %%i
goto :EOF

:StartPing
PING %1 -n 1| FIND /i "TTL" > nul && goto Success
PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg

:Success
for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

set IPAddress=%Address::=%

for /f "tokens=2" %%b in ('nslookup %IPAddress%^|find /i "Name"') do set fqdn=%%b

echo %1, %IPAddress%,%Hostname%
echo %1, %IPAddress%,%Hostname% >> Results.csv
goto :EOF

:Timedout
Echo %1, Request timed out.
Echo %1, Request timed out. >> Results.csv

:ErrorMsg
Echo %1, Ping request could not find host.
Echo %1, Ping request could not find host. >> Results.csv
goto :EOF

:Syntax
echo . . .
goto :EOF

:EOF
echo this is the END OF FILE
pause

每次运行hh.bat host.txt时,一切运行正常,直到它到达nslookup部分,然后在Windows任务管理器中创建1500到3000个进程。没有NSLOOKUP部分hh.bat工作正常。

我将nslookup部分放在一个名为nslookup.bat的单独脚本中,它运行良好一段时间,现在它不想工作。每次创建那么多的进程......

在具有3个Windows 7个和DC的Hyper-V环境中进行测试。

0 个答案:

没有答案