2011-06-12 50 views
1

我已經在perl中有一個小腳本來執行反向查找,但除非另一臺機器也安裝了perl,否則它不可移植。我想要一個可以在同事機器上無縫運行的腳本,也可以將其轉換爲自定義命令(通過更新PATH環境變量PATH &)。該腳本文件必須是可移植的,並且可供非管理員用戶使用。如何使用BATCH腳本調用Windows gethostbyaddr API

批處理腳本似乎符合這個目的,但我無法弄清楚如何調用gethostbyaddr API。我想VBScript也是一個選項,並且可以打開它。

gethostbyaddr API

+1

你不能調用從VBScript中的API獨自一人,如果你想有一個批處理文件是什麼,當談到NETBIOS反向查找有關調用NSLOOKUP – 2011-06-12 10:55:38

+0

NSLOOKUP是不是非常有用。在數千個客戶端的巨大LAN環境中,我們需要NETBIOS反向查找。 'NBTSTAT -A'很有用,但我不確定兩個結果是否相似。也許它也在內部使用gethostbyaddr API(或者也許是相反的方式)。運行反向查找的perl腳本始終提供最佳結果,但NBTSTAT輸出過於混亂,如果NETBIOS查找失敗,則不會交替反向DNS。 – Benny 2011-06-12 11:34:40

回答

0

這裏有兩個批處理腳本。希望至少有一個人可以提供幫助。

@ECHO OFF 
::Lookup.bat 
::http://www.computing.net/answers/programming/ip-by-hostname/25313.html 
::Takes input file of hostnames and creates csv file with hostnames and IP addresses. 
::Gets IP's using nslookup 
:: Output in file hostnames.csv in current folder 

if "%1"=="" goto :Syntax 

SET SCRIPTPATH=%~p0 
CD %SCRIPTPATH% 

del hostnames.csv 

for /f %%e in (%1) do call :LOOKUP %%e 
echo Done!!! 
goto :EOF 

:LOOKUP 
SET HOST1=%1 
FOR /F "skip=4 tokens=2 delims=:" %%A IN ('2^>NUL nslookup %1') DO (ECHO %1,%%A>>%SCRIPTPATH%hostnames.csv) 
GOTO :EOF 

:Syntax 
echo. 
echo Syntax: 
echo. 
echo %0 ^<FileName^> 
echo. 
echo where ^<FileName^> is a file containing a list of hostnames. 
echo. 
echo The batch file will return the results to file hostnames.csv in current folder 
goto :EOF 

@echo off 
::gethostname.bat 
::Takes input file of IP addresses and creates csv file with IP address and hostnames. 
::Gets hostnames using netBIOS, which is usually accurate. If no info avail from 
::netBIOS, then use nslookup. Plenty of debug statements in screen output. :) 
:: Output in file C:\TEMP\ip-resolved.csv 

if "%1"=="" goto :Syntax 

SET SCRIPTPATH=%~p0 
CD %SCRIPTPATH% 

echo ip,hostname,power>C:\TEMP\ip-resolved.csv 
for /f %%e in (%1) do call :PING-NBT %%e 
echo Done!!! 
goto :EOF 

:PING-NBT 
set ipaddress=%1 
set host1= 
echo. 
echo ***Pinging %ipaddress% 
SET ON=0 
PING -n 1 %1 | find /i "Reply from" >nul && SET ON=1 
echo Just parsed ping reply... host is %ON% 
REM Next line skips netBIOS check if host is offline 
IF "%ON%"=="0" GOTO :LOOKUP %ipaddress% %ON% 
echo Proceeding to nbtstat with host %on% 

REM The next lines check netBIOS name. 
echo nbt1-start 
for /f %%i in ('nbtstat -a %1^|find "<20>"') do @set host1=%%i 
echo nbt=%host1% power=%ON% 
REM If there isn't a NetBIOS name, then skips to nslookup section. 
REM echo %2 
if "%host1%"=="" goto :LOOKUP %1 %ON% 
ECHO %ipaddress%,%host1%,%ON% >> C:\TEMP\ip-resolved.csv 
echo nbt2-end 
goto :EOF 

:LOOKUP 
echo nslookup1-start 
for /f "tokens=2" %%i in ('nslookup %1^|find "Name:"') do @set host1=%%i 
echo nslookup=%host1% power=%2 
REM Next line=if host var 
rem if not "%host1%"=="%1" goto :EOF 
ECHO %ipaddress%,%host1%,%2 >>C:\TEMP\ip-resolved.csv 
set host1= 
echo nslookup2-end 
goto :EOF 

:Syntax 
echo. 
echo Syntax: 
echo. 
echo %0 ^<FileName^> 
echo. 
echo where ^<FileName^> is a file containing a list of IP addresses to 
echo   which we need the hostname. 
echo. 
echo The batch file will return the results via a file 
echo C:\TEMP\ip-resolved.csv 
goto :EOF 
相關問題