l2tpctl.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/sh
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #
  5. # This is a Shell script for configure and start L2TP/IPSec VPN server with Docker image
  6. #
  7. # Copyright (C) 2018 - 2019 Teddysun <[email protected]>
  8. #
  9. # Reference URL:
  10. # https://github.com/libreswan/libreswan
  11. # https://github.com/xelerance/xl2tpd
  12. rand(){
  13. str=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
  14. echo ${str}
  15. }
  16. list_users(){
  17. if [ ! -f /etc/ppp/chap-secrets ];then
  18. echo "Error: /etc/ppp/chap-secrets file not found."
  19. exit 1
  20. fi
  21. local line="+-------------------------------------------+\n"
  22. local string=%20s
  23. printf "${line}|${string} |${string} |\n${line}" Username Password
  24. grep -v "^#" /etc/ppp/chap-secrets | awk '{printf "|'${string}' |'${string}' |\n", $1,$3}'
  25. printf ${line}
  26. }
  27. add_user(){
  28. while :
  29. do
  30. read -p "Please enter Username:" user
  31. if [ -z ${user} ]; then
  32. echo "Username can not be empty"
  33. else
  34. grep -w "${user}" /etc/ppp/chap-secrets > /dev/null 2>&1
  35. if [ $? -eq 0 ];then
  36. echo "Username (${user}) already exists. Please re-enter your username."
  37. else
  38. break
  39. fi
  40. fi
  41. done
  42. pass="$(rand)"
  43. echo "Please enter ${user}'s password:"
  44. read -p "(Default Password: ${pass}):" tmppass
  45. [ ! -z ${tmppass} ] && pass=${tmppass}
  46. pass_enc=$(openssl passwd -1 "${pass}")
  47. echo "${user} l2tpd ${pass} *" >> /etc/ppp/chap-secrets
  48. echo "${user}:${pass_enc}:xauth-psk" >> /etc/ipsec.d/passwd
  49. echo "Username (${user}) add completed."
  50. }
  51. del_user(){
  52. while :
  53. do
  54. read -p "Please enter Username you want to delete it:" user
  55. if [ -z ${user} ]; then
  56. echo "Username can not be empty"
  57. else
  58. grep -w "${user}" /etc/ppp/chap-secrets >/dev/null 2>&1
  59. if [ $? -eq 0 ];then
  60. break
  61. else
  62. echo "Username (${user}) is not exists. Please re-enter your username."
  63. fi
  64. fi
  65. done
  66. sed -i "/^\<${user}\>/d" /etc/ppp/chap-secrets
  67. sed -i "/^\<${user}\>/d" /etc/ipsec.d/passwd
  68. echo "Username (${user}) delete completed."
  69. }
  70. mod_user(){
  71. while :
  72. do
  73. read -p "Please enter Username you want to change password:" user
  74. if [ -z ${user} ]; then
  75. echo "Username can not be empty"
  76. else
  77. grep -w "${user}" /etc/ppp/chap-secrets >/dev/null 2>&1
  78. if [ $? -eq 0 ];then
  79. break
  80. else
  81. echo "Username (${user}) is not exists. Please re-enter your username."
  82. fi
  83. fi
  84. done
  85. pass="$(rand)"
  86. echo "Please enter ${user}'s new password:"
  87. read -p "(Default Password: ${pass}):" tmppass
  88. [ ! -z ${tmppass} ] && pass=${tmppass}
  89. pass_enc=$(openssl passwd -1 "${pass}")
  90. sed -i "/^\<${user}\>/d" /etc/ppp/chap-secrets
  91. sed -i "/^\<${user}\>/d" /etc/ipsec.d/passwd
  92. echo "${user} l2tpd ${pass} *" >> /etc/ppp/chap-secrets
  93. echo "${user}:${pass_enc}:xauth-psk" >> /etc/ipsec.d/passwd
  94. echo "Username ${user}'s password has been changed."
  95. }
  96. get_version(){
  97. ipsec --version
  98. xl2tpd --version
  99. }
  100. action=$1
  101. case ${action} in
  102. -l|--list)
  103. list_users
  104. ;;
  105. -a|--add)
  106. add_user
  107. ;;
  108. -d|--del)
  109. del_user
  110. ;;
  111. -m|--mod)
  112. mod_user
  113. ;;
  114. -v|--version)
  115. get_version
  116. ;;
  117. -h|--help)
  118. echo "Usage: `basename $0` -l,--list List all users"
  119. echo " `basename $0` -a,--add Add a user"
  120. echo " `basename $0` -d,--del Delete a user"
  121. echo " `basename $0` -m,--mod Modify a user password"
  122. echo " `basename $0` -v,--version Print program version"
  123. echo " `basename $0` -h,--help Print this help information"
  124. ;;
  125. *)
  126. echo "Usage: `basename $0` [-l,--list|-a,--add|-d,--del|-m,--mod|-v,--version|-h,--help]" && exit
  127. ;;
  128. esac