0

我目前正在尝试 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 pcs 和一个 DC 的 Hyper-V 环境中进行测试。

4

0 回答 0