Change IP address for a given MAC address

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Change IP address for a given MAC address

#1 Post by kahlenberg » 24 Jun 2016 02:22

Hello,

I have seen a lot of batch script that get MAC address or change IP address for a ethernet card. None of them worked for me. I need a batch script that will be able to get MAC addresses and compares MACs with a given MAC as parameter, and find the name of the ethernet card, and then set the IP address for this name. I am using win8.1 German, so it should also work in English Windows but this functionality can be ignored for the first step.

So What I need:

1) Parse ipconfig /all command and find the name of the ethernet card for a MAC address, that is given as parameter.

2) Take the name of the ethernet card and set a new IP address for this name with command netsh int ip set address...

Problems:
getmac command outputs following:

Code: Select all

Physisch. Adresse   Transportname
=================== ========================
11-22-33-44-55-66   Nicht zutreffend
77-88-99-AA-BB-CC   Medien ausgeworfen
DD-EE-FF-00-11-22   Medien ausgeworfen
Nicht zutreffend    Hardware nicht vorhanden
00-11-22-33-44-55   Medien ausgeworfen


As you can see, getmac command doesn't display the name of the interface.

To cut a long story short, the batch script takes a MAC address and IP address as parameter and change the IP address for this interface that has that given MAC.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change IP address for a given MAC address

#2 Post by foxidrive » 24 Jun 2016 05:09

Test this with %1 as the mac address - it should just echo the interface name for your mac address to add to the netsh command.

Code: Select all

@echo off
set "interface="
for /f "tokens=1 delims=," %%a in ('getmac /v /fo csv ^|find /i "%~1" ') do set interface=%%~a
if defined interface echo netsh int ip set address with "%interface%"
pause

kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Re: Change IP address for a given MAC address

#3 Post by kahlenberg » 27 Jun 2016 07:30

Hi,
Thanks for reply. I changed the script a little bit for my purposes, but it doesn't work, it means it is not changing the ip address. If I type the exactly same command in command line, it works, it changes the ip address. I think the script has a syntax error somewhere.

This works:

Code: Select all

D:\Scripts>netsh interface ip set address name="Ethernet 2" static 192.168.1.12 255.255.255.0 192.168.1.1


Script not:

Code: Select all

@echo off
set "interface_name="
for /f "tokens=1 delims=," %%a in ('getmac /v /fo csv ^|find /i "%~1" ') do set interface_name=%%~a
if defined interface_name echo netsh interface ip set address name="%interface_name%" static %2 %3 %4


If I switch on echo, it gives following output:

Code: Select all

D:\Scripts>ChangeIP.bat 11-22-33-44-55-66 192.168.1.12 255.255.255.0 192.168.1.1
netsh interface ip set address name="Ethernet 2" static 192.168.1.12 255.255.255.0 192.168.1.1

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Change IP address for a given MAC address

#4 Post by Squashman » 27 Jun 2016 12:20

The ECHO command will just print everything after the ECHO to the screen. It will not execute your NETSH command. If you need the NETSH command to execute then you need to remove the ECHO command.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change IP address for a given MAC address

#5 Post by foxidrive » 27 Jun 2016 22:49

kahlenberg wrote:Hi,
Thanks for reply. I changed the script a little bit for my purposes, but it doesn't work, it means it is not changing the ip address. If I type the exactly same command in command line, it works, it changes the ip address.


hehe You owe Squashman five paypal dollars. ;)

kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Re: Change IP address for a given MAC address

#6 Post by kahlenberg » 28 Jun 2016 01:16

Thank you! Now it works. OK, I owe 5 dollar :)

Post Reply