Advertisement
MartineauPASTEBIN

ARPDevices.sh

Sep 6th, 2018
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.36 KB | None | 0 0
  1. #!/bin/sh
  2. VER="v01.02"
  3. #======================================================================================================= © 2016-2018 Martineau, v1.02
  4. #
  5. # Check if ARP cache is valid for expected devices.
  6. #
  7. #     ARPDevices
  8. #                  List ARP cache
  9. #                  e.g.
  10. #                       Warning may take up to 60secs to resolve ARP
  11. #                       10.88.8.12  c4:xx:xx:xx:xx:xx   TL-SG2008-Bed1  (TL-SG2008-Bed1.Martineau.lan)
  12. #                       10.88.8.100 <incomplete>        N/A             (EeeBox.Martineau.lan)
  13. #                       <snip>
  14. #
  15. #                       WAN.ISP.xxx.xxx    28:xx:xx:xx:xx:xx   N/A     (WAN_ISP_provider)
  16. #
  17. #                       Records CNT=19 TAGGED=18
  18. #
  19. #                       .......took 3 seconds
  20.  
  21. #
  22. #     ARPDevices   refresh
  23. #                  If '/jffs/scripts/Reset_NVRAM-DHCPstatic.sh' exists; contents will be used as a ping target(s)
  24. #                  otherwise
  25. #                  NVRAM variable dhcp_staticlist will be used as a ping target(s)
  26. #
  27.  
  28. #
  29. Say(){
  30.    echo -e $$ $@ | logger -st "($(basename $0))"
  31. }
  32. SayT(){
  33.    echo -e $$ $@ | logger -t "($(basename $0))"
  34. }
  35.  
  36. # Print between line beginning with'#==' to first blank line inclusive
  37. ShowHelp() {
  38.     /usr/bin/awk '/^#==/{f=1} f{print; if (!NF) exit}' $0
  39. }
  40. # Function Parse(String delimiter(s) variable_names)
  41. Parse() {
  42.     #
  43.     #   Parse       "Word1,Word2|Word3" ",|" VAR1 VAR2 REST
  44.     #               (Effectivley executes VAR1="Word1";VAR2="Word2";REST="Word3")
  45.  
  46.     local string IFS
  47.  
  48.     TEXT="$1"
  49.     IFS="$2"
  50.     shift 2
  51.     read -r -- "$@" <<EOF
  52. $TEXT
  53. EOF
  54. }
  55.  
  56. ANSIColours () {
  57.  
  58.     cRESET="\e[0m";cBLA="\e[30m";cRED="\e[31m";cGRE="\e[32m";cYEL="\e[33m";cBLU="\e[34m";cMAG="\e[35m";cCYA="\e[36m";cGRA="\e[37m"
  59.     cBGRA="\e[90m";cBRED="\e[91m";cBGRE="\e[92m";cBYEL="\e[93m";cBBLU="\e[94m";cBMAG="\e[95m";cBCYA="\e[96m";cBWHT="\e[97m"
  60.     aBOLD="\e[1m";aDIM="\e[2m";aUNDER="\e[4m";aBLINK="\e[5m";aREVERSE="\e[7m"
  61.     cRED_="\e[41m";cGRE_="\e[42m"
  62.  
  63. }
  64.  
  65. ANSIColours
  66.  
  67. # Provide assistance
  68. if [ "$1" = "-h" ] || [ "$1" = "help" ]; then
  69.    ShowHelp                                                     # Show help
  70.    exit 0
  71. fi
  72.  
  73. FIRMWARE=$(echo $(nvram get buildno) | awk 'BEGIN { FS = "." } {printf("%03d%02d",$1,$2)}')
  74.  
  75. HACK=0                                                      # Tacky!.... variables not available outside of do loop????
  76.  
  77. LANIPADDR=$(nvram get lan_ipaddr)
  78. LAN_SUBNET=${LANIPADDR%.*}
  79. OCTET1=${LANIPADDR%%.*}                                     # v1.02
  80.  
  81. NVRAM_FN="Reset_NVRAM-DHCPstatic.sh"                        # Basically use custom list to check all devices..
  82.  
  83. # Perform a PING refresh of the ARP cache
  84. if [ "$1" == "refresh" ];then
  85.     if [ ! -f "$NVRAM_FN" ];then
  86.         # Assumes I haven't kept 'Reset_NVRAM-DHCPstatic.sh' up to date! ;-)
  87.         NVRAM_FN="/tmp/NVRAM_dhcp"          # Use the NVRAM variable to check all DHCP reserved devices..
  88.         # Ensure the $FN contains records
  89.         nvram get dhcp_staticlist | sed 's/</\n</g' > $NVRAM_FN
  90.  
  91.     fi
  92.         CNT=$(cat "$NVRAM_FN" | grep -v "^#" | grep -Fc "<" )
  93.         echo -e $cBCYA"\n\t"$VER "PING ARP refresh: may take up to" $CNT "secs if ALL are not ONLINE! (1 second per IP) using '$NVRAM_FN'\n"
  94.  
  95.         CNT=
  96.         start=`date +%s`
  97.  
  98.         for IP in $(cat "$NVRAM_FN" | grep -v "^#" | grep -F "<" | awk ' BEGIN {FS=">" } {print $2}')
  99.             do
  100.                 COLOR=$cBGRE
  101.                 if [ ! -z "$(echo $IP | grep LAN_SUBNET)" ];then
  102.                     IP=${LAN_SUBNET}.${IP##*\.}
  103.                 fi
  104.                 ping -q -c1 -w1 $IP 2>&1 >/dev/null
  105.                 if [ $? -eq 1 ];then
  106.                     COLOR=$cBRED
  107.                 fi
  108.                 echo -en ${COLOR}$IP"\t"
  109.             done
  110.  
  111.         end=`date +%s`
  112.         difftime=$((end-start))
  113.         start=`date +%s`
  114.         echo -e $cBYEL"\n\n\t.......took $(($difftime % 60)) seconds\n"$cRESET
  115.  
  116.         echo -e $cRESET
  117. fi
  118.  
  119. if [ -z "$1" ];then
  120.     echo -e $cBYEL"\n\t"$VER "ARP cache report - Warning may take up to 60secs to resolve ARP\n\n"
  121. else
  122.     echo -e $cBCYA"\n\t"$VER "ARP cache report"
  123. fi
  124.  
  125. echo -e $cBMAG
  126.  
  127. start=`date +%s`
  128.  
  129. #
  130. #       arp -a
  131. #           LIFX-Reading.Martineau.lan (10.88.8.31) at d0:xx:xx:xx:xx:xx [ether]  on br0
  132. #
  133. #       ip neigh
  134. #           10.88.8.31 dev br0 lladdr d0:xx:xx:xx:xx:xx REACHABLE
  135. #
  136. #       cat /proc/net/arp
  137. #           10.88.8.31       0x1         0x2         d0:xx:xx:xx:xx:xx     *        br0
  138.  
  139. # Check if counts match
  140. if [ $(wc -l  </etc/hosts.dnsmasq) -ne $(grep -cE "^dhcp-host" /etc/dnsmasq.conf) ];then
  141.     echo -e $cRED"\n\t\a**Warning: Number of '/etc/hosts.dnsmasq' entries does not match number of 'dhcp-host' entries in '/etc/dnsmasq.conf'"$cRESET
  142. fi
  143.  
  144.  
  145. arp -a | awk '{print $2","$4","$1}' | tr -d '()' | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 | while read ARP_DEVICE
  146. do
  147.     if [ -z $CNT ];then
  148.         CNT=0;TAGGED=0
  149.     fi
  150.  
  151.     # Match MAC with what we have in /etc/ethers -> /etc/hosts.dnsmasq to get a valid description
  152.     # Device must be defined in DHCP table :-(
  153.     Parse $ARP_DEVICE "," ARP_IP ARP_MAC ARP_DESC
  154.     DESC="N/A\t"
  155.     if [ "$ARP_MAC" != "<incomplete>" ];then
  156.         if [ "$ARP_DESC" != "?" ];then
  157.             TAGGED=$((TAGGED+1))
  158.         fi
  159.         FN="/etc/ethers"
  160.         if [ $FIRMWARE -ge 38201 ];then
  161.             # /etc/ethers no longer exists/used
  162.             # Instead /etc/dnsmasq.conf contains
  163.             #         dhcp-host=00:xx:xx:xx:xx:xx,10.88.8.254
  164.             FN="/etc/dnsmasq.conf"
  165.             if [ "$(grep -i "$ARP_MAC" "$FN" | awk ' FS="," {print $2}' | wc -l)" -gt 1 ];then
  166.                 echo -e $cRED"\a*Duplicate*\t"$ARP_MAC"\tFOUND in '"$FN"' ???; Following description may be INVALID"$cBMAG
  167.             fi
  168.             if [ ! -z "$(grep -i "$ARP_MAC" "$FN" | awk ' FS="," {print $2}')" ];then
  169.                 DESC=$(grep -i "$(grep -i "$ARP_MAC" "$FN" | awk ' FS=","{print $2}')\b" /etc/hosts.dnsmasq | awk '{print $2}')
  170.                 if [ -z "$DESC" ];then
  171.                     DESC="N/A\t"
  172.                 else
  173.                     if [ "${#DESC}" -lt 8 ];then
  174.                         DESC=$DESC"\t"                  # Cosmetic tabular formatting!
  175.                     fi
  176.                 fi
  177.             fi
  178.         else
  179.             if [ ! -z $(grep -i "$ARP_MAC" "$FN" | awk '{print $2}') ];then
  180.                 DESC=$(grep -i "$(grep -i "$ARP_MAC" "$FN" | awk '{print $2}')\b" /etc/hosts.dnsmasq | awk '{print $2}')
  181.                 if [ -z "$DESC" ];then
  182.                     DESC="N/A\t"
  183.                 else
  184.                     if [ "${#DESC}" -lt 8 ];then
  185.                         DESC=$DESC"\t"                  # Cosmetic tabular formatting!
  186.                     fi
  187.                 fi
  188.             fi
  189.         fi
  190.     else
  191.         DESC="\tN/A\t"
  192.     fi
  193.  
  194.     CNT=$((CNT+1))
  195.  
  196.     if [ -z "$(echo "$ARP_IP" | grep "^$OCTET1")" ];then
  197.         echo -e $cBYEL
  198.         HACK=1                                                      # Tacky!.... variables not available outside of do loop????
  199.     fi
  200.     echo -e $ARP_IP"\t"$ARP_MAC"\t"$DESC"\t("$ARP_DESC")"
  201.     if [ $HACK -eq 1 ];then
  202.         echo -e "\nRecords CNT="$CNT "TAGGED="$TAGGED               # Tacky!.... variables not available outside of do loop????
  203.     fi
  204.     DESC=
  205. done
  206.  
  207. end=`date +%s`
  208. difftime=$((end-start))
  209.  
  210. echo -e $cBYEL"\n\t.......took $(($difftime % 60)) seconds\n"$cRESET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement