[shell]脚本库(Python,PostgreSQL,vsftpd)

318次阅读
没有评论

Python多版本编译安装脚本

#!/bin/bash
# version:v1.0
# func:Python 3.5 3.6 3.7安装

# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
src_path=/usr/local/src/
install_log_name=install_python.log
env_file=/etc/profile.d/python.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
python_dir=/usr/local/python3
sys_version=`rpm -q centos-release|cut -d- -f3`

clear
echo "##########################################"
echo "#                                        #"
echo "#          安装 Python 3.5 3.6 3.7       #"
echo "#                                        #"
echo "##########################################"
echo "1: Install Python-3.5"
echo "2: Install Python-3.6"
echo "3: Install Python-3.7"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion

# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
ok_msg() {
    for msg in $*;do
        action $msg /bin/true
    done
}
error_msg() {
    for msg in $*;do
        action $msg /bin/false
    done
}

base_yum_install() {
  echo '----------------------------环境安装-------------------------------'
  for package in $*;do
    yum install -y ${package} &> /dev/null
    ok_msg "安装软件包:${package}"
  done
  if [ $? -eq 0 ];then
          echo "`date +%F' '%H:%M:%S` 环境安装完成">>${install_log_path}${install_log_name} && return 0
  else
    echo "`date +%F' '%H:%M:%S` 环境安装失败">>${install_log_path}${install_log_name} && return 1
  fi
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
        ok_msg "命令检查:$1"
        hash $1 &> /dev/null
        if [ $? -eq 0 ];then
            echo "`date +%F' '%H:%M:%S` $1 命令检测完成" >> ${install_log_path}${install_log_name} && return 0
        else
            yum -y install $2 >/dev/null 2>&1
        fi
}

# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
    echo "----------------------------目录检测-------------------------------"
    for dirname in $*;do
        [ -d $1 ] || mkdir -p $dirname >/dev/null 2>&1
        ok_msg "目录检查:${dirname}"
        echo "`date +%F' '%H:%M:%S` $dirname 目录检测完成" >> ${install_log_path}${install_log_name}
    done
}

# 下载文件并解压至安装目录,传入url链接地址
download_file() {
    ok_msg "下载源码包:$2"
    mkdir -p $download_path 
    wget $1 -c -P $download_path &> /dev/null
    if [ $? -eq 0 ];then
       echo "`date +%F' '%H:%M:%S` $2 下载成功">>${install_log_path}${install_log_name}
    else
       echo "`date +%F' '%H:%M:%s` $2 下载失败">>${install_log_path}${install_log_name} && exit 1
    fi
}


# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
   ok_msg "解压源码包:$2"
   cd ${download_path}
   for file in $*;do
       if [ "${file##*.}" == "gz" ];then
           tar -zxf $file -C $src_path && echo "`date +%F' '%H:%M:%S` $file 解压成功">>${install_log_path}${install_log_name}
       elif [ "${file##*.}" == "zip" ];then
           unzip -q $file -d $src_path && echo "`date +%F' '%H:%M:%S` $file 解压失败">>${install_log_path}${install_log_name}
       elif [ "${file##*.}" == "tgz" ];then
		   tar -xf $file -C $src_path && echo "`date +%F' '%H:%M:%S` $file 解压成功">>${install_log_path}${install_log_name}
	   else
           echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
       fi
    done
}

# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
    ok_msg "环境变量配置"
    echo "export PATH=\$PATH:$1" >${env_file}
    source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}

}


# Python编译函数
compile_python() {
    echo '----------------------------安装Python-----------------------------'
    cd ${src_path}${python_name}
    ./configure --prefix=${python_dir} --enable-shared CFLAGS=-fPIC &> /dev/null
    ok_msg "编译完成"
    echo "`date +%F' '%H:%M:%S` 编译完成!">> ${install_log_path}${install_log_name}
    make &> /dev/null
    make install &> /dev/null
    ok_msg "安装完成"
    ln -s ${python_dir}bin/python3 /usr/bin/python3
    ln -s ${python_dir}bin/pip3 /usr/bin/pip3
    ok_msg "软连接配置"
    echo "`date +%F' '%H:%M:%S` 软连接建立完成!">> ${install_log_path}${install_log_name}
    if [ ${softversion} == "1" ];then
    	/bin/cp libpython3.5m.so.1.0 /usr/lib64/
    	/bin/cp libpython3.5m.so.1.0 /usr/lib/
    elif [ ${softversion} == "2" ];then
    	/bin/cp libpython3.6m.so.1.0 /usr/lib64/
    	/bin/cp libpython3.6m.so.1.0 /usr/lib/
    elif [ ${softversion} == "3" ];then
    	/bin/cp libpython3.7m.so.1.0 /usr/lib64/
    	/bin/cp libpython3.7m.so.1.0 /usr/lib/
    fi
}
main() {
	check_dir $install_log_path $install_path ${python_dir} ${src_path} ${download_path}
    base_yum_install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline-devel openssl openssl-devel openssl-static xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel
	check_yum_command wget wget
	check_yum_command unzip unzip
	download_file $URL Python
	for filename in `ls $download_path`;do
		extract_file ${download_path}$filename ${filename}
	done
	python_name=`ls ${src_path} | grep Python`
	compile_python
	config_env ${python_dir}/bin
	rm -fr ${download_path}
}
case ${softversion} in
	1)
		URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/Python/Python-3.5.6.tgz"
		main
	;;
	2)
		URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/Python/Python-3.6.4.tgz"
		main
	;;
	3)
		URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/Python/Python-3.7.0.tgz"
		main
	;;
	4)
		exit 0
	;;
	*)
		echo "input Error! Place input{1|2|3|4}"
		exit 1
esac

PostgreSQL多版本安装脚本

#!/bin/bash
# CentOS 6|7 已测试
# Time: 2018-8-20
# install_postgresql
# CentOS 6|7已适配
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
install_log_name=install_postgresql.log
env_file=/etc/profile.d/postgresql.sh
install_log_path=/var/log/appinstall/
download_path=/tmp/tmpdir/
sys_version=`rpm -q centos-release|cut -d- -f3`


clear
echo "##########################################"
echo "#                                        #"
echo "#        安装 PostgreSQL 9.4 9.6 10      #"
echo "#                                        #"
echo "##########################################"
echo "1: Yum install PostgreSQL-9.4"
echo "2: Yum install PostgreSQL-9.6"
echo "3: Yum install PostgreSQL-10.5"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion

# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
ok_msg() {
    for msg in $*;do
        action $msg /bin/true
    done
}
error_msg() {
    for msg in $*;do
        action $msg /bin/false
    done
}
base_yum_install() {
	echo '----------------------------环境安装-------------------------------'
	for package in $*;do
		yum install -y ${package} &> /dev/null
		ok_msg "安装软件包:${package}"
	done
	if [ $? -eq 0 ];then
	        echo "`date +%F' '%H:%M:%S` 环境安装完成">>${install_log_path}${install_log_name} && return 0
	else
		echo "`date +%F' '%H:%M:%S` 环境安装失败">>${install_log_path}${install_log_name} && return 1
	fi
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
    ok_msg "命令检查:$1"
    hash $1 &> /dev/null
    if [ $? -eq 0 ];then
        echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
    else
        yum -y install $2 >/dev/null 2>&1
    fi
}

# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
    echo '----------------------------目录检测-------------------------------'
    for dirname in $*;do
        [ -d ${dirname} ] || mkdir -p $dirname &> /dev/null
    	ok_msg "目录检查:${dirname}"
    done
    echo "`date +%F' '%H:%M:%S` 目录检查完成" >> ${install_log_path}${install_log_name}
}

# 下载文件并解压至安装目录,传入url链接地址
download_file() {
    ok_msg "下载源码包:$2"
    mkdir -p $download_path 
    wget $1 -c -P $download_path &> /dev/null
    if [ $? -eq 0 ];then
       echo "`date +%F' '%H:%M:%S` $2 下载成功">>${install_log_path}${install_log_name}
    else
       echo "`date +%F' '%H:%M:%s` $2 下载失败">>${install_log_path}${install_log_name} && exit 1
    fi
}

# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
   ok_msg "解压源码包:$2"
   cd ${download_path}
   for file in $1;do
       if [ "${file##*.}" == "gz" ];then
           tar -zxf $file -C ${src_path} && echo "`date +%F' '%H:%M:%S` $file 解压成功">>${install_log_path}${install_log_name}
       elif [ "${file##*.}" == "zip" ];then
           unzip -q $file -d ${src_path} && echo "`date +%F' '%H:%M:%S` $file 解压失败">>${install_log_path}${install_log_name}
       else
           echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
       fi
    done
}

# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
    ok_msg "环境变量配置"
    echo "export PATH=\$PATH:$1" >${env_file}
    source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}

}


# 编译PostgreSQL函数
start_postgresql() {
	yum list | grep postgresql &> /dev/null
	if [ $? -eq 0 ];then
		 base_yum_install ${postsqlname}-server ${postsqlname} ${postsqlname}-libs
		 echo "`date +%F' '%H:%M:%S` 安装成功">> ${install_log_path}${install_log_name}
	else
		error_msg "安装"
		echo "No packages valid in yum repos,Please download repo files in https://yum.postgresql.org/"
		echo "`date +%F' '%H:%M:%S` No packages valid in yum repos,Please download repo files in https://yum.postgresql.org/">> ${install_log_path}${install_log_name}
		exit 1
	fi
	if [ ${sys_version} == "7" ];then
		if [ ${softversion} == "3" ];then
			/usr/pgsql-${version}/bin/postgresql-${version}-setup initdb &> /dev/null
		fi
		/usr/pgsql-${version}/bin/${postsqlname}-setup initdb &> /dev/null
	elif [ ${sys_version} == "6" ];then
		service ${servicename} initdb &> /dev/null
	fi
	ok_msg "初始化"
	if [ ${sys_version} == "7" ];then
		systemctl start ${servicename}
	elif [ ${sys_version} == "6" ];then
		/etc/init.d/${servicename} start &> /dev/null
	fi
	
	netstat -tunlp | grep 5432 &> /dev/null
	[ $? -eq 0 ] && ok_msg "启动PostgreSQL" || error_msg "启动PostgreSQL"
}

main() {
	check_dir ${download_path} ${install_log_path} ${pg_data}
	check_yum_command wget wget
	check_yum_command unzip unzip
	download_file ${URL} PostgreSQL
	rpm -ivh $download_path*.rpm --force &> /dev/null
	start_postgresql
	rm -fr ${download_path}
}

case ${softversion} in
	1)
		version="9.4"
		postsqlname="postgresql94"
		servicename="postgresql-9.4"
		if [ ${sys_version} == "7" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos7-9.4.noarch.rpm"
		elif [ ${sys_version} == "6" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos6-9.4.noarch.rpm"
		fi
		main
	;;
	2)
		version="9.6"
		postsqlname="postgresql96"
		servicename="postgresql-9.6"
		if [ ${sys_version} == "7" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos7-9.6.noarch.rpm"
		elif [ ${sys_version} == "6" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos6-9.6.noarch.rpm"
		fi
		main
	;;
	3)
		version="10"
		postsqlname="postgresql10"
		servicename="postgresql-10"
		if [ ${sys_version} == "7" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos7-10-2.noarch.rpm"
		elif [ ${sys_version} == "6" ];then
			URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/PostgreSQL/pgdg-centos6-10-2.noarch.rpm"
		fi
		main
	;;
	4)
		exit 0
	;;
	*)
		echo "input Error! Place input{1|2|3}"
		exit 1
esac

vsftpd译安装脚本

#!/bin/bash
# date:2018-08-10
# install_vsftpd
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo -e "\033[31mError: You must be root to run this script\033[0m" && exit 1
#Check install status
echo -e "\033[33mChecking...\033[0m"
rpm -q vsftpd &> /dev/null
[ $? -eq 0 ] && echo -e "\033[31mVsftpd had installed,exit...\033[0m" && exit 1

clear
echo "##########################################"
echo "#                                        #"
echo "#          安装 vsftpd                   #"
echo "#                                        #"
echo "##########################################"
echo "1: Install vsftpd"
echo "2: EXIT"
read -p "Please input your choice:" softversion

install() {
	echo "----------------------------安装中-------------------------------"
	yum install vsftpd -y &> /dev/null
}
config() {
	echo "----------------------------配置中-------------------------------"
	cat >/etc/vsftpd/vsftpd.conf <<EOF
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_local_user=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES
guest_username=ftp
user_config_dir=/etc/vsftpd/vconf
pasv_enable=YES
pasv_min_port=30001
pasv_max_port=30005
userlist_deny=NO
EOF
	touch /etc/vsftpd/account.txt
	[ ! -d /etc/vsftpd/vconf ] && mkdir /etc/vsftpd/vconf
	db_load -T -thash -f /etc/vsftpd/account.txt /etc/vsftpd/account.db
	cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
	> /etc/pam.d/vsftpd
	cat >/etc/pam.d/vsftpd <<EOF
auth 	   required     /lib64/security/pam_userdb.so db=/etc/vsftpd/account
account    required     /lib64/security/pam_userdb.so db=/etc/vsftpd/account
EOF
	[ $? -eq 0 ] && action "安装vsftpd" /bin/true || action "安装vsftpd" /bin/false
	/etc/init.d/vsftpd start &> /dev/null
	netstat -tunlp | grep vsftpd &> /dev/null
	[ $? -eq 0 ] && action "启动服务" /bin/true || action "启动服务" /bin/false
}
main() {
	install
	config
}

case ${softversion} in
	1)
		main
	;;
	2)
		exit 0
	;;
	*)
		echo "input Error! Place input{1|2|3|4}"
		exit 1
esac

 

 

Honest1y
版权声明:本站原创文章,由Honest1y于2018年05月20日发表,共计14800字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)