-1

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.

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • Uh, why are you generating the script every time? Your question would be much simpler to answer if you just posted the generated script, regardless. – tripleee Mar 20 '16 at 06:16
  • The script from the site would generate a file `/tmp/www/wlan.html` if you uncommented those lines. – tripleee Mar 20 '16 at 06:21
  • ... Though it's a tad clumsy with the temp file. Without sample output from `wl assoclist` and `getleases` it's not really clear what it's doing. – tripleee Mar 20 '16 at 06:24
  • @tripleee Running *wl assoclist* gives me no output... – StealthRT Mar 20 '16 at 06:29

2 Answers2

1

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).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • for the **while true** it continuously says **could not open input file: No such file or directory** – StealthRT Mar 23 '16 at 01:09
  • Did you forget the shebang, or did you put a non-breaking space? – tripleee Mar 23 '16 at 04:58
  • See update now for proper JSON quotes and trimmed parens from the IP address field. – tripleee Mar 23 '16 at 08:00
  • would you mind modifying your answer to show hoe to put that info into a html page that can be called? – StealthRT Nov 19 '16 at 22:44
  • That's a too broad requirement to easily fit into this question. I'm guessing you are asking about CGI but this is probably best covered in a new, separate question with detailed requirements. – tripleee Nov 20 '16 at 09:43
0

If interested in a JSON formated version:

for this DD-WRT arp format:

root@myrouter:~# arp
myhost1.mysite.com (192.168.2.12) at 08:0b:05:04:08:04 [ether]  on br0 
myhost2.mysite.com (192.168.2.21) at 18:1b:15:14:18:14 [ether]  on br0 ...

The following command:

arp | awk 'BEGIN { print "[" } {gsub(/[\(\)]/,""); print "  {\"MAC\": \"" $4 "\",\n   \"IP\": \"" $2 "\",\n   \"Address\": \"" $1 "\",\n   \"Iface\": \"" $7 "\"},"} END { print "]" }'

Returns:

[
  {"MAC": "08:0b:05:04:08:04",
   "IP": "192.168.10.12",
   "Address": "myhost1.mysite.com",
   "Iface": "br0"},
  {"MAC": "18:1b:15:14:18:14",
   "IP": "192.168.10.21",
   "Address": "myhost2.mysite.com",
   "Iface": "br0"},
]
Martro
  • 1