Skip to content

Instantly share code, notes, and snippets.

@yujqiao
Created September 21, 2022 05:03
Show Gist options
  • Save yujqiao/e5861030352611a922ba9a4d70ff32f8 to your computer and use it in GitHub Desktop.
Save yujqiao/e5861030352611a922ba9a4d70ff32f8 to your computer and use it in GitHub Desktop.

Revisions

  1. yujqiao renamed this gist Sep 21, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. yujqiao created this gist Sep 21, 2022.
    35 changes: 35 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/sh
    mac_blacklist="01:00:8c:fa:1a:ae:d1"

    leases=$(cat /tmp/dhcp.leases | sed 's/*/unknown/')

    IFS='
    '

    online_cnt=0
    # grep NUD status, see also https://www.ccexpert.us/routing-tcp-ip-2/neighbor-unreachability-detection.html
    ip nei | grep -E "(REACHABLE)|(DELAY)" > /tmp/arp_table
    for line in $leases
    do
    ip=$(echo $line | awk '{print $3}')
    name=$(echo $line | awk '{print $4}')
    mac=$(echo $line | awk '{print $5}')
    if ( grep "$ip" /tmp/arp_table > /dev/null)
    then
    valid=1
    for black in $mac_blacklist
    do
    if [[ $black = $mac ]]
    then
    valid=0
    fi
    done
    if [[ $valid -eq 1 ]]
    then
    echo $ip "$name" "$mac"
    online_cnt=$((online_cnt + 1))
    fi
    fi
    done

    return $online_cnt