0

Hey all 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.

Any BASH guru's here that can help spot the problem?

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • how do you run that script at startup? rc.local? If yes please post the contents of rc.local, or did you put the above script directly there? – zabeltech Nov 19 '16 at 18:35
  • @zabeltech I run it by going to **Administration>command** tab and saving it as the **startup script**. It looks like this in the GUI http://www.octanevpn.com/media/wysiwyg/ddwrt3.png – StealthRT Nov 19 '16 at 18:37
  • 2
    You should first debug this, before you run it as a startup script. See [How to debug a bash script?](http://stackoverflow.com/q/951336/1741542) for help. – Olaf Dietsche Nov 19 '16 at 18:40
  • 1
    does it work, when you run it by hand? also but #!/bin/bash at the beginning of the file. otherwise dd-wrt does not know which programm to use to execute this script – zabeltech Nov 19 '16 at 18:40
  • @OlafDietsche Thats not very helpful for someone like myself that has very limited knowledge of doing BASH scripts.... – StealthRT Nov 19 '16 at 18:42
  • @zabeltech Adding **#!/bin/bash** to the top doesnt seem like it made any difference sadly. – StealthRT Nov 19 '16 at 18:46
  • 1
    It is much easier to help you, when we can see the output and the commands of the script as it runs. That's the reason why I sugested the other question. Without this, someone might find the problem, but it's a lot of guessing instead of knowing. – Olaf Dietsche Nov 19 '16 at 20:00
  • there's at least one community that specifically supports DD-WRT. Did you look there for "getting started with DD-WRT"?. Given your comments, it seem you have found this script someplace, it has general directions for use, and now that those directions aren't working you're looking for help. Qs on StackOverflow need to be better focused, and with not expectation of hand-holding by the poster. You may find better/more experienced/patient help at the DD-WRT specific sites. Good luck. – shellter Nov 20 '16 at 04:30

1 Answers1

0

I think it should be

echo -e "HTTP/1.1 200 OK\n\n $(cat temp.log)" | nc -l -p$PORT 0.0.0.0 >/dev/null

zabeltech
  • 963
  • 11
  • 27
  • When doing that command in SSH on the router i get the error of **cat: can't open 'temp.log': No such file or directory** – StealthRT Nov 19 '16 at 18:57
  • yes because temp.log is written by your script, if you run the above command alone it willcertainly fail. since you're in over ssh anyway, try running your whole script and see what it tells you/or post that here – zabeltech Nov 19 '16 at 18:59
  • How do i paste the whole script in the windows using **PuTTY**? – StealthRT Nov 19 '16 at 18:59
  • first tip: stop using windows :) second, go in with putty then: 1. ```nano script.sh``` copy and paste it in 2. ```chmod +x script.sh``` 3. ```./script.sh``` – zabeltech Nov 19 '16 at 19:00
  • mhm ok either you need to find out which editor is installed on your system, or how to copy files with putty. when you have the contents on your router, continue with 2. – zabeltech Nov 19 '16 at 19:04
  • yeah I don't know how to go about installing that within ddwrt. sorry. any other way? – StealthRT Nov 19 '16 at 22:40