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.

https://docs.gl-inet.com/en/3/tutorials/ssh/

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

Trying now
All MACs saved, will check tomorrow if something change