Batch file question

T

tonysathre

I need to add hostname resolution to my getMAC.bat script. Right now it
only works with IP addresses. I found this:
http://www.robvanderwoude.com/files/hostname_nt.txt script that gets
the hostname for a given IP, and ECHO's it to stdouot. How can I
integrate that that for loop into my existing script?

@echo off

set ip= %1
if /i "%1" == "?" goto help
if /i "%1" == "/?" goto help
if /i "%1" == "-h" goto help
if /i "%1" == "--help" goto help
if /i "%1" == "/h" goto help
if /i "%1" == "" (goto start
)else goto auto

:start
cls

echo.
echo.
echo.
echo Clearing ARP Cache . . .
netsh interface ip delete arpcache


echo Enter IP address of remote computer:
set /p ip=
if %ip% == exit goto end
if %ip% == quit goto end

for %%i in (%ip%) do (ping -n 1 %%i > NUL
if errorlevel 1 goto fail

cls
echo.
echo IP Address MAC Address Type
echo ___________________________________________________
echo.
arp -a | findstr "%%i"
goto eof
)

echo.

:auto
ping -n 1 %1 > NUL
if errorlevel 1 goto fail2
cls
echo.
echo IP Address MAC Address Type
echo ___________________________________________________
echo.
arp -a | findstr "%1"
echo.
goto end


:fail
echo.
cls
echo.
echo Host not found, or not alive
echo.
goto eof

:fail2
echo.
cls
echo.
echo Host not found, or not alive
echo.
goto end

:eof
echo.
pause
goto start

:help
@echo off

echo.
echo DESCRIPTION:
echo Gets the MAC address for a remote computer, given it's IP address.
echo.
echo USAGE:
echo getmac [ip_address]
echo.
echo PARAMETERS:
echo

echo To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1

:end
title Command Prompt


Thanks,

Tony
 
P

Pegasus \(MVP\)

tonysathre said:
I need to add hostname resolution to my getMAC.bat script. Right now it
only works with IP addresses. I found this:
http://www.robvanderwoude.com/files/hostname_nt.txt script that gets
the hostname for a given IP, and ECHO's it to stdouot. How can I
integrate that that for loop into my existing script?

@echo off

set ip= %1
if /i "%1" == "?" goto help
if /i "%1" == "/?" goto help
if /i "%1" == "-h" goto help
if /i "%1" == "--help" goto help
if /i "%1" == "/h" goto help
if /i "%1" == "" (goto start
)else goto auto

:start
cls

echo.
echo.
echo.
echo Clearing ARP Cache . . .
netsh interface ip delete arpcache


echo Enter IP address of remote computer:
set /p ip=
if %ip% == exit goto end
if %ip% == quit goto end

for %%i in (%ip%) do (ping -n 1 %%i > NUL
if errorlevel 1 goto fail

cls
echo.
echo IP Address MAC Address Type
echo ___________________________________________________
echo.
arp -a | findstr "%%i"
goto eof
)

echo.

:auto
ping -n 1 %1 > NUL
if errorlevel 1 goto fail2
cls
echo.
echo IP Address MAC Address Type
echo ___________________________________________________
echo.
arp -a | findstr "%1"
echo.
goto end


:fail
echo.
cls
echo.
echo Host not found, or not alive
echo.
goto eof

:fail2
echo.
cls
echo.
echo Host not found, or not alive
echo.
goto end

:eof
echo.
pause
goto start

:help
@echo off

echo.
echo DESCRIPTION:
echo Gets the MAC address for a remote computer, given it's IP address.
echo.
echo USAGE:
echo getmac [ip_address]
echo.
echo PARAMETERS:
echo

echo To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1

:end
title Command Prompt


Thanks,

Tony

Try this:

@echo off
set ip=%1
if "%1"=="" goto start
echo %1 | find "." > nul
if ErrorLevel 1 (goto help) else (goto auto)

:start
echo.
echo Clearing ARP Cache . . .
netsh interface ip delete arpcache

echo Enter IP address of remote computer:
set /p ip=
if %ip% =="" goto end

ping %ip% | find /i "bytes=" > nul || goto fail
arp -a %ip%
goto eof
===================
:auto
ping -n 1 %1 | find /i "bytes=" > nul || goto fail
arp -a | findstr "%1"
goto end
===================
:fail
echo.
echo.
echo Host not found, or not alive
echo.

:eof
echo.
pause
goto start
======================
:help
echo.
echo DESCRIPTION:
echo Gets the MAC address for a remote computer, given its IP address.
echo.
echo USAGE:
echo getmac [ip_address]
echo.
echo PARAMETERS:
echo

echo To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1

:end
echo.
title Command Prompt
 
T

tonysathre

Thanks, but that didn't work. It works with IP's like the one I wrote,
but not hostnames. The problem is caused by the arp command not
supporting hostnames. What I think needs to be done is the hostname
given needs to be put into a variable, and then the IP needs to be
found for that hostname and put into another variable. If the 2
variables values are ==, then run the arp command for that variable.

Thanks,

Tony
 
P

Pegasus \(MVP\)

tonysathre said:
Thanks, but that didn't work. It works with IP's like the one I wrote,
but not hostnames. The problem is caused by the arp command not
supporting hostnames. What I think needs to be done is the hostname
given needs to be put into a variable, and then the IP needs to be
found for that hostname and put into another variable. If the 2
variables values are ==, then run the arp command for that variable.

Thanks,

Tony

What you write is incorrect: arp.exe works with IP addresses
and also with host names - see below.

Your statement "If the 2 variables values are ==, then run the arp
command for that variable." is unclear. What "2 variables"?

@echo off
set ip=%1
if "%1"=="" goto start
echo %1 | find "." > nul
if ErrorLevel 1 (goto help) else (goto auto)

:start
echo.
echo Clearing ARP Cache . . .
netsh interface ip delete arpcache

echo Enter IP address of remote computer:
set /p ip=
if %ip% =="" goto end

ping %ip% | find /i "bytes=" > nul || goto fail
call :Sub
arp %switch% %ip%
goto eof
===================
:auto
ping -n 1 %ip% | find /i "bytes=" > nul || goto fail
call :Sub
arp %switch% | findstr "%1"
goto end
===================
:fail
echo.
echo.
echo Host not found, or not alive
echo.

:eof
echo.
pause
goto start
======================
:help
echo.
echo DESCRIPTION:
echo Gets the MAC address for a remote computer, given its IP address.
echo.
echo USAGE:
echo getmac [ip_address]
echo.
echo PARAMETERS:
echo

echo To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1

:end
echo.
title Command Prompt
goto :eof
=============
:Sub
for /F "tokens=1-4 delims=." %%a in ('echo %IP%') do set I1=%%a%%b & set
I2=%%c%%d
set /a x=%I1% + 1 - 1
set /a y=%I2% + 1 - 1
if %x%==%I1% if %y%==%I2% (set switch=-a) else (set switch=-A)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top