I use something like this fast solution:
awk '$4~/[1-9a-f]+/&&$6~/^wl/{print "ip: "$1" mac: "$4}' /proc/net/arp
The /[1-9a-f]/
filter removes MAC addresses like 00:00:00...
from listing.
The /^wl/
filter removes all non-wireless interfaces (without "wl" at the beginning of their names).
The example result is:
ip: 192.168.0.1 mac: 64:6e:ea:d1:d3:0a
ip: 192.168.0.2 mac: a0:d3:a1:6d:d0:4a
Also, for my convenience, I use a file macs
with list of mac addresses of this context:
90:94:97:9f:85:10 My Huawei
64:6e:ea:d1:d3:0a Rostelecom Router
To get a list of connected devices with parsing this file I use such command (the END section is unnecessary - it's an updater):
awk 'BEGIN{while((getline<"macs")>0){nmm=$0;gsub($1" ","",nmm);nmz[$1]=nmm}}$4~/[1-9a-f]+/&&$6~/^wl/{print $1 " " (nmz[$4]?nmz[$4]:$4);if(!nmz[$4]){nmz[$4]=$4;update=1}}END{if(update){system("gawk -f macupd.awk")}}' /proc/net/arp
So in this way the result will be:
192.168.0.1 Rostelecom Router
192.168.0.74 My Huawei
The macupd.awk
is the script for updating macs
file. It gets vendor names from file oui.txt (located in the same directory) and uses them as descriptions of unknown hosts/clients connected to your machine and writes them to the macs
list file (you may disable this by removing END section from command). Here is the script:
#!/usr/bin/gawk -f
BEGIN{
while((getline<"macs")>0){
str++
if($0!~"(^#|^$)"){
nam=$0
gsub($1" ","",nam)
macnamz[$1]=nam
}
}
while((getline<"/proc/net/arp")>0){
if($4~/[1-9a-f]+/&&$6~/^wl/){
if(!macnamz[$4]){
mac=macv=$4
gsub(":","",macv)
vendor=substr(macv,1,6)
rs=RS
RS="\n|\r"
while((getline<"oui.txt")>0){
if(toupper($1)~toupper(vendor)){gsub(/.*\t+/,"");macnamz[mac]=$0;update=1}
}
RS=rs
}
}
}
if(update){
for(i in macnamz){print i " " macnamz[i] > "macs"}
print "" > "macs"
}
}
So if in the first running of my imperfect command the result will be (if all hosts where unknown):
192.168.10.1 00:d0:ef:aa:ee:ff
192.168.10.2 f4:bd:9e:00:00:00
192.168.10.3 00:22:72:11:22:33
The second running will be such:
192.168.10.1 IGT
192.168.10.2 Cisco Systems, Inc
192.168.10.3 American Micro-Fuel Device Corp.
The description of my script.
getline<"macs"
- Reading lines from list
if($0!~"(^#|^$)")
- Ignoring commented and empty lines.
gsub($1" ","",nam)
- I used nam
variable for description (everything after space).
macnamz[$1]=nam
- Final conversion of line to a part of associative array (there can not be 2 duplicated MACs - 2nd will overwrite 1st).
getline<"/proc/net/arp"
- Getting connected IPs and MACs.
if($4~/[1-9a-f]+/&&$6~/^wl/)
- Filtering out header line, zero MACs, and non-wireless interfaces.
if(!macnamz[$4])
- If connected device is not registered in our macs
file.
gsub(":","",macv)
and vendor=substr(macv,1,6)
- Getting first 6 symbols of MAC.
RS="\n|\r"
- Because oui.txt
may have MS Windows line endings.
gsub(/.*\t+/,"")
- Removing tabs
macnamz[mac]=$0
- Converting line to an array member.
update=1
- This variable says that updating of file is necessary (if it is).