#!/bin/bash
# Script Name   : OneShellScript.sh
# Description   : 这个一个综合脚本，可以自动完成相关的软件操作。
# Author        : 秋风落叶 https://wangjian.run
# Date          : 2024-09-04
#==============================自定义的全局变量==============================
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export LANG=en_US.utf8
ipv4=$( ip addr | grep inet | grep -E -v "docker|127.0.0.1|inet6|safeline-ce" | awk '{print $2}' | awk -F "/" '{print $1}' | tail -n 1)
neicun=$(free -m|grep Mem|awk '{print $2}')
is64bit=$(getconf LONG_BIT)
CPU_INFO=$(getconf _NPROCESSORS_ONLN)
DangQianShiJian=$(date +"%Y-%m-%d %H:%M:%S") #当前时间


#==============================自定义的全局判断,如果不符合就退出==============================
LianWang(){
#判断是否可以联网，如果不能联网，就退出操作
if ping -c 1 baidu.com &>/dev/null ; then    
echo -e "\e[32m连接互联网正常\e[0m"
else    
#一闪一闪
#echo -e "\e[5;31m==========本地连接互联网失败,请先连接网络==========\e[0m"    
echo -e "\e[31m==========本地连接互联网失败,请先连接网络==========\e[0m"
exit
fi

}

#是否是root用户操作,如果不是就退出
if [ $(whoami) != "root" ];then
	echo "==========请使用root权限执行操作!=========="
	#exit 1;
fi



#判断系统是否是7版本,如果不是就退出
# Centos6Check=$(cat /etc/redhat-release | grep ' 6.' | grep -iE 'centos|Red Hat')
# if [ "${Centos6Check}" ];then # 当字符串 str1 为非空时为真
# 	echo "==========此脚本只支持RedHat或者CentOS的7和8版本的系列,其它系统暂不支持=========="
# 	exit 1
# fi 

#Centos6Check=$(cat /etc/redhat-release | grep -iE ' 7.|8.|9.' | grep -iE 'centos|Red Hat')
Centos6Check=$(cat /etc/redhat-release | grep -iE ' 7.' | grep -iE 'centos|Red Hat')
if [ -z "${Centos6Check}" ];then # 当字符串 str1 空时为真
	echo "==========此脚本只在RedHat或者CentOS的7.x版本验证过,其它系统暂未验证=========="
	#exit 1
fi 

#判断主机名是否为空,如果为空就退出
HOSTNAME_CHECK=$(cat /etc/hostname)
if [ -z "${HOSTNAME_CHECK}" ];then
	echo "当前主机名hostname为空,请先设置主机名名称"
	#exit 1
fi

#判断yum源是否可用
PanDaunYum(){
#yum check-update 2>/dev/null
#yum repolist 2>/dev/null
#方式2:# 尝试进行YUM刷新缓存操作，并重定向所有输出到/dev/null,   yum makecache total 2>/dev/null 1>/dev/null
echo -e "\e[33m==========正在检查yum源是否可用,速度取决于本机的网络环境,第一次可能比较慢==========\e[0m"
yum clean all &>/dev/null
#yum makecache &>/dev/null
yum makecache total 2>/dev/null 1>/dev/null
repo=$(yum repolist | grep repolist | awk '{print $2}') > /dev/null
clear
if [ "$repo" = "0" ];then
	echo -e "\e[31m==========yum源不可用,请先配置yum源==========\e[0m"
	exit
else
	echo -e "\e[32m====================yum源可用,可用源如下====================\e[0m"
	yum repolist 
fi
}
#判断是否挂载了光盘，没有就退出
gpp(){
	gpgp=$(lsblk | grep sr0 | awk '{print $4}')
	if [ "$gpgp" == "1024M" ];then
		echo "没有光盘请先挂载光盘"
		exit 
	fi
}
#==============================自定义函数==============================


#echo "==============现在时间是:$DangQianShiJian=============="
#PS3="请选择对应的序号:"
#系统基本信息
dizhisi(){
#判断系统是否是64位不是就退出
# is64bit=$(getconf LONG_BIT)
# if [ "${is64bit}" != '64' ];then
# 	Red_Error "抱歉, 当前面板版本不支持32位系统, 请使用64位系统或安装宝塔5.9!";
# fi


echo "本机系统ipv4地址是:$ipv4"
echo "本机系统是:$is64bit位系统"
echo "本机系统的内存是:$neicun M"
echo "本机系统CPU核数是:$CPU_INFO核"
}
#配置阿里centos7源
yumyuan(){
LianWang
rm -rf  /etc/yum.repos.d/CentOS-Base.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
}
#基础软件安装
basesoft(){
echo -e "\e[32m====================开始安装常用软件====================\e[0m"
yum clean all
yum makecache
yum -y install \
vim \
telnet \
net-tools \
lrzsz \
wget \
zip \
unzip \
nc \
telnet \
dos2unix \
fontconfig \
urw-fonts \
yum-utils \
wget \
bash-completion \
lsof
}

#万能文件复制
wangnengkaobei(){
read -p "请输入要复制的文件名称:" bendiwenjian
read -p "请输入远程服务器的ssh端口:" ycdk
echo -e "==============目前阿里云服务器IP是:106.14.95.216=============="
read -p "请输入远程服务器的ip地址:" ycipdizhi
read -p "请输入远程服务器的用户名:" ycname
echo -e "==============存放目录是:/www/wwwroot/wangjian.run/ShellScript/=============="
read -p "请输入远程服务器存放的目录:" ycmulu
scp -P $ycdk $bendiwenjian $ycname@$ycipdizhi:$ycmulu

}
#关闭安全设置
anquan(){
systemctl stop firewalld
systemctl disable firewalld
echo "==============防火墙关闭,状态如下:=============="
systemctl status firewalld | grep Active:
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
echo "==============selinux关闭,状态入如下:=============="
more /etc/selinux/config | grep -v ^#|grep SELINUX=

}
sese(){
#临时关闭selinux
setenforce 0
#永久关闭selinux,重启生效
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
echo "==============elinux关闭,状态入如下:=============="
more /etc/selinux/config | grep -v ^#|grep SELINUX=
}

#	#自动配置固定IP
#	ippeizhi(){
#	#取netmask   ifconfig默认是未安装的
#	#mask=$(ifconfig | grep netmask | head -n1 | awk '{print $4}')
#
#	#取网卡名
#	wkm=$(ip addr |grep ens | head -n1 | awk '{print $2}' | awk -F ":" '{print $1}')
#
#	#route -n | awk 'NR==3' | awk '{print $8}'方式二
#	#BOOTPROTO修改
#	sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#	#配置ip地址
#	echo "IPADDR0=$ipv4" >> /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#	#设置子网掩码
#	echo "PREFIX0=24" >> /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#	#dns设置
#	dnsdz=$(more /etc/resolv.conf | awk 'NR==3' | awk '{print $2}')
#	echo "DNS1=$dnsdz" >> /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#
#	#设置网关
#	wgdz=$(ip route | grep default | awk '{print $3}')
#	echo "GATEWAY0=$wgdz" >> /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#
#	#重启网络
#	systemctl restart network
#
#	echo "固定IP配置完成,详情如下:"
#	cat /etc/sysconfig/network-scripts/ifcfg-"$wkm"
#
#	}
#
#本地配置yum源
BenDiYumYuan(){
	#先检查光盘是否存在
	gpp
	mount /dev/sr0 /mnt &>/dev/null
	echo "mount /dev/sr0 /mnt" >> /etc/rc.local
	chmod +x /etc/rc.local
	mkdir -p /opt/wj-bak/yum-old/
		#这里需要加个判断是否是redhat系统,目前还没写
		cp /etc/yum/pluginconf.d/subscription-manager.conf /etc/yum/pluginconf.d/subscription-manager.conf.bak
		cp /etc/yum/pluginconf.d/fastestmirror.conf /etc/yum/pluginconf.d/fastestmirror.conf.bak
		sed -i s/enabled=1/enabled=0/g /etc/yum/pluginconf.d/subscription-manager.conf
		sed -i s/enabled=1/enabled=0/g /etc/yum/pluginconf.d/fastestmirror.conf
		mv /etc/yum.repos.d/* /opt/wj-bak/yum-old/
cat > /etc/yum.repos.d/wj.repo << EOF
[local_wj] 
name=wj_wj
baseurl=file:///mnt
enabled=1 
gpgcheck=0
EOF
		yum clean all && yum makecache &>/dev/null
		echo -e "\e[32m##########本地离线yum源配置完成,启用的源如下:##########\e[0m"
		yum repolist 
		exit
}
#nginx安装
NginxAnZhuang(){
#-M参数不创建对应的家目录
#-s参数指定登录后的Shell解释器为/sbin/nologin，确保任何人都不能通过这个账号登录主机
NginxBanBenHao=1.26.2
yum -y install gcc zlib zlib-devel openssl-devel
#useradd nginx -M -s /sbin/nologin
curl -O http://nginx.org/download/nginx-$NginxBanBenHao.tar.gz
tar zxvf nginx-$NginxBanBenHao.tar.gz
cd nginx-$NginxBanBenHao/ || exit
./configure --prefix=/opt/nginx-$NginxBanBenHao \
--with-http_ssl_module \
--without-http_rewrite_module \
--with-http_stub_status_module \
--with-http_gzip_static_module
jieguo=$(echo $?)
if [ "$jieguo" = 0 ];then
  make && make install
  jieguoer=$(echo $?)
  if [ "$jieguoer" = 0 ];then
	echo "nginx安装完成"
	cd /opt/nginx-$NginxBanBenHao/sbin/ && ./nginx
	echo "启动信息如下:"  
	ps -ef | grep nginx
	echo "安装的版本信息如下:"
	cd /opt/nginx-$NginxBanBenHao/sbin/ && ./nginx -V
  else
	echo "安装失败，请检查"
  fi
else
  echo "配置检查失败"
fi

}

#root用户离线安装
NginxLiXian(){
yum -y install gcc zlib zlib-devel openssl-devel
if [ -e /root/nginx-1.22.1.tar.gz ];then
	echo "++++++++++++++++++++++++++++++++++++++++++离线文件存在开始执行安装++++++++++++++++++++++++++++++++++++++++++"
	tar zxvf nginx-1.22.1.tar.gz
	cd nginx-1.22.1 || exit
	./configure --prefix=/root/nginx/ \
	--with-http_v2_module \
	--with-stream_ssl_module \
	--with-http_ssl_module \
	--with-http_gzip_static_module \
	--without-http_rewrite_module
	jieguo=$(echo $?)
	if [ "$jieguo" = 0 ];then
		make && make install
		jieguoer=$(echo $?)
		if [ "$jieguoer" = 0 ];then
			sed -i 's/#user  nobody/user root/g' /root/nginx/conf/nginx.conf
			cd /root/nginx/sbin/ && ./nginx
			echo "启动信息如下:"  
			pgrep -f|grep nginx 
			echo "++++++++++++++++++安装的版本信息如下++++++++++++++++++:"
			cd /root/nginx/sbin/ && ./nginx -V
		else
			echo "安装失败，请检查"
		fi
	else
		echo "配置检查失败"
	fi

else
	echo "++++++++++++++++++++++++++++++++++++++++++离线文件不存在,请先在/root/nginx/目录下放置nginx-1.22.1.tar.gz离线包++++++++++++++++++++++++++++++++++++++++++"
fi
}
#you-get
youget(){
yum -y install python3-pip
pip3 install you-get
jieguo=$(echo $?)
if [ $jieguo == 0 ];then
    echo "**********you-get安装成功**********"
else
	echo "you-get安装失败"
	exit
fi

}


#==============================菜单==============================
while true ;do
echo "============================OneShellScript控制面板============================"
echo "========================现在时间是:$DangQianShiJian========================"
echo -e "\e[33m===重要提示:在使用脚本之前,请先把yum源及基础软件安装好配置好,不然可能会出现其它意想不到的结果===\e[0m"
PS3="请选择对应的序号:"
select i in "系统基本信息" "检测yum源是否可用" "本地配置yum源" "在线配置阿里centos7源" "基础软件安装" "关闭防火墙和selinux" "关闭selinux" "本地文件复制到远程" "在线编译安装nginx-1.26.2" "离线编译安装nginx-1.22.1" "you-get在线一键安装" "退出";
	do
	case $i in
	"系统基本信息")
		echo "==============系统基本信息如下=============="
		dizhisi
		;;
	"检测yum源是否可用")
		PanDaunYum
		break
		;;
	"本地配置yum源")
		echo "==============开始yun源配置=============="
		BenDiYumYuan
		#break
		;;
	"在线配置阿里centos7源")
		yumyuan
		echo "阿里centos7源配置完成"
		;;
	"基础软件安装")
		PanDaunYum
		echo -e "\e[32m====================首先更新yum源数据,首次可能比较慢,取决你的网络环境====================\e[0m"
		basesoft
		echo -e "\e[32m====================常用安装软件安装完成====================\e[0m"
		break
		;;
	"关闭防火墙和selinux")
		echo -e "\e[32m==============开始关闭防火墙和selinux==============\e[0m"
		anquan
		echo -e "\e[32m==============防火墙和selinux关闭完成==============\e[0m"
		;;
	"关闭selinux")
		sese
		;;
	"本地文件复制到远程")
		wangnengkaobei
		;;
	"在线编译安装nginx-1.26.2")
		#PanDaunYum
		#basesoft
		NginxAnZhuang
		break
		;;
	"离线编译安装nginx-1.22.1")
		NginxLiXian
		break
		;;
			
	"you-get在线一键安装")
		youget
		break
		;;
	"退出")
		exit
		;;
	*)
		echo -e "\e[31m你输入的序号不对,请重新输入!!!\e[0m"
		break	
	esac
	done 
done
