Computing.Net > Forums > Programming > getMAC.bat script

getMAC.bat script

Reply to Message Icon

Original Message
Name: tonysathre
Date: August 28, 2006 at 14:22:01 Pacific
Subject: getMAC.bat script
OS: Windows XP Pro SP2
CPU/Ram: P4 2.53 Ghz\ 768 MB
Model/Manufacturer: Dell Dimension 4550
Comment:

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 off

title getMAC

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

echo 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 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 To use getMAC interactively, run getMAC with no arguments.
echo.

Thanks,

Tony


login: yes
password: dont have one

password is incorrect

login: yes
password: incorrect


Report Offensive Message For Removal

Response Number 1
Name: Mechanix2Go
Date: August 28, 2006 at 15:19:06 Pacific
Subject: getMAC.bat script
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 2
Name: tonysathre
Date: August 28, 2006 at 15:37:03 Pacific
Subject: getMAC.bat script
Reply: (edit)

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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: August 28, 2006 at 15:42:49 Pacific
Subject: getMAC.bat script
Reply: (edit)

Something like this:

if %1'==' goto :interact


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 4
Name: tonysathre
Date: August 30, 2006 at 11:23:45 Pacific
Subject: getMAC.bat script
Reply: (edit)

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
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 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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: August 30, 2006 at 12:15:25 Pacific
Subject: getMAC.bat script
Reply: (edit)

@echo off
setLocal EnableDelayedExpansion

if %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



Report Offensive Follow Up For Removal


Response Number 6
Name: tonysathre
Date: August 30, 2006 at 12:40:35 Pacific
Subject: getMAC.bat script
Reply: (edit)

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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: August 30, 2006 at 12:58:08 Pacific
Subject: getMAC.bat script
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 8
Name: tonysathre
Date: August 30, 2006 at 13:08:06 Pacific
Subject: getMAC.bat script
Reply: (edit)

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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 9
Name: Mechanix2Go
Date: August 30, 2006 at 13:19:38 Pacific
Subject: getMAC.bat script
Reply: (edit)

What is this trying to do:

arp -a | findstr "%%i"


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 10
Name: tonysathre
Date: August 30, 2006 at 13:27:23 Pacific
Subject: getMAC.bat script
Reply: (edit)

That finds the IP address entered at the command line or prompt, in the ARP cache.

login: yes
password: dont have one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 11
Name: Mechanix2Go
Date: August 30, 2006 at 13:29:57 Pacific
Subject: getMAC.bat script
Reply: (edit)

You lost me. Post your script.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 12
Name: tonysathre
Date: August 30, 2006 at 13:35:36 Pacific
Subject: getMAC.bat script
Reply: (edit)

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
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 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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 13
Name: Mechanix2Go
Date: August 30, 2006 at 14:58:46 Pacific
Subject: getMAC.bat script
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 14
Name: tonysathre
Date: August 30, 2006 at 15:10:44 Pacific
Subject: getMAC.bat script
Reply: (edit)

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 one

password is incorrect

login: yes
password: incorrect


Report Offensive Follow Up For Removal

Response Number 15
Name: robfromaaa
Date: September 13, 2006 at 00:59:33 Pacific
Subject: getMAC.bat script
Reply: (edit)

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)


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: getMAC.bat script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software