Change MAC when disconnected, please check and correct my script

Hi guys, I’m using a GL- AR750 connected to Wireless Network: Client “WOW FI - FASTWEB” (wlan-sta) with a WPA2-EAP-MSCHAPV2 certificate. Connection can works flawlessly for days, but sometimes its MAC address get banned so I need to randomize from admin panel (More Settings - MAC clone - Your Router -WAN-). Doing this way, I’m back online in 10 seconds.

I read a lot of useful articles and scripts here, and tried merging. The result is

start() {

server=8.8.8.8
ping -c2 ${server} > /dev/null
if [ $? -ne 0 ]
then

  interface="wlan-sta"
  current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
  new_mac=$(dd if=/dev/random bs=1 count=3 2>/dev/null | hexdump -C | head -1 | cut -d' ' -f2- | awk '{ print "34:36:3b:"$1":"$2":"$3 }')
  if [ $? -eq 0 ];
  	then
  	ifconfig $interface down
  	ifconfig $interface hw ether $new_mac
  	ifconfig $interface up
  		if [ $? -eq 0 ]; then
  			current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
        exit 0
     else
        exit 1

fi
}

But of course it won’t work, I don’t know the syntax of the script language
My goal is to save this file
/usr/bin/randomizer.sh
and use cron every minute
*/1 * * * * /usr/bin/randomizer.sh
to check if connection still works, otherwise change MAC on wlan-sta

No need to disconnect and reconnect from wlan-sta or restart wlan0 interface, so maybe the “interface up and down” part can be removed?

Please help me, thanks in advance

This should work. But I didn’t test.

start() {

server=8.8.8.8
ping -c2 ${server} > /dev/null
if [ $? -ne 0 ]
then

  interface="wlan-sta"
  current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
  new_mac=$(dd if=/dev/random bs=1 count=3 2>/dev/null | hexdump -C | head -1 | cut -d' ' -f2- | awk '{ print "34:36:3b:"$1":"$2":"$3 }')
  if [ $? -eq 0 ];
  	then
  	ifconfig $interface down
  	ifconfig $interface hw ether $new_mac
  	ifconfig $interface up
  		if [ $? -eq 0 ]; then
  			current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
                        exit 0
                 else
                         exit 1
                 fi
      fi
fi
}
start 

Thanks, will test immediately!
Tested, after disconnection it can’t reconnect at all, I must disable script and change MAC manually. And it doesn’t even work, MAC remain the same. I should test with wlan0 instead wlan-sta

Can I remove the following lines without problems or should they remain? Consider that wlan-sta remain connected to main network even when my MAC get banned (but without internet connection)

ifconfig $interface down
ifconfig $interface up
fi
fi

You cannot remove these. Instead you should debug the script a little bit.

You cannot do it this way for wireless sta
mac=$(cat /proc/interrupts|sha256sum|sed “s/^(…)(…)(…)(…)(…).*$/02:\1:\2:\3:\4:\5/”)
uci set wireless.default_radio0.macaddr=“$mac” >/dev/null 2>&1
wifi down; sleep 3; wifi

Thanks, how can I insert in the script?

Hi Rabbit, have you found a solution to change the mac address? I also need it for the wow fi.
I managed to ping google and take down and up the wlan-sta interface when unreachable.
The mac address change would do the trick!

You should ssh to the router and try script in the terminal.

Unfortunately no. I’m still using this script

#!/bin/ash
SERVER=8.8.8.8

ping -c2 ${SERVER} > /dev/null
if [ $? -ne 0 ]
then
echo “Offline”
ifconfig wlan-sta down
sleep 15
ifconfig wlan-sta up
else
echo “Online”
fi

saved in “/usr/bin/wifi_rebooter.sh” and activated with “*/1 * * * * /usr/bin/wifi_rebooter.sh” line using cron

But most of the times my router remain connected to WOW-FI network but without internet access, and when happen I must open web interface and change MAC manually, regaining internet access in few seconds. So a script would be the best option, but don’t know how to write it

Your script does not change mac address. Try

#!/bin/ash
server=8.8.8.8
ping -c2 ${server} > /dev/null
if [ $? -ne 0 ]
then

  interface="wlan-sta"
  current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
  new_mac=$(dd if=/dev/random bs=1 count=3 2>/dev/null | hexdump -C | head -1 | cut -d' ' -f2- | awk '{ print "34:36:3b:"$1":"$2":"$3 }')
  if [ $? -eq 0 ];
  	then
  	ifconfig $interface down
  	ifconfig $interface hw ether $new_mac
  	ifconfig $interface up
  		if [ $? -eq 0 ]; then
  			current_mac=$(ifconfig ${interface} | grep "HWaddr" | awk '{print$5}')
                        exit 0
                 else
                         exit 1
                 fi
      fi
fi

Tested from a day, luckily no disconnections today so mac didn’t change. Will continue testing and let all know

1 Like

It seems not to work, it does not change the mac address of wlan-sta

Confirmed, same here. But got no disconnections since running this script.

I discovered that I can get “mac banned” in few seconds by uploading whatsapp backup to google drive when connected to WOW-FI network, then I must change MAC or wait from minutes to hours, even my girlfriend learned how to change MAC manually from web interface when needed :slight_smile:

Maybe we can face the fact: Using script to do automation is more complicated. Let’s just do it manually.

I’m doing it manually since 3 years, not the best but I spared so much money compared to having an internet contract that I can live with that. But automatizing would be awesome. Luckily in the past 3 days I had no MAC bans at all, hope it will continue this way

In addition to the mac address replacement, is there a way to set a random client name too?

I don’t know, but I guess that this parameter isn’t needed to avoid disconnections. I wrote “FASTWEB” here since 1st day. But should I get disconnected with no automatic reconnection when I’m on PC, I’ll try changing this field instead of requiring a new random MAC