Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tonysathre
I have a batch script I wrote to get the MAC address of a remote computer. The way it works now, you have to run it in interactive mode. I would like to be able to run it from the command line, such as: getmac ip_address, and have it run the script for ip_address. If someone could help me I would appreciate it.
@echo off
@if /i "%1" == "/?" goto help
@if /i "%1" == "-h" goto help
@if /i "%1" == "--help" goto help
@if /i "%1" == "/h" goto help:start
cls
@echo offtitle getMAC
echo.
echo.
echo.
echo Clearing ARP Cache . . .
netsh interface ip delete arpcacheecho Enter IP address of remote computer:
set /p ip=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.
:fail
@echo off
echo.
cls
echo.
echo Host not found, or not alive
echo.
goto eof:eof
echo.
pause
goto start:help
@echo offecho.
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 To use getMAC interactively, run getMAC with no arguments.
echo.Thanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

Hi Tony,
You can change this:
echo Enter IP address of remote computer:
set /p ip=to this:
set ip=%1
BTW, in the next line, not much point in a for statement, since it resolves to:
ping -n 1 %ip%
Nor do you need to prefix @ to your commands once echo is off. [And ONE echo off is enough.]
=====================================
If at first you don't succeed, you're about average.M2

Thanks, but is there anyway to combine the two uses (if thats the correct word)? By that I mean, interactive mode and command line mode? When I run it from the command line, it doesn't let me put something else into %ip%. It just keeps looping with what ever value I specified on the command line for %ip%. I hope that clear enough. If you run the script you will see what I mean.
Thanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

Something like this:
if %1'==' goto :interact
=====================================
If at first you don't succeed, you're about average.M2

Okay, I got it working now, thanks a lot. Now I want to add hostname resolution, so that it can be run for a given hostname, or IP address. I tried but couldn't get it working. I think 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. Then compare the variables. If the 2 variables values are ==, then run the arp command for that variable, else goto :EOF.
Does that sound correct?
Here's the 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
clsecho.
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 endfor %%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 offecho.
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 To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1:end
title Command PromptThanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

@echo off
setLocal EnableDelayedExpansionif %1'==' goto :get
set ip=%1:get
echo Enter IP address of remote computer: [0 to quit]
set /p ip=
if %ip%==0 goto :eof
goto :get
=====================================
If at first you don't succeed, you're about average.M2

Hey M2, thanks, but that doesn't do anything different than my current script. What I need is hostname resolution. I want to be able to run the script like this:
getmac [hostname]
or
getmac [ip_address]
and get the same results for the MAC address.
If you don't understand, ask please.
Thanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

Hi Tony,
What I posted gets you the ip either command line or interactivr. Once you've got that, do your PING and ARP.
=====================================
If at first you don't succeed, you're about average.M2

Okay, but I already got that part working. Now the only thing I need is the script to support IP's AND hostnames>/b>. Any ideas on how to add that in?
Thanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

What is this trying to do:
arp -a | findstr "%%i"
=====================================
If at first you don't succeed, you're about average.M2

That finds the IP address entered at the command line or prompt, in the ARP cache.
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

You lost me. Post your script.
=====================================
If at first you don't succeed, you're about average.M2

Here you go. Sorry if I'm not being clear enough.
@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
clsecho.
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 endfor %%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 offecho.
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 To use interactively, run getMAC with no parameters.
echo.
echo EXAMPLE:
echo.
echo getmac 192.168.1.1:end
title Command Prompt
Thanks,Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

Hi Tony,
I chopped out some of it so it wouldn't be six blocks long. I think you'll figure it out.
Notes:
eof is a "built-in 'label' [?]" and I think that redeclaring it anywhere else is a bad idea. Weirdly, you can not "goto eof" you must "goto :eof".
I think the main snag was that ping does not set errorlevel 1 if it just times out.
Also, this:
set ip= %1
is NOT the same as:
set ip=%1
In this script it doesn't matter much, but in others it may cause you grief.
::== tony9.bat
@echo off
set ip=%1
if /i "%1" == "" (goto :start
)else goto :auto
:start
echo Clearing ARP Cache . . .
echo netsh interface ip delete arpcache
echo Enter IP address of remote computer:
set /p ip=
if %ip% == exit goto :eof
for %%i in (%ip%) do (ping -n 1 %%i |find /i "reply" > NUL
if errorlevel 1 goto :fail
echo IP Address MAC Address Type
arp -a | findstr "%%i"
goto :eof
)
:auto
ping -n 1 %1 |find /i "reply" > NUL
if errorlevel 1 goto :fail2
arp -a | findstr "%1"
goto :eof
:fail
echo Host not found, or not alive
goto :eof
:fail2
echo Host not found, or not alive
goto :eof
pause
goto :start
::== DONE
=====================================
If at first you don't succeed, you're about average.M2

Hey M2, that also does the same thing as my script. I think your misunderstanding what I need accomplished. My script already works in interactive mode and command line mode just fine, so the last 2 scripts you have posted have already been taken care of by me.
What I need done is hostname resolution so that I can run the script like this:
getmac hostname
or
getmac ip-address
and get the same results for the ARP command.
Thanks,
Tony
login: yes
password: dont have onepassword is incorrect
login: yes
password: incorrect

This looks relevant to what I am trying to do. Is it possible to find a target computer's MAC address by its Internet IP address? (Internet, not local network)

![]() |
C++ prog to read every e-...
|
Makefile Dependencies
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |