Skip to content

Instantly share code, notes, and snippets.

@Darkbat91
Last active August 9, 2019 08:44
Show Gist options
  • Save Darkbat91/5fbcbacb60f1ba33acc5601726937ca3 to your computer and use it in GitHub Desktop.
Save Darkbat91/5fbcbacb60f1ba33acc5601726937ca3 to your computer and use it in GitHub Desktop.
Fixes wifi Issues with the OpenWRT Routers with both client mode and AP Mode being active at the same time
#!/bin/sh
# Make the python file
mkdir -p /usr/share/wififix/
cat >/usr/share/wififix/fix.py <<EOL
import subprocess
def updatewireless():
edit = False
needdisable = True
# Open our temp wireless config with Write
tmp = open('/etc/config/wireless.tmp', "w")
# Open the file with read only permit
with open("/etc/config/wireless", "r") as f:
# use readline() to read the first line
line = f.readline()
while line:
# Find the character count of the space
count = line.find(' ')
# If the space is after the first character we have a heading
if count == 6:
# We found the next header
# Header of the config option
if 'radio' not in line:
# This is what we care about means we are on a client interface
edit = True
else:
# Radio interface lets not disable that
edit = False
# reset the need disable flag
needdisable = True
elif edit and needdisable and count == -1:
# We need to add the disable line since it doesnt already exist count being -1
# means we are at the end of a configuration block
# needdisable being true means we have not found that line in this block
tmp.write("\toption disabled '1'\n")
else:
# We are on a config line
if 'option disabled' in line and edit:
needdisable = False
# If the line containes a disabled option make sure we flip it to true
line = line.replace('0','1')
# write the line to the file
tmp.write(line)
# use realine() to read next line
line = f.readline()
# we are done gracefully close the file
tmp.close()
subprocess.call(["mv", "/etc/config/wireless.tmp", "/etc/config/wireless"])
ipcommand = subprocess.check_output(["ip", "a"])
if 'NO-CARRIER' in ipcommand:
print "Fixing wireless"
updatewireless()
subprocess.call("/etc/init.d/network restart", shell=True)
else:
print "All good"
EOL
# Inject the fix python to the init but after system has finalized
if grep -q "wififix" /etc/rc.local;
then
echo "Nothing to do WifiFix Already installed"
else
echo "writing Installing Wifi fix"
sed -i '/exit 0/i python /usr/share/wififix/fix.py' /etc/rc.local
fi
@Darkbat91
Copy link
Author

Darkbat91 commented Aug 8, 2019

Problem

OpenWrt wireless connections will not open the access point network if the client network is defined but the client network is unable to connect.
The below process indicates a solution to this problem.

Good wireless

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br-lan state UNKNOWN qlen 1000
    link/ether fa:02:a4:65:c8:29 brd ff:ff:ff:ff:ff:ff
7: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether fa:02:a4:65:c8:29 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
       valid_lft forever preferred_lft forever
    inet6 fd1d:9576:ba9e::1/60 scope global
       valid_lft forever preferred_lft forever
    inet6 fe80::f802:a4ff:fe65:c829/64 scope link
       valid_lft forever preferred_lft forever
8: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br-lan state UP qlen 1000
    link/ether 00:1c:c2:32:05:32 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::21c:c2ff:fe32:532/64 scope link
       valid_lft forever preferred_lft forever

Bad wireless

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br-lan state UNKNOWN qlen 1000
    link/ether fa:02:a4:65:c8:29 brd ff:ff:ff:ff:ff:ff
7: br-lan: <NO-CARRIER> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether fa:02:a4:65:c8:29 brd ff:ff:ff:ff:ff:ff
8: wlan0: <NO-CARRIER> mtu 1500 qdisc mq master br-lan state UP qlen 1000
    link/ether 00:1c:c2:32:05:32 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::21c:c2ff:fe32:532/64 scope link

Config Example

config wifi-device 'radio0'
        option type 'mac80211'
        option hwmode '11g'
        option path 'platform/10180000.wmac'
        option htmode 'HT20'
        option disabled '0'
        option country '00'
        option legacy_rates '1'
        option channel '4'

config wifi-iface 'default_radio0'
        option device 'radio0'
        option network 'lan'
        option mode 'ap'
        option ssid 'OpenWrt'
        option encryption 'psk2+tkip+ccmp'
        option key 'supersecretpassword'

config wifi-iface
        option network 'wwan'
        option ssid 'Hhonors'
        option encryption 'psk2'
        option device 'radio0'
        option mode 'sta'
        option bssid 'network bssid'
        option key 'othersupersecretpassword'
        option disabled '0'

fix

Flip all non connecting clients to disabled

service network restart

python Code

import subprocess
def updatewireless():
    edit = False
    needdisable = True
    # Open our temp wireless config with Write
    tmp = open('/etc/config/wireless.tmp', "w")
    # Open the file with read only permit
    with open("/etc/config/wireless", "r") as f:
        # use readline() to read the first line
        line = f.readline()
        while line:
            # Find the character count of the space
            count = line.find(' ')
            # If the space is after the first character we have a heading
            if count == 6:
                # We found the next header
                # Header of the config option
                if 'radio' not in line:
                    # This is what we care about means we are on a client interface
                    edit = True
                else:
                    # Radio interface lets not disable that
                    edit = False
                # reset the need disable flag
                needdisable = True
            elif edit and needdisable and count == -1:
                # We need to add the disable line since it doesnt already exist count being -1
                # means we are at the end of a configuration block
                # needdisable being true means we have not found that line in this block
                tmp.write("\toption disabled '1'\n")
            else:
                # We are on a config line
                if 'option disabled' in line and edit:
                    needdisable = False
                    # If the line containes a disabled option make sure we flip it to true
                    line = line.replace('0','1')
            # write the line to the file
            tmp.write(line)
            # use realine() to read next line
            line = f.readline()
    # we are done gracefully close the file
    tmp.close()
    subprocess.call(["mv", "/etc/config/wireless.tmp", "/etc/config/wireless"])
ipcommand = subprocess.check_output(["ip", "a"])
if 'NO-CARRIER' in ipcommand:
    print "Fixing wireless"
    updatewireless()
    subprocess.call("/etc/init.d/network restart", shell=True)
else:
    print "All good"

Make run on system after initialization

save somewere and add below line in /etc/rc.local

python /path/to/wififix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment