ssh-vpn-script.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. function add-user() {
  3. clear
  4. echo -e "Add SSH & VPN User"
  5. echo -e "------------------"
  6. read -p "Username : " user
  7. if getent passwd $user > /dev/null 2>&1; then
  8. echo ""
  9. echo "User '$user' already exist."
  10. echo ""
  11. exit 0
  12. fi
  13. read -p "Password : " pass
  14. read -p "Duration (day) : " duration
  15. useradd -e $(date -d +${duration}days +%Y-%m-%d) -s /bin/false -M $user
  16. echo -e "$pass\n$pass\n"|passwd $user &> /dev/null
  17. exp=$(date -d +${duration}days +"%d %b %Y")
  18. clear
  19. echo -e "SSH & VPN User Information"
  20. echo -e "--------------------------"
  21. echo -e "Username : $user "
  22. echo -e "Password : $pass"
  23. echo -e "Expired date : $exp"
  24. echo -e ""
  25. }
  26. function delete-user() {
  27. clear
  28. echo -e "Delete SSH & VPN User"
  29. echo -e "---------------------"
  30. read -p "Username : " user
  31. echo -e ""
  32. if getent passwd $user > /dev/null 2>&1; then
  33. userdel $user
  34. echo -e "User '$user' deleted successfully."
  35. echo -e ""
  36. else
  37. echo -e "User '$user' does not exist."
  38. echo -e ""
  39. fi
  40. }
  41. function extend-user() {
  42. clear
  43. echo -e "Extend SSH & VPN User"
  44. echo -e "---------------------"
  45. read -p "Username : " user
  46. if ! getent passwd $user > /dev/null 2>&1; then
  47. echo -e "User '$user' does not exist."
  48. echo -e ""
  49. exit 0
  50. fi
  51. read -p "Duration (day) : " extend
  52. exp_old=$(chage -l $user | grep "Account expires" | awk -F": " '{print $2}')
  53. diff=$((($(date -d "${exp_old}" +%s)-$(date +%s))/(86400)))
  54. duration=$(expr $diff + $extend + 1)
  55. chage -E $(date -d +${duration}days +%Y-%m-%d) $user
  56. exp_new=$(chage -l $user | grep "Account expires" | awk -F": " '{print $2}')
  57. exp=$(date -d "${exp_new}" +"%d %b %Y")
  58. clear
  59. echo -e "SSH & VPN User Information"
  60. echo -e "--------------------------"
  61. echo -e "Username : $user "
  62. echo -e "Expired date : $exp"
  63. echo -e ""
  64. }
  65. function user-list() {
  66. clear
  67. echo -e ""
  68. echo -e "==============================="
  69. echo -e "Username Exp. Date"
  70. echo -e "-------------------------------"
  71. while read expired; do
  72. account=$(echo $expired | cut -d: -f1)
  73. id=$(echo $expired | grep -v nobody | cut -d: -f3)
  74. exp=$(chage -l $account | grep "Account expires" | awk -F": " '{print $2}')
  75. if [[ $id -ge 1000 ]]; then
  76. exp_date=$(date -d "${exp}" +"%d %b %Y")
  77. printf "%-17s %2s\n" "$account" "$exp_date"
  78. fi
  79. done < /etc/passwd
  80. total=$(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd | wc -l)
  81. echo -e "-------------------------------"
  82. echo -e "Total accounts : $total"
  83. echo -e "==============================="
  84. echo -e ""
  85. }
  86. function user-monitor() {
  87. data=($(ps aux | grep -i dropbear | awk '{print $2}'))
  88. clear
  89. echo -e ""
  90. echo -e "==============================="
  91. echo -e " Dropbear Login Monitor"
  92. echo -e "-------------------------------"
  93. for pid in "${data[@]}"
  94. do
  95. num=$(cat /var/log/auth.log | grep -i dropbear | grep -i "Password auth succeeded" | grep "dropbear\[$pid\]" | wc -l)
  96. user=$(cat /var/log/auth.log | grep -i dropbear | grep -i "Password auth succeeded" | grep "dropbear\[$pid\]" | awk '{print $10}' | tr -d "'")
  97. ip=$(cat /var/log/auth.log | grep -i dropbear | grep -i "Password auth succeeded" | grep "dropbear\[$pid\]" | awk '{print $12}')
  98. if [ $num -eq 1 ]; then
  99. echo -e "$pid - $user - $ip"
  100. fi
  101. done
  102. echo -e "==============================="
  103. echo -e ""
  104. echo -e "==============================="
  105. echo -e " OpenVPN (TCP) Login Monitor"
  106. echo -e "-------------------------------"
  107. a=$(grep -n "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since" /var/log/openvpn/server-tcp-status.log | awk -F":" '{print $1}')
  108. b=$(expr $(grep -n "ROUTING TABLE" /var/log/openvpn/server-tcp-status.log | awk -F":" '{print $1}') - 1)
  109. c=$(expr ${b} - ${a})
  110. cat /var/log/openvpn/server-tcp-status.log | head -n $b | tail -n $c | sed -e 's/,/\t/g' > /tmp/openvpn-tcp-login.txt
  111. while read login; do
  112. user=$(echo $login | awk '{print $1}')
  113. ip=$(echo $login | awk '{print $2}')
  114. echo -e "$user - $ip"
  115. done < /tmp/openvpn-tcp-login.txt
  116. echo -e "==============================="
  117. echo -e ""
  118. echo -e "==============================="
  119. echo -e " OpenVPN (UDP) Login Monitor"
  120. echo -e "-------------------------------"
  121. a=$(grep -n "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since" /var/log/openvpn/server-udp-status.log | awk -F":" '{print $1}')
  122. b=$(expr $(grep -n "ROUTING TABLE" /var/log/openvpn/server-udp-status.log | awk -F":" '{print $1}') - 1)
  123. c=$(expr ${b} - ${a})
  124. cat /var/log/openvpn/server-udp-status.log | head -n $b | tail -n $c | sed -e 's/,/\t/g' > /tmp/openvpn-udp-login.txt
  125. while read login; do
  126. user=$(echo $login | awk '{print $1}')
  127. ip=$(echo $login | awk '{print $2}')
  128. echo -e "$user - $ip"
  129. done < /tmp/openvpn-udp-login.txt
  130. echo -e "==============================="
  131. echo -e ""
  132. }
  133. function ovpn-config() {
  134. clear
  135. echo -e "OpenVPN Config"
  136. echo -e "--------------"
  137. echo -e " [1] Config TCP"
  138. echo -e " [2] Config UDP"
  139. echo -e " [3] Exit"
  140. echo -e ""
  141. until [[ ${option} =~ ^[1-3]$ ]]; do
  142. read -rp "Select an option [1-3]: " option
  143. done
  144. case "${option}" in
  145. 1)
  146. clear
  147. echo -e "OpenVPN Config - TCP"
  148. echo -e "--------------------"
  149. echo -e ""
  150. cat /iriszz/openvpn/client-tcp.ovpn
  151. echo -e ""
  152. exit
  153. ;;
  154. 2)
  155. clear
  156. echo -e "OpenVPN Config - UDP"
  157. echo -e "--------------------"
  158. echo -e ""
  159. cat /iriszz/openvpn/client-udp.ovpn
  160. echo -e ""
  161. exit
  162. ;;
  163. 3)
  164. clear
  165. exit 0
  166. ;;
  167. esac
  168. }
  169. clear
  170. echo -e "====[ SSH & VPN Menu ]===="
  171. echo -e ""
  172. echo -e " [1] Add user"
  173. echo -e " [2] Delete user"
  174. echo -e " [3] Extend user"
  175. echo -e " [4] User list"
  176. echo -e " [5] User monitor"
  177. echo -e " [6] OVPN config"
  178. echo -e " [7] Exit"
  179. echo -e ""
  180. until [[ ${option} =~ ^[1-7]$ ]]; do
  181. read -rp "Select an option [1-7]: " option
  182. done
  183. case "${option}" in
  184. 1)
  185. add-user
  186. exit
  187. ;;
  188. 2)
  189. delete-user
  190. exit
  191. ;;
  192. 3)
  193. extend-user
  194. exit
  195. ;;
  196. 4)
  197. user-list
  198. exit
  199. ;;
  200. 5)
  201. user-monitor
  202. exit
  203. ;;
  204. 6)
  205. ovpn-config
  206. exit
  207. ;;
  208. 7)
  209. clear
  210. exit 0
  211. ;;
  212. esac