1

I have the following bash script running at startup on my WRT1900ac linksys:

USER="admin"
PASS="passhere"
PROTOCOL="http"
ROUTER_IP="192.168.1.1"

# Port to connect to which will provide the JSON data.
PORT=9898

while [ 1 ]
do
    # Grab connected device MAC addresses through router status page.
    MACS=$(curl -s --user $USER:$PASS $PROTOCOL://$ROUTER_IP/Status_Wireless.live.asp)

    # clear temp JSON file
    echo > temp.log

    # Get hostname and IP (just in case there is no hostname).
    for MAC in $(echo $MACS | grep -oE "wl_mac::[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}" | cut -c 9-);
    do
        grep 0x /proc/net/arp | awk '{print $1 " " $4}' | while IFS= read -r line
        do
        IP=$(echo $line | cut -d' ' -f1)
        MACTEMP=$(echo $line | cut -d' ' -f2)
        HOST=$(arp -a | grep $IP | cut -d' ' -f1)

        # if no hostname exists, just use IP.
        if [ "$HOST" == "" ]
        then
            HOST=$IP
        fi

        if [ "$MAC" == "$MACTEMP" ]
        then
            JSON="{'hostname' : '$HOST', 'mac_address' : '$MAC'}"
            echo $JSON >> temp.log
        fi

        done
    done

    # Provide the JSON formatted output on $PORT of router.
    # This allows one connection before closing the port (connect, receive data, close).
    # Port will reopen every 5 minutes with new data as setup in a cron job.
    echo -e "HTTP/1.1 200 OK\n\n $(cat temp.log)" | nc -l -p$PORT >/dev/null

    # Wait for 10 seconds and do it all over.
    sleep 10

done

And for some reason when I reboot the router and then try to visit http://192.168.1.1:9898 it just shows a blank page even though I have my android cell phone connected via wifi to the router and the router shows the MAC address on the status page.

What should be on that page is all the wireless MAC address that are currently connected to the router and displaying them out in JSON form.

StealthRT
  • 131
  • 3

1 Answers1

1

What you posted is not a bash script, it's a script that will be executed by some unspecified shell. A shell script should always begin with a shebang line. A bash script must begin with #!/usr/bin/env bash or #! followed by the path to bash on your system, typically #!/bin/bash.

I haven't looked at your script in detail, but chances are that it's failing because you use bash constructs but embedded Linux installations such as DD-wrt don't typically include bash, only BusyBox ash. To make your script work on DD-wrt, stick to portable sh constructs. You can use checkbashisms (available in most Linux distributions) to look for bash-specific constructs.

One bash-specific construct I've spotted is the == operator in conditionals, e.g. [ "$HOST" == "" ]. The portable way to write this is [ "$HOST" = "" ] or [ -z "$HOST" ]. Likewise [ "$MAC" = "$MACTEMP" ].

Also always put double quotes around variable substitutions unless you know why you need to leave them out. Your script may happen to work if your data never contains shell special characters, but don't count on luck, just type those ".