OpenWrt Forum Archive

Topic: Changing Wan Mac Address on boot

The content of this topic has been archived on 29 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

I am using Kabel Internet and need to change the Wan Mac Address to change the IP Address.
After changing the Wan Mac Address the Router needs to reboot to receive a new IP Address.

So I have to do these Steps to change my IP:

1. Browse to 192.168.1.1
2. Logg into Luci
3. Go to the Mac Settings
4. Change Mac
5. Restart Router

My opinion is that this is quite much clicking and typing.

Is there any Script that changes the Mac before trying to connect to the Modem?
That would save a lot of time and maintain oneĀ“s cool.

Hehe, this is exactly what I'm doing smile

root@wr841n:~# vi /etc/rc.local
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

uci set network.wan.macaddr=aa:bb:cc:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`
uci commit

exit 0

Just change aa, bb, cc to some registered mac. You can of course change all the digits. There's macchanger package available, but it eats valuable flash memory, my script is a lot smaller wink This makes you'll get the new MAC next time you reboot / restart wan interface.

(Last edited by nozombian on 30 Dec 2013, 15:19)

Just one side note though, you set the mac after the network becomes active.
This means it uses the old settings for the mac address. I would make it into a init script and also make it only commit the network part.

#!/bin/sh /etc/rc.common
 
START=17

uci set network.wan.macaddr=aa:bb:cc:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`

uci commit network

Store this file to /etc/init.d/macchgr execute chmod +x /etc/init.d/macchgr and last /etc/init.d/macchgr enable

Now the mac will be updated just before the firewall and network init. The STOP= ommited since there is nothing to stop wink
When you do save the file to the router, make sure that you save it with the correct line endings Windows and linux differ on that.

(Last edited by FriedZombie on 30 Dec 2013, 17:58)

FriedZombie wrote:

Just one side note though, you set the mac after the network becomes active.
This means it uses the old settings for the mac address. I would make it into a init script and also make it only commit the network part.

#!/bin/sh /etc/rc.common
 
START=17

uci set network.wan.macaddr=aa:bb:cc:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`
uci commit network

Store this file to /etc/init.d/macchgr execute chmod +x /etc/init.d/macchgr and last /etc/init.d/macchgr enable

Now the mac will be updated just before the firewall and network init. The STOP= ommited since there is nothing to stop wink
When you do save the file to the router, make sure that you save it with the correct line endings Windows and linux differ on that.

Thx for the fast reply to you both.
But I have no clue how to install this script.
Do I have to use SSH?

s.aaron wrote:

But I have no clue how to install this script.
Do I have to use SSH?

Yes, if you follow FriedZombie's advice and place it to /etc/init.d/macchgr.

If you settle for nozombian's rc.local solution, you can do the editing from Luci ( --> System --> startup --> local startup script). But like already said, with this solution the new mac gets applied the next time you reboot the router (or touch network settings causing a reload for network components). It is not applied for the current boot process.

And please note that there are only 2 command lines, both starting with "uci". The first one is really long and gets wrapped here in forum.

(Last edited by hnyman on 30 Dec 2013, 17:10)

There I unwrapped my example smile

anyway, you have to use ssh to add my script.
connect to the router with ssh and login with your credentials.

type/copy the command: cat >  /etc/init.d/macchgr
Now copy and paste the script:

#!/bin/sh /etc/rc.common
 
START=17

uci set network.wan.macaddr=aa:bb:cc:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`

uci commit network

Make sure the last line is empty (if that's not the case, hit enter).
After this press [crtl]+d and you now have the new file /etc/init.d/macchgr

Make the file executable with typing/copying the command: chmod +x /etc/init.d/macchgr
The last step to make it start is simply executing the command /etc/init.d/macchgr enable

To see if the script works you can start it manually in the cli by typing /etc/init.d/macchgr start
If you see no errors (or any output for that matter). it works.

(Last edited by FriedZombie on 30 Dec 2013, 18:18)

FriedZombie wrote:

There I unwrapped my example smile

anyway, you have to use ssh to add my script.
connect to the router with ssh and login with your credentials.

type/copy the command: cat >  /etc/init.d/macchgr
Now copy and paste the script:

#!/bin/sh /etc/rc.common
 
START=17

uci set network.wan.macaddr=aa:bb:cc:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`:`head /dev/urandom |
                    tr -dc "0123456789abcdef" | head -c2`

uci commit network

Make sure the last line is empty (if that's not the case, hit enter).
After this press [crtl]+d and you now have the new file /etc/init.d/macchgr

Make the file executable with typing/copying the command: chmod +x /etc/init.d/macchgr
The last step to make it start is simply executing the command /etc/init.d/macchgr enable

To see if the script works you can start it manually in the cli by typing /etc/init.d/macchgr start
If you see no errors (or any output for that matter). it works.

Thx for that easy guide.
It works now!
Thx to all

well,

First, I never see anyone successfully make the Macchanger.ipk work,
tried, failed.

Second, any better script to create the random mac without fixing the first 24 digits?

Thanks.

eeff11 wrote:

Second, any better script to create the random mac without fixing the first 24 digits?

If you want all bytes randomized, just replace the fixed bytes with three more "head ..." command blocks.

Starting it before network comes up is surely better idea, though I haven't that knowledge before smile To change all the digits, and to better imagine how the script works:

echo    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:\
    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:\
    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:\
    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:\
    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`:\
    `head /dev/urandom | tr -dc "0123456789abcdef" | head -c2`

The downside of changing all the digits is, that you will sooner or later generate one not registered, which could be even more suspicious, that you are faking your mac.

well I shorter way to generator a random mac:

echo $(dd if=/dev/urandom bs=1024 count=1 2>/dev/null|
   md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/')

To get a semi fixed part, just remove some stuff at the end wink example with aa:bb:cc

echo $(dd if=/dev/urandom bs=1024 count=1 2>/dev/null|
   md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/aa:bb:cc\1:\2:\3/')

It gave me headache, how you made it, but I got it eventually smile That's a clever one and problably faster too. How many regexp maniacs are needed to change a bulb? big_smile

Hi,

This is my first post on the forum.  In case anyone is still interested here's a bit shorter way to generate a random MAC. The idea is to use hexdump(1):

  00`hexdump -n5 -e'5/1 ":%02x"' /dev/urandom`

This will create a MAC starting with 00 and having 5 random octets separated by colons ':' .  If you want something different, adjust the starting 00 and the two 5's accordingly. smile

So your command would be:

uci set network.wan.macaddr=00`hexdump -n5 -e'5/1 ":%02x"' /dev/urandom`

I'm new to OpenWRT and haven't tried this with uci yet ... need to learn more about uci.  I had set the MAC in luci and in /etc/config/network, but things became unstable.  If I can't get it to work I should start another thread.

(Last edited by serval on 26 Jan 2014, 21:57)

Welcome to the club and thanks for another tip. There are probably 101 neat ways how to fake your mac addr smile

Tested your solution and it works fine even with 3 digits I need:

root@wr1043nd:~# cat /etc/init.d/macchgr
#!/bin/sh /etc/rc.common
START=17
uci set network.wan.macaddr=aa:bb:cc`hexdump -n3 -e'3/1 ":%02x"' /dev/urandom`
uci commit network

(Last edited by nozombian on 1 Apr 2014, 23:31)

Thanks for the welcome, nozombian, and sorry for the delayed reply, I'm just returning to look at OpenWRT again for a different reason.

I just wanted to add to this thread, that before I "discovered" the hexdump-based method, I was using this kind of construct based on xxd :

00$( xxd -l 5 -p < /dev/urandom )

xxd is part of vim, IIRC. Anyway, I decided to switch to hexdump since it seems a little more "universal" (no need for vim).

101 ways indeed! wink

The discussion might have continued from here.