Commit 8e104c564bc43f9efc3d0a52ea6e07690048f121
1 parent
2762816b
Exists in
master
增加开机启动功能
Showing
1 changed file
with
49 additions
and
27 deletions
Show diff stats
ngrok_install.sh
1 | 1 | #!/bin/bash |
2 | +#=============================================================================================== | |
3 | +# System Required: CentOS Debian or Ubuntu (32bit/64bit) | |
4 | +# Description: Install Ngrok for CentOS Debian or Ubuntu | |
5 | +# Author: Clang <admin@clangcn.com> | |
6 | +# Intro: http://clang.cn | |
7 | +#=============================================================================================== | |
2 | 8 | PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
3 | 9 | export PATH |
4 | 10 | shell_run_start=`date "+%Y-%m-%d %H:%M:%S"` #shell run start time |
5 | -version="V4.0" | |
11 | +version="V5.0" | |
12 | +str_ngrok_dir="/usr/local/ngrok" | |
6 | 13 | # Check if user is root |
7 | 14 | function rootness(){ |
8 | 15 | if [[ $EUID -ne 0 ]]; then |
... | ... | @@ -10,9 +17,7 @@ function rootness(){ |
10 | 17 | exit 1 |
11 | 18 | fi |
12 | 19 | } |
13 | - | |
14 | -get_char() | |
15 | -{ | |
20 | +function get_char(){ | |
16 | 21 | SAVEDSTTY=`stty -g` |
17 | 22 | stty -echo |
18 | 23 | stty cbreak |
... | ... | @@ -21,11 +26,10 @@ get_char() |
21 | 26 | stty echo |
22 | 27 | stty $SAVEDSTTY |
23 | 28 | } |
24 | - | |
25 | 29 | function fun_clangcn.com(){ |
26 | 30 | echo "" |
27 | 31 | echo "#######################################################################" |
28 | -echo "# On key install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server" | |
32 | +echo "# install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server" | |
29 | 33 | echo "# Intro: http://clang.cn/blog/" |
30 | 34 | echo "#" |
31 | 35 | echo "# Author: Clang <admin@clangcn.com>" |
... | ... | @@ -33,7 +37,6 @@ echo "# version:${version}" |
33 | 37 | echo "#######################################################################" |
34 | 38 | echo "" |
35 | 39 | } |
36 | - | |
37 | 40 | # Check OS |
38 | 41 | function checkos(){ |
39 | 42 | if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then |
... | ... | @@ -47,7 +50,6 @@ function checkos(){ |
47 | 50 | exit 1 |
48 | 51 | fi |
49 | 52 | } |
50 | - | |
51 | 53 | # Get version |
52 | 54 | function getversion(){ |
53 | 55 | if [[ -s /etc/redhat-release ]];then |
... | ... | @@ -56,7 +58,6 @@ function getversion(){ |
56 | 58 | grep -oE "[0-9.]+" /etc/issue |
57 | 59 | fi |
58 | 60 | } |
59 | - | |
60 | 61 | # CentOS version |
61 | 62 | function centosversion(){ |
62 | 63 | local code=$1 |
... | ... | @@ -68,7 +69,6 @@ function centosversion(){ |
68 | 69 | return 1 |
69 | 70 | fi |
70 | 71 | } |
71 | - | |
72 | 72 | # Check OS bit |
73 | 73 | function check_os_bit(){ |
74 | 74 | if [[ `getconf WORD_BIT` = '32' && `getconf LONG_BIT` = '64' ]] ; then |
... | ... | @@ -77,14 +77,12 @@ function check_os_bit(){ |
77 | 77 | Is_64bit='n' |
78 | 78 | fi |
79 | 79 | } |
80 | - | |
81 | 80 | function check_centosversion(){ |
82 | 81 | if centosversion 5; then |
83 | 82 | echo "Not support CentOS 5.x, please change to CentOS 6,7 or Debian or Ubuntu and try again." |
84 | 83 | exit 1 |
85 | 84 | fi |
86 | 85 | } |
87 | - | |
88 | 86 | # Disable selinux |
89 | 87 | function disable_selinux(){ |
90 | 88 | if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then |
... | ... | @@ -92,22 +90,29 @@ function disable_selinux(){ |
92 | 90 | setenforce 0 |
93 | 91 | fi |
94 | 92 | } |
95 | - | |
96 | - | |
97 | - | |
98 | 93 | function fun_set_ngrok_domain(){ |
99 | 94 | # Set ngrok domain |
100 | 95 | NGROK_DOMAIN="" |
101 | 96 | read -p "Please input domain for Ngrok(e.g.:ngrok.clang.cn):" NGROK_DOMAIN |
102 | 97 | check_input |
103 | 98 | } |
104 | - | |
99 | +function fun_randstr(){ | |
100 | + index=0 | |
101 | + strRandomPass="" | |
102 | + for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
103 | + for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
104 | + for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
105 | + for i in {1..16}; do strRandomPass="$strRandomPass${arr[$RANDOM%$index]}"; done | |
106 | + echo $strRandomPass | |
107 | +} | |
105 | 108 | function fun_set_ngrok_pass(){ |
106 | 109 | # Set ngrok pass |
107 | - ngrok_pass="" | |
108 | - read -p "Please input password for Ngrok:" ngrok_pass | |
110 | + ngrokpass=`fun_randstr` | |
111 | + read -p "Please input password for Ngrok(Default Password: ${ngrokpass}):" ngrok_pass | |
112 | + if [ "${ngrok_pass}" = "" ]; then | |
113 | + ngrok_pass="${ngrokpass}" | |
114 | + fi | |
109 | 115 | } |
110 | - | |
111 | 116 | function check_input(){ |
112 | 117 | # check ngrok domain |
113 | 118 | if [ "$NGROK_DOMAIN" = "" ]; then |
... | ... | @@ -128,9 +133,8 @@ function check_input(){ |
128 | 133 | pre_install |
129 | 134 | fi |
130 | 135 | } |
131 | - | |
132 | 136 | function config_runshell_ngrok(){ |
133 | -cat > /root/.ngrok_config.sh <<EOF | |
137 | +cat > ${str_ngrok_dir}/.ngrok_config.sh <<EOF | |
134 | 138 | #!/bin/bash |
135 | 139 | # -------------config START------------- |
136 | 140 | dns="${NGROK_DOMAIN}" |
... | ... | @@ -140,12 +144,31 @@ https_port=443 |
140 | 144 | remote_port=4443 |
141 | 145 | srtCRT=server.crt |
142 | 146 | strKey=server.key |
147 | +loglevel="INFO" | |
143 | 148 | # -------------config END------------- |
144 | 149 | EOF |
145 | -wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrok.sh -O /root/ngrok.sh | |
146 | -chmod 500 /root/ngrok.sh /root/.ngrok_config.sh | |
147 | -} | |
148 | 150 | |
151 | +if [ ! -s /etc/init.d/ngrokd ]; then | |
152 | + if ! wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrokd.init -O /etc/init.d/ngrokd; then | |
153 | + echo "Failed to download ngrokd.init file!" | |
154 | + exit 1 | |
155 | + fi | |
156 | +fi | |
157 | +[ ! -x ${str_ngrok_dir}/.ngrok_config.sh ] && chmod 500 ${str_ngrok_dir}/.ngrok_config.sh | |
158 | +[ ! -x /etc/init.d/ngrokd ] && chmod 755 /etc/init.d/ngrokd | |
159 | +if [ "${OS}" == 'CentOS' ]; then | |
160 | + if [ -s /etc/init.d/ngrokd ]; then | |
161 | + chmod +x /etc/init.d/ngrokd | |
162 | + chkconfig --add ngrokd | |
163 | + fi | |
164 | +else | |
165 | + if [ -s /etc/init.d/ngrokd ]; then | |
166 | + chmod +x /etc/init.d/ngrokd | |
167 | + update-rc.d -f ngrokd defaults | |
168 | + fi | |
169 | +fi | |
170 | + | |
171 | +} | |
149 | 172 | function fun_install_ngrok(){ |
150 | 173 | checkos |
151 | 174 | check_centosversion |
... | ... | @@ -153,7 +176,6 @@ function fun_install_ngrok(){ |
153 | 176 | disable_selinux |
154 | 177 | fun_set_ngrok_domain |
155 | 178 | } |
156 | - | |
157 | 179 | function pre_install(){ |
158 | 180 | echo "install ngrok,please wait..." |
159 | 181 | if [ "${OS}" == 'CentOS' ]; then |
... | ... | @@ -221,13 +243,13 @@ function pre_install(){ |
221 | 243 | echo "" |
222 | 244 | echo "For more information please visit http://clang.cn/" |
223 | 245 | echo "" |
224 | - echo -e "ngrok status manage: \033[45;37m/root/ngrok.sh\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m {\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;31mstart\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;32mstop\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;33mrestart\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;34mconfig\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;35madduser\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;36minfo\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m}" | |
246 | + echo -e "ngrok status manage: \033[45;37m/etc/init.d/ngrokd\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m {\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;31mstart\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;32mstop\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;33mrestart\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;34mconfig\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;35madduser\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m|\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[40;36minfo\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"33[0m}" | |
225 | 247 | echo -e "Your Domain: \033[32m\033[01m${NGROK_DOMAIN}\033[0m" |
226 | 248 | echo -e "Ngrok password: \033[32m\033[01m${ngrok_pass}\033[0m" |
227 | 249 | echo -e "http_port: \033[32m\033[01m80\033[0m" |
228 | 250 | echo -e "https_port: \033[32m\033[01m443\033[0m" |
229 | 251 | echo -e "remote_port: \033[32m\033[01m4443\033[0m" |
230 | - echo -e "Config file: \033[32m\033[01m/root/.ngrok_config.sh/.ngrok_config.sh\033[0m"33[0m" | |
252 | + echo -e "Config file: \033[32m\033[01m${str_ngrok_dir}/.ngrok_config.sh/.ngrok_config.sh\033[0m"33[0m" | |
231 | 253 | echo "" |
232 | 254 | echo "=========================================================================" |
233 | 255 | else | ... | ... |