> CentOS > CentOS教程 >

支持centos 5.x的shadowsocks python版安装脚本

 最近用上了shadowsocks之后,就不再想用vpn来翻墙了,相信很多朋友都想偷懒,希望一个脚本就配置好.下面我就给大家个我从别人那拿来改了之后的脚本,原来这个脚本不支持centos 5.x,我修改成只适合centos 5.x用,下面来看看脚本吧.
  系统:centos 5.x 64位(我只在64位上测试了脚本,32位的我相信应该没有问题)
脚本内容:
cat /root/soft_shell/shadowsocks.sh
001 #! /bin/bash
002 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
003 export PATH
004 #===============================================================================================
005 #   System Required:  CentOS5.x (32bit/64bit)
006 #   Description:  Install Shadowsocks(Python) for CentOS
007 #   Author: rocdk890
008 #===============================================================================================
009 clear
010 echo "#############################################################"
011 echo "# Install Shadowsocks(Python) for CentOS 5.x (32bit/64bit)"
012 echo "# Intro: http://blog.slogra.com"
013 echo "#"
014 echo "# Author: rocdk890"
015 echo "#############################################################"
016 echo ""
017  
018 # Make sure only root can run our script
019 function rootness(){
020 if [[ $EUID -ne 0 ]]; then
021    echo "Error:This script must be run as root!" 1>&2
022    exit 1
023 fi
024 }
025  
026 # Check OS
027 function checkos(){
028 if [ -f /etc/redhat-release ];then
029    OS=CentOS
030    else
031        echo "Not support OS, Please reinstall OS and retry!"
032        exit 1
033 fi
034 }
035  
036 # Get version
037 function getversion(){
038     if [[ -s /etc/redhat-release ]];then
039         grep -oE  "[0-9.]+" /etc/redhat-release
040     else  
041         grep -oE  "[0-9.]+" /etc/issue
042     fi  
043 }
044  
045 # CentOS version
046 function centosversion(){
047     local code=$1
048     local version="`getversion`"
049     local main_ver=${version%%.*}
050     if [ $main_ver == $code ];then
051         return 0
052     else
053         return 1
054     fi      
055 }
056  
057 # Disable selinux
058 function disable_selinux(){
059 if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config;then
060     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
061     setenforce 0
062 fi
063 }
064  
065 #Current folder
066 cur_dir=`pwd`
067 cd $cur_dir
068  
069 #download epel
070 function download_epel(){
071 if [ -s epel-release-5-4.noarch.rpm ]; then
072     echo "epel-release-5-4.noarch.rpm [found]"
073   else
074     if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
075         wget -c --tries=3 http://mirrors.ustc.edu.cn/fedora/epel/5/x86_64/epel-release-5-4.noarch.rpm
076      else
077         wget -c --tries=3 http://mirrors.ustc.edu.cn/fedora/epel/5/i386/epel-release-5-4.noarch.rpm
078  exit 1
079  fi
080 fi
081 }
082  
083 # Pre-installation settings
084 function pre_install(){
085     # Not support CentOS 6
086     if centosversion 6; then
087         echo "Not support CentOS 6.x, please change to CentOS 5 and try again."
088         exit 1
089     # Not support CentOS 7  
090     elif centosversion 7; then
091         echo "Not support CentOS 7.x, please change to CentOS 5 try again."
092         exit 1
093     fi
094     #Set shadowsocks config password
095     echo "Please input password for shadowsocks:"
096     read -p "(Default password: blog.slogra.com):" shadowsockspwd
097     if [ "$shadowsockspwd" = "" ]; then
098         shadowsockspwd="blog.slogra.com"
099     fi
100     echo "password:$shadowsockspwd"
101     echo "####################################"
102     get_char(){
103         SAVEDSTTY=`stty -g`
104         stty -echo
105         stty cbreak
106         dd if=/dev/tty bs=1 count=1 2> /dev/null
107         stty -raw
108         stty echo
109         stty $SAVEDSTTY
110     }
111     echo ""
112     echo "Press any key to start...or Press Ctrl+C to cancel"
113     char=`get_char`
114     #Install necessary dependencies
115     cd $cur_dir
116     rpm -ivh epel-release-5-4.noarch.rpm
117     yum install -y wget unzip openssl-devel gcc gcc-c++ which python26 python26-devel autoconf libtool libevent automake make curl curl-devel zlib-devel perl pcre pcre-devel cpio expat-devel gettext-devel
118     mv /usr/bin/python /usr/bin/python.old
119     ln -s /usr/bin/python26 /usr/bin/python
120     old_python=`cat /usr/bin/yum |awk 'NR==1'|awk -F "/" '{print $4}'|awk -F"python" '{print $NF}'`
121     ls -l /usr/bin/python2.4 >/tmp/t.txt
122         python=`cat /root/t.txt |awk '{print $NF}'|awk -F "python" '{print $NF}'`
123     if [[ $old_python = $python ]];then
124         rm -f /tmp/t.txt
125         echo "The two Pythons are identical !"
126     else
127         sed -i 's/python/python2.4/g' /usr/bin/yum
128         rm -f /tmp/t.txt
129         echo "Python has been fixed successfully !"
130     fi
131     # Get IP address
132     echo "Getting Public IP address, Please wait a moment..."
133     IP=`curl -s checkip.dyndns.com | cut -d' ' -f 6  | cut -d'<' -f 1`
134     if [ $? -ne 0 -o -z $IP ]; then
135         IP=`curl -s -4 ipinfo.io | grep "ip" | awk -F\" '{print $4}'`
136     fi
137     echo -e "Your main public IP is\t\033[32m$IP\033[0m"
138     echo ""
139     cd $cur_dir
140 }
141  
142 # Download files
143 function download_files(){
144     if [ -f setuptools-12.4.zip ]; then
145         echo "setuptools-12.4.zip [found]"
146     else
147         echo "setuptools-12.4.zip not found!!!download now......"
148         if ! wget --no-check-certificatehttps://pypi.python.org/packages/source/s/setuptools/setuptools-12.4.zip; then
149             echo "Failed to download setuptools-12.4.zip!"
150         fi
151     fi
152  
153     if [ -f pip-6.0.8.tar.gz ]; then
154         echo "pip-6.0.8.tar.gz [found]"
155     else
156         echo "pip-6.0.8.tar.gz not found!!!download now......"
157         if ! wget -c --tries=3 http://download.slogra.com/python/pip-6.0.8.tar.gz; then
158             echo "Failed to download pip-6.0.8.tar.gz!"
159         fi
160     fi
161  
162     if [ -f swig-3.0.0.tar.gz ]; then
163         echo "swig-3.0.0.tar.gz [found]"
164     else
165         echo "swig-3.0.0.tar.gz not found!!!download now......"
166         if ! wget http://jaist.dl.sourceforge.net/project/swig/swig/swig-3.0.0/swig-3.0.0.tar.gz; then
167             echo "Failed to download swig-3.0.0.tar.gz!"
168         fi
169     fi
170     # Download shadowsocks chkconfig file
171     if ! wget --no-check-certificatehttps://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-O /etc/init.d/shadowsocks; then
172         echo "Failed to download shadowsocks chkconfig file!"
173     fi
174 }
175  
176 #install setuptools
177 function install_setuptools(){
178     if [ ! -f /usr/bin/easy_install ];then
179         cd $cur_dir
180         unzip setuptools-12.4.zip
181         cd $cur_dir/setuptools-12.4
182         python setup.py build
183         python setup.py install
184     else
185         echo "setuptools had been installed!"
186     fi
187  
188     if [ ! -f /usr/local/bin/swig ];then
189         cd $cur_dir
190         tar zxf swig-3.0.0.tar.gz
191         cd $cur_dir/swig-3.0.0
192         ./configure
193         make
194         make install
195         ln -s /usr/local/bin/swig /usr/bin/swig
196     else
197         echo "swig had been installed!"
198     fi
199 }
200  
201 # Config shadowsocks
202 function config_shadowsocks(){
203     cat > /etc/shadowsocks.json<<-EOF
204 {
205     "server":"0.0.0.0",
206     "server_port":8989,
207     "local_address": "127.0.0.1",
208     "local_port":1080,
209     "password":"${shadowsockspwd}",
210     "timeout":300,
211     "method":"aes-256-cfb",
212     "fast_open":false
213 }
214 EOF
215 }
216  
217 # iptables set
218 function iptables_set(){
219     echo "iptables start setting..."
220     /sbin/service iptables status 1>/dev/null 2>&1
221     if [ $? -eq 0 ]; then
222         /etc/init.d/iptables status | grep '8989' | grep 'ACCEPT' >/dev/null 2>&1
223         if [ $? -ne 0 ]; then
224             /sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8989 -j ACCEPT
225             /etc/init.d/iptables save
226             /etc/init.d/iptables restart
227         else
228             echo "port 8989 has been set up."
229         fi
230     else
231         echo "iptables looks like shutdown, please manually set it if necessary."
232     fi
233 }
234  
235 # Install
236 function install(){
237     which pip > /dev/null 2>&1
238     if [ $? -ne 0 ]; then
239         #python ez_setup.py install
240         /usr/bin/easy_install pip-6.0.8.tar.gz
241     fi
242     if [ -f /usr/bin/pip ]; then
243         pip install M2Crypto
244         pip install greenlet
245         pip install gevent
246         pip install shadowsocks
247         if [ -f /usr/bin/ssserver ]; then
248             chmod +x /etc/init.d/shadowsocks
249             # Add run on system start up
250             chkconfig --add shadowsocks
251             chkconfig shadowsocks on
252             # Run shadowsocks in the background
253             /etc/init.d/shadowsocks start
254         else
255             echo ""
256             echo "Shadowsocks install failed!"
257             exit 1
258         fi
259         clear
260         echo ""
261         echo "Congratulations, shadowsocks install completed!"
262         echo -e "Your Server IP: \033[41;37m ${IP} \033[0m"
263         echo -e "Your Server Port: \033[41;37m 8989 \033[0m"
264         echo -e "Your Password: \033[41;37m ${shadowsockspwd} \033[0m"
265         echo -e "Your Local IP: \033[41;37m 127.0.0.1 \033[0m"
266         echo -e "Your Local Port: \033[41;37m 1080 \033[0m"
267         echo -e "Your Encryption Method: \033[41;37m aes-256-cfb \033[0m"
268         echo ""
269         echo "Welcome to visit:http://blog.slogra.com"
270         echo "Enjoy it!"
271         echo ""
272         exit 0
273     else
274         echo ""
275         echo "pip install failed!"
276         exit 1
277     fi
278 }
279  
280 # Uninstall Shadowsocks
281 function uninstall_shadowsocks(){
282     printf "Are you sure uninstall Shadowsocks? (y/n) "
283     printf "\n"
284     read -p "(Default: n):" answer
285     if [ -z $answer ]; then
286         answer="n"
287     fi
288     if [ "$answer" = "y" ]; then
289         NODE_PID=`ps -ef | grep -v grep | grep -v ps | grep -i '/usr/bin/python /usr/bin/ssserver' | awk '{print $2}'`
290         if [ ! -z $NODE_PID ]; then
291             for pid in $NODE_PID
292             do
293                 kill -9 $pid
294                 if [ $? -eq 0 ]; then
295                     echo "Shadowsocks process[$pid] has been killed"
296                 fi
297             done
298         fi
299         chkconfig --del shadowsocks
300         # delete config file
301         rm -f /etc/shadowsocks.json
302         rm -f /var/run/shadowsocks.pid
303         rm -f /etc/init.d/shadowsocks
304         pip uninstall -y shadowsocks
305         if [ $? -eq 0 ]; then
306             echo "Shadowsocks uninstall success!"
307         else
308             echo "Shadowsocks uninstall failed!"
309         fi
310     else
311         echo "uninstall cancelled, Nothing to do"
312     fi
313 }
314  
315 # Install Shadowsocks-python
316 function install_shadowsocks(){
317     checkos
318     rootness
319     disable_selinux
320     download_epel
321     pre_install
322     download_files
323     install_setuptools
324     config_shadowsocks
325     iptables_set
326     install
327 }
328  
329 # Initialization step
330 action=$1
331 [  -z $1 ] && action=install
332 case "$action" in
333 install)
334     install_shadowsocks
335     ;;
336 uninstall)
337     uninstall_shadowsocks
338     ;;
339 *)
340     echo "Arguments error! [${action} ]"
341     echo "Usage: `basename $0` {install|uninstall}"
342     ;;
343 esac

好了,为了给更多的技术人员可以使用到google服务,希望这个脚本给很多懒人带来方便.
ps:
希望原作者不会怪我把他的脚本改得看起来很低端,我也无耻的把制作人改成了我自己.


(责任编辑:IT)