user-limit.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # Program untuk membatasi jumlah login user dropbear
  3. PARAM=$1
  4. echo "Semua user dropbear yang login lebih dari $1 akan di kill"
  5. echo -n > /tmp/pid2
  6. ps ax|grep dropbear > /tmp/pid
  7. cat /tmp/pid | grep -i 'dropbear -p' > /tmp/pids
  8. cat /var/log/secure | grep -i "Password auth succeeded" > /tmp/sks
  9. perl -pi -e 's/Password auth succeeded for//g' /tmp/sks
  10. perl -pi -e 's/dropbear//g' /tmp/sks
  11. cat /tmp/pid | while read line;do
  12. set -- $line
  13. p=$1
  14. var=`cat /tmp/sks | grep -i $1`
  15. set -- $var
  16. l=$6
  17. if [ "$6" != '' ]
  18. then
  19. echo "$p $l" | cat - /tmp/pid2 > /tmp/temp && mv /tmp/temp /tmp/pid2
  20. fi
  21. done
  22. case $PARAM in
  23. 1)
  24. echo -n > /tmp/user1
  25. cat /tmp/pid2 | while read line;do
  26. set -- $line
  27. p=$1
  28. u=$2
  29. cat /tmp/user1 | grep -i $u > /dev/null
  30. if [ $? = 1 ];then
  31. echo $line >> /tmp/user1
  32. else
  33. kill $p
  34. echo "kill $p user $u"
  35. fi
  36. done
  37. rm -f /tmp/pid
  38. rm -f /tmp/pid2
  39. rm -f /tmp/pids
  40. rm -f /tmp/sks
  41. rm -f /tmp/user1
  42. exit 0
  43. ;;
  44. 2)
  45. echo -n > /tmp/user1
  46. echo -n > /tmp/user2
  47. cat /tmp/pid2 | while read line;do
  48. set -- $line
  49. p=$1
  50. u=$2
  51. cat /tmp/user1 | grep -i $u > /dev/null
  52. if [ $? = 1 ];then
  53. echo $line >> /tmp/user1
  54. else
  55. cat /tmp/user2 | grep -i $u > /dev/null
  56. if [ $? = 1 ];then
  57. echo $line >> /tmp/user2
  58. else
  59. kill $p
  60. echo "kill $p user $u"
  61. fi
  62. fi
  63. done
  64. rm -f /tmp/pid
  65. rm -f /tmp/pid2
  66. rm -f /tmp/pids
  67. rm -f /tmp/sks
  68. rm -f /tmp/user1
  69. rm -f /tmp/user2
  70. exit 0
  71. ;;
  72. *)
  73. echo " gunakan perintah userlimit 1 untuk limit 1 login saja"
  74. echo " atau userlimit 2 untuk melimit max 2 login"
  75. rm -f /tmp/pid
  76. rm -f /tmp/pid2
  77. rm -f /tmp/pids
  78. rm -f /tmp/sks
  79. exit 1
  80. ;;
  81. esac