kms.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env bash
  2. #
  3. # Auto install KMS Server
  4. # System Required: CentOS 6+, Debian7+, Ubuntu12+
  5. # Copyright (C) 2017-2018 Teddysun <[email protected]>
  6. # URL: https://teddysun.com/530.html
  7. #
  8. # Thanks: https://github.com/Wind4/vlmcsd
  9. #
  10. red='\033[0;31m'
  11. green='\033[0;32m'
  12. yellow='\033[0;33m'
  13. plain='\033[0m'
  14. cur_dir=$(pwd)
  15. [[ $EUID -ne 0 ]] && echo -e "${red}Error:${plain} This script must be run as root!" && exit 1
  16. if [ -f /etc/redhat-release ]; then
  17. release="centos"
  18. elif grep -Eqi "debian" /etc/issue; then
  19. release="debian"
  20. elif grep -Eqi "ubuntu" /etc/issue; then
  21. release="ubuntu"
  22. elif grep -Eqi "centos|red hat|redhat" /etc/issue; then
  23. release="centos"
  24. elif grep -Eqi "debian" /proc/version; then
  25. release="debian"
  26. elif grep -Eqi "ubuntu" /proc/version; then
  27. release="ubuntu"
  28. elif grep -Eqi "centos|red hat|redhat" /proc/version; then
  29. release="centos"
  30. else
  31. release=""
  32. fi
  33. boot_start(){
  34. if [[ x"${release}" == x"debian" || x"${release}" == x"ubuntu" ]]; then
  35. update-rc.d -f "${1}" defaults
  36. elif [[ x"${release}" == x"centos" ]]; then
  37. chkconfig --add "${1}"
  38. chkconfig "${1}" on
  39. fi
  40. }
  41. boot_stop(){
  42. if [[ x"${release}" == x"debian" || x"${release}" == x"ubuntu" ]]; then
  43. update-rc.d -f "${1}" remove
  44. elif [[ x"${release}" == x"centos" ]]; then
  45. chkconfig "${1}" off
  46. chkconfig --del "${1}"
  47. fi
  48. }
  49. # Get version
  50. getversion(){
  51. if [[ -s /etc/redhat-release ]]; then
  52. grep -oE "[0-9.]+" /etc/redhat-release
  53. else
  54. grep -oE "[0-9.]+" /etc/issue
  55. fi
  56. }
  57. # CentOS version
  58. centosversion(){
  59. if [[ x"${release}" == x"centos" ]]; then
  60. local code=$1
  61. local version="$(getversion)"
  62. local main_ver=${version%%.*}
  63. if [ "$main_ver" == "$code" ]; then
  64. return 0
  65. else
  66. return 1
  67. fi
  68. else
  69. return 1
  70. fi
  71. }
  72. get_opsy() {
  73. [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
  74. [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
  75. [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
  76. }
  77. get_char() {
  78. SAVEDSTTY=$(stty -g)
  79. stty -echo
  80. stty cbreak
  81. dd if=/dev/tty bs=1 count=1 2> /dev/null
  82. stty -raw
  83. stty echo
  84. stty "$SAVEDSTTY"
  85. }
  86. set_firewall() {
  87. if centosversion 6; then
  88. /etc/init.d/iptables status > /dev/null 2>&1
  89. if [ $? -eq 0 ]; then
  90. iptables -L -n | grep -i 1688 > /dev/null 2>&1
  91. if [ $? -ne 0 ]; then
  92. iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 1688 -j ACCEPT
  93. /etc/init.d/iptables save
  94. /etc/init.d/iptables restart
  95. fi
  96. else
  97. echo -e "${yellow}Warning:${plain} iptables looks like shutdown or not installed, please enable port 1688 manually set if necessary."
  98. fi
  99. elif centosversion 7; then
  100. systemctl status firewalld > /dev/null 2>&1
  101. if [ $? -eq 0 ]; then
  102. firewall-cmd --permanent --zone=public --add-port=1688/tcp
  103. firewall-cmd --reload
  104. else
  105. echo -e "${yellow}Warning:${plain} firewalld looks like shutdown or not installed, please enable port 1688 manually set if necessary."
  106. fi
  107. fi
  108. }
  109. install_main() {
  110. [ -f /usr/bin/vlmcsd ] && echo -e "${yellow}Warning:${plain} KMS Server is already installed. nothing to do..." && exit 1
  111. clear
  112. opsy=$( get_opsy )
  113. arch=$( uname -m )
  114. lbit=$( getconf LONG_BIT )
  115. kern=$( uname -r )
  116. echo "---------- System Information ----------"
  117. echo " OS : $opsy"
  118. echo " Arch : $arch ($lbit Bit)"
  119. echo " Kernel : $kern"
  120. echo "----------------------------------------"
  121. echo " Auto install KMS Server"
  122. echo
  123. echo " URL: https://teddysun.com/530.html"
  124. echo "----------------------------------------"
  125. echo
  126. echo "Press any key to start...or Press Ctrl+C to cancel"
  127. char=$(get_char)
  128. if [[ x"${release}" == x"centos" ]]; then
  129. yum -y install gcc git make nss curl libcurl
  130. if ! wget --no-check-certificate -O /etc/init.d/kms https://raw.githubusercontent.com/teddysun/across/master/kms; then
  131. echo -e "[${red}Error:${plain}] Failed to download KMS Server script."
  132. exit 1
  133. fi
  134. elif [[ x"${release}" == x"debian" || x"${release}" == x"ubuntu" ]]; then
  135. apt-get -y update
  136. apt-get install -y gcc git make libnss3 curl libcurl3-nss
  137. if ! wget --no-check-certificate -O /etc/init.d/kms https://raw.githubusercontent.com/teddysun/across/master/kms-debian; then
  138. echo -e "[${red}Error:${plain}] Failed to download KMS Server script."
  139. exit 1
  140. fi
  141. else
  142. echo -e "${red}Error:${plain} OS is not be supported, please change to CentOS/Debian/Ubuntu and try again."
  143. exit 1
  144. fi
  145. cd "${cur_dir}" || exit
  146. git clone https://github.com/Wind4/vlmcsd.git > /dev/null 2>&1
  147. [ -d vlmcsd ] && cd vlmcsd || echo -e "[${red}Error:${plain}] Failed to git clone vlmcsd."
  148. make
  149. if [ $? -ne 0 ]; then
  150. echo -e "${red}Error:${plain} Install KMS Server failed, please check it and try again."
  151. exit 1
  152. fi
  153. cp -p bin/vlmcsd /usr/bin/
  154. chmod 755 /usr/bin/vlmcsd
  155. chmod 755 /etc/init.d/kms
  156. boot_start kms
  157. /etc/init.d/kms start
  158. if [ $? -ne 0 ]; then
  159. echo -e "${red}Error:${plain} KMS server start failed."
  160. fi
  161. if [[ x"${release}" == x"centos" ]]; then
  162. set_firewall
  163. fi
  164. cd "${cur_dir}" || exit
  165. rm -rf vlmcsd
  166. echo
  167. echo "Install KMS Server success"
  168. echo "Welcome to visit:https://teddysun.com/530.html"
  169. echo "Enjoy it!"
  170. echo
  171. }
  172. install_kms() {
  173. install_main 2>&1 | tee "${cur_dir}"/install_kms.log
  174. }
  175. # Uninstall KMS Server
  176. uninstall_kms() {
  177. printf "Are you sure uninstall KMS Server? (y/n) "
  178. printf "\n"
  179. read -p "(Default: n):" answer
  180. [ -z "${answer}" ] && answer="n"
  181. if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
  182. /etc/init.d/kms status > /dev/null 2>&1
  183. if [ $? -eq 0 ]; then
  184. /etc/init.d/kms stop
  185. fi
  186. boot_stop kms
  187. # delete kms server
  188. rm -f /usr/bin/vlmcsd
  189. rm -f /etc/init.d/kms
  190. rm -f /var/log/vlmcsd.log
  191. echo "KMS Server uninstall success"
  192. else
  193. echo
  194. echo "Uninstall cancelled, nothing to do..."
  195. echo
  196. fi
  197. }
  198. # Initialization step
  199. action=$1
  200. [ -z "$1" ] && action=install
  201. case "$action" in
  202. install|uninstall)
  203. ${action}_kms
  204. ;;
  205. *)
  206. echo "Arguments error! [${action}]"
  207. echo "Usage: $(basename $0) [install|uninstall]"
  208. ;;
  209. esac