linuxbashshellcrondd-wrt

DD-WRT Shell script Wifi IP and Name addition


Hello everyone I am running DD-WRT v.3.0 on my Linksys EA6500 router and I have the following script running in order to give me access to the WiFi MAC address that are currently connected to the router:

echo "#!/bin/ash" > /tmp/getmac.sh
echo 'echo { > /tmp/www/list.html' >>/tmp/getmac.sh
echo "for i in \$(arp | awk '{print toupper(\$4)}'); do echo \$i, >> /tmp/www/list.html; done" >>/tmp/getmac.sh
echo 'echo } >> /tmp/www/list.html' >>/tmp/getmac.sh
chmod +x /tmp/getmac.sh
/tmp/getmac.sh

I can visit http://192.168.1.1/user/list.html and it will show me a list of wifi MAC address that are currently connected to the router:

Example:

{ 01:81:18:3d:49:5e, 04:10:87:8c:47:9a, }

However, I would like to modify that to also include the IP ADDRESS and also the NAME OF THE DEVICE.

I found this on the DD-WRT website but when running the command and checking the directory, I do not see it anywhere.

 # mkdir -p /tmp/www
 while [ 1 ];
  do
  wl assoclist | awk '{print tolower($2)}' > /tmp/assocLIST
  # echo "<meta http-equiv="refresh" content="10"><b>Hostnames and IP addresses of WLAN clients</b> (last update: $(date))<p>" > /tmp/www/wlan.html
  while read assocLINE
   do
     dumpleases | awk '/'"$assocLINE"'/ {print "Hostname: " $1, "MAC: " $2, "IP: " $3}'
   # echo "<br>";
        done < /tmp/assocLIST     # >> /tmp/www/wlan.html
  sleep 10;
done;

I would like it to output like so:

{
    "data": [{
        "IP": "192.168.1.55",
        "MAC": "01:81:18:3d:49:5e",
        "HOST": "DavidsAndroidPhone"
    }, {
        "IP": "192.168.1.79",
        "MAC": "04:10:87:8c:47:9a",
        "HOST": "BobsIphone"
    }]
}

Could anyone help me out in modifying my first script I posted to include the IP and NAME?

UPDATE

When I do the command arp in PuTTYtel I get the following info:

DD-WRT login: root
Password:
==========================================================

     ___  ___     _      _____  ______       ____  ___
    / _ \/ _ \___| | /| / / _ \/_  __/ _  __|_  / / _ \
   / // / // /___/ |/ |/ / , _/ / /   | |/ //_ <_/ // /
  /____/____/    |__/|__/_/|_| /_/    |___/____(_)___/

                       DD-WRT v3.0
                   http://www.dd-wrt.com

==========================================================


BusyBox v1.24.1 (2016-03-07 05:09:22 CET) built-in shell (ash)

root@DD-WRT:~# arp
android-17af243062d3eb6b (192.168.1.144) at 00:ae:fa:4a:3a:4c [ether]  on br0

So currently the script I am running (getmac.sh) looks at this and gets only this:

{ 00:ae:fa:4a:3a:4c, }

So given that, how can I modify the script to get more of the information I am looking for in the proper JSON layout?

UPDATE 2

Ok I have this code here:

arp | awk 'BEGIN { print "{" } { print "MAC:" $4 ", IP:" $2 ", HOST:" $1} END { print "}" }'

Which outputs the following:

{
MAC:00:ae:fa:4a:3a:4c, IP:(192.168.1.144), HOST:android-17af243062d3eb6b
}

Now given that above, how can I remove the ( and the ) from the IP and format it in proper JSON form? I've tried awk -F'(' but that doesn't seem to work.


Solution

  • First off, your current script contains an incredible amount of cruft. It could be refactored to just

    arp | awk 'BEGIN { print "{" } { print toupper($4) } END { print "}" }'
    

    Without access to the output from dumpleases we can't really tell you how to add the IP address resolution, but arp -n on many Linuxes returns the IP address in $1, and you get the resolved address (if any) without -n. But looking at the script from the web site, all you want is probably really dumpleases. The script from the web site could be refactored to

    while true  # fix silly [ 1 ]
    do
      wl assoclist | awk '{print tolower($2)}' > /tmp/assocLIST
      dumpleases | awk 'NR==FNR { a[$1]++; next }
          $2 in a { print s "{"; print "\"Hostname\": \"" $1 "\";
             gsub("()", "", $2); print "\"MAC\": "\""" $2 "\"";
             print "\"IP\": \"" $3 "\""; print "}"; s=",\n" }' /tmp/assocLIST -
    done
    

    and if you don't care about the wl assoclist part or the endless loop, all you seem to need is to refactor the dumpleases output to JSON. That's the part after $2 in a in the script above. (I assumed the MAC address is what wl assoclist produces; without output samples, I just have to guess a lot of things here.)


    As for generating the script on the fly, that's just silly. Anything that (I am guessing a bit here again) looks like

    ssh busybox "echo 'echo moo' >/tmp/script
        chmod +x /tmp/script
        /tmp/script"
    

    could be rewritten without any loss of functionality as just

    ssh busybox 'echo moo'
    

    (and for what it's worth, if the script wasn't so useless, I would definitely recommend rewriting it to use a here document instead).