Monday, December 08, 2008

skrip deteksi duplikasi mac

Skrip shell openbsd untuk mencari mac address duplicate di router mikrotik, file penunjangnya adalah sshwrap. Ada beberapa bagian yang bikin pusing, yaitu komparasi array dengan if, yang hasilnya gak cocok sama sekali, sehingga harus diakal menggunakan variable biasa.

  1. ==================potongdisini=====================================
  2. # cat /sbin/macduplicate.sh
  3. #!/bin/sh
  4. #x=`telnetmikrotik.exp`
  5. /sbin/sshwrap.exp 10.20.30.1 22000 diag diag "ip arp print without-paging terse file=maclist"
  6. wget -O /tmp/maclist.txt ftp://diag:diag@10.20.30.1/maclist.txt
  7. a=`cat /tmp/maclist.txt | cut -f 3 -d "=" | egrep "([0-9A-F]{2}:){5}[0-9A-F]{2}" | cut -f 1 -d " " | sort`
  8. i=`cat /tmp/maclist.txt | cut -f 3 -d "=" | egrep "([0-9A-F]{2}:){5}[0-9A-F]{2}" | cut -f 1 -d " " | sort | wc -l`
  9. b=`cat /tmp/maclist.txt | cut -f 3 -d "=" | egrep "([0-9A-F]{2}:){5}[0-9A-F]{2}" | cut -f 1 -d " " | sort | uniq -d`
  10. j=`cat /tmp/maclist.txt | cut -f 3 -d "=" | egrep "([0-9A-F]{2}:){5}[0-9A-F]{2}" | cut -f 1 -d " " | sort | uniq -d | wc -l`
  11. rm -rf /tmp/maclist.txt
  12. x=0
  13. y=0
  14. z=0
  15. alarm=4
  16. while [ $z -lt $i ]; do
  17. let "z=$z+1"
  18. maclist[z]=`echo $a |cut -f $z -d " "`
  19. # echo "$z- ${maclist[z]}"
  20. done
  21. z=0
  22. while [ $z -lt $j ]; do
  23. let "z=$z+1"
  24. macduplist[$z]=`echo $b |cut -f $z -d " "`
  25. # echo "$z- ${macduplist[$z]}"
  26. done
  27. z=0
  28. y=0
  29. while [ $y -lt $i ]; do
  30. let "y=$y+1"
  31. while [ $z -lt $j ]; do
  32. let "z=$z+1"
  33. f=${maclist[$y]}
  34. g=${macduplist[$z]}
  35. if [ $f == $g ]; then
  36. # if [ {$maclist[$y]} == {$macduplist[$z]} ]; then
  37. let "dupcount[$z]=${dupcount[$z]}+1"
  38. # echo "$y dup $f $g"
  39. else
  40. # echo "$y ${maclist[$y]} ${macduplist[$z]}"
  41. echo
  42. fi
  43. done
  44. z=0
  45. done
  46. z=0
  47. y=0
  48. while [ $z -lt $j ]; do
  49. let "z=$z+1"
  50. if [ ${dupcount[z]} -ge $alarm ]; then
  51. # echo "awas! ${macduplist[z]}"
  52. /sbin/sshwrap.exp 10.20.30.1 22000 diag diag "system script run sirine"
  53. /sbin/sshwrap.exp 10.20.30.1 22000 diag diag ":log info ${dupcount[z]}-mac-address-${macduplist[z]}-more-than-$alarm-entries"
  54. fi
  55. # echo "${macduplist[z]}-${dupcount[z]}"
  56. done
  57. #
  58. ==================potongdisini=====================================
  59. # cat /sbin/sshwrap.exp
  60. #!/usr/local/bin/expect -f
  61. #spawn "ssh $argv 1 -p $argv 2 -l $argv 3 $argv 5"
  62. #expect assword:
  63. #send $argv 4\r
  64. set addr [lindex $argv 0]
  65. set port [lindex $argv 1]
  66. set user [lindex $argv 2]
  67. set password [lindex $argv 3]
  68. set command [lindex $argv 4]
  69. spawn ssh $addr -p $port -l $user
  70. expect assword:
  71. send $password\r
  72. expect >
  73. send $command\r
  74. expect >
  75. send quit\r
  76. #
  77. ==================potongdisini=====================================
  78. [yusuf81@linkmedia] > system script print terse from=16
  79. 0 name=sirine source=:for t from=1000 to=2000 step=20 do={:beep frequency=$t length=10ms; :delay 10ms}\r\n:for t from=2000
  80. to=1000 step=-20 do={:beep frequency=$t length=10ms; :delay 10ms}
  81. owner=yusuf81 policy=ftp,read,winbox last-started=dec/08/2008 20:25:16 run-count=48
  82. [yusuf81@linkmedia] >

No comments: