转换文档格式

  1. sed -i 's/\r$//' *.sh

1、服务器信息巡检

  1. #!/bin/bash
  2. echo "系统巡检脚本:Version `date +%F`"
  3. echo "信安"
  4. echo -e "****************系统检查****************"
  5. echo "系统:`uname -a | awk '{print $NF}'`"
  6. echo "发行版本:`cat /etc/os-release`"
  7. echo "内核:`uname -r`"
  8. echo "主机名:`hostname`"
  9. echo "SELinux:`/usr/sbin/sestatus | grep 'SELinux status:' | awk '{print $3}'`"
  10. echo "语言/编码:`echo $LANG`"
  11. echo "当前时间:`date +%F_%T`"
  12. echo "最后启动:`who -b | awk '{print $3,$4}'`"
  13. echo "运行时间:`uptime | awk '{print $3}' | sed 's/,//g'`"
  14. echo -e "****************CPU检查 ****************"
  15. echo "物理CPU个数: `cat /proc/cpuinfo | grep "physical id" | awk '{print $4}' | sort | uniq | wc -l`"
  16. echo "逻辑CPU个数: `cat /proc/cpuinfo | grep "processor" | awk '{print $3}' | sort | uniq | wc -l`"
  17. echo "每CPU核心数: `cat /proc/cpuinfo | grep "cores" | awk '{print $4}'`"
  18. echo "CPU型号: `cat /proc/cpuinfo | grep "model name" | awk -F":" '{print $2}'`"
  19. echo "CPU架构: `uname -m`"
  20. echo -e "****************内存检查 ****************"
  21. echo "总共内存:`free -mh | awk "NR==2"| awk '{print $2}'`"
  22. echo "使用内存:`free -mh | awk "NR==2"| awk '{print $3}'` "
  23. echo "剩余内存:`free -mh | awk "NR==2"| awk '{print $4}'`"
  24. echo -e "****************硬盘检查 ****************"
  25. echo "总共磁盘大小:`df -hT | awk "NR==2"|awk '{print $3}'`"
  26. echo -e "****************网络检查 ****************"
  27. echo "IP:`ifconfig | awk 'NR==2' | awk '{print $2}'`"
  28. echo "网关:`ip route | awk 'NR==1'| awk '{print $3}'`"
  29. echo "DNS: `cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}'`"
  30. ping -c 4 www.baidu.com > /dev/null
  31. if [ $? -eq 0 ];then
  32. echo "外网连接:正常"
  33. else
  34. echo "外网连接:失败 请检查DNS配置"
  35. fi
  36. echo -e "****************安全检查****************"
  37. echo "登陆用户信息:`last | grep "still logged in" | awk '{print $1}'| sort | uniq`"
  38. md5sum -c --quiet /etc/passwd > /dev/null 2&>1

2、sh部署jdk+tomcat+ftp被动

将apache-tomcat-8.5.65.tar.gz和jdk-8u291-linux-x64.tar.gz跟脚本放到同级目录

  1. #!/bin/bash
  2. echo "---------------------执行java部署---------------------"
  3. yum -y install net-tools vim unzip
  4. java_home="/usr/local/java"
  5. if [ ! -d $java_home ];then
  6. mkdir -p $java_home
  7. fi
  8. #为了脚本可以重复执行,如果已经安装过,就删除
  9. if [ ! -d "${java_home}/jdk1.8.0_291" ]; then
  10. echo "没有配置过java"
  11. else
  12. rm -rf "${java_home}/jdk1.8.0_291"
  13. fi
  14. echo "解压jdk到$java_home"
  15. tar -zxf jdk-8u291-linux-x64.tar.gz
  16. mv jdk1.* $java_home
  17. echo "配置环境变量"
  18. echo 'export JAVA_HOME='${java_home}'/jdk1.8.0_291' > /etc/profile
  19. echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
  20. echo 'export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar' >> /etc/profile
  21. echo "环境变量配置完成"
  22. sleep 2
  23. source /etc/profile
  24. java -version
  25. echo ""
  26. echo "---------------------执行tomcat部署---------------------"
  27. #为了脚本可以重复执行,如果已经安装过,就不操作
  28. tomcat_file=/usr/local/tomcat
  29. if [ ! -d "${tomcat_file}" ]; then
  30. mkdir $tomcat_file
  31. fi
  32. for ((i=1;i<=2;i++));
  33. do
  34. if [ $i == 1 ]; then
  35. ${tomcat_file}/Tomcat_DzwlServer/bin/shutdown.sh &> /dev/null
  36. if [ ! -d "${tomcat_file}/Tomcat_DzwlServer" ]; then
  37. echo "创建${tomcat_file}/Tomcat_DzwlServer"
  38. tar -zxvf apache-tomcat-8.5.65.tar.gz
  39. mv apache-tomcat-8.5.65 ${tomcat_file}/Tomcat_DzwlServer
  40. echo "配置Tomcat_DzwlServer开机自启"
  41. echo 'export JAVA_HOME='${java_home}'/jdk1.8.0_291' >> /etc/rc.d/rc.local
  42. echo $tomcat_file'/Tomcat_DzwlServer/bin/startup.sh start' >> /etc/rc.d/rc.local
  43. echo "Tomcat_DzwlServer开机自启已配置"
  44. echo "Tomcat_DzwlServer配置8090端口号"
  45. sed -i 's/Connector port="8080"/Connector port="8090"/' $tomcat_file'/Tomcat_DzwlServer/conf/server.xml'
  46. grep "<Connector port=" $tomcat_file'/Tomcat_DzwlServer/conf/server.xml'
  47. else
  48. echo "已存在${tomcat_file}/Tomcat_DzwlServer"
  49. fi
  50. elif [ $i == 2 ]; then
  51. ${tomcat_file}/Tomcat_webdzwlclient/bin/shutdown.sh &> /dev/null
  52. if [ ! -d "${tomcat_file}/Tomcat_webdzwlclient" ]; then
  53. echo "创建${tomcat_file}/Tomcat_webdzwlclient"
  54. tar -zxf apache-tomcat-8.5.65.tar.gz
  55. mv apache-tomcat-8.5.65 ${tomcat_file}/Tomcat_webdzwlclient
  56. echo "配置Tomcat_DzwlServer开机自启"
  57. echo $tomcat_file'/Tomcat_webdzwlclient/bin/startup.sh start' >> /etc/rc.d/rc.local
  58. echo "Tomcat_webdzwlclient开机自启已配置"
  59. grep "<Connector port=" $tomcat_file'/Tomcat_webdzwlclient/conf/server.xml'
  60. else
  61. echo "已存在${tomcat_file}/Tomcat_webdzwlclient"
  62. fi
  63. fi
  64. done
  65. echo ""
  66. echo "---------------------执行FTP配置---------------------"
  67. ftp_name="fddFileServer"
  68. ftp_pwd="fdd"
  69. ftp_dir="/usr/share/dzwl/fddfiletmp"
  70. if [ ! -d "${ftp_dir}" ]; then
  71. mkdir -p $ftp_dir
  72. fi
  73. yum install vsftpd -y
  74. echo "开始修改ftp配置"
  75. sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' '/etc/vsftpd/vsftpd.conf'
  76. sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' '/etc/vsftpd/vsftpd.conf'
  77. sed -i 's/connect_from_port_20=YES/connect_from_port_20=NO/' '/etc/vsftpd/vsftpd.conf'
  78. sed -i 's/#xferlog_file=/xferlog_file=/' '/etc/vsftpd/vsftpd.conf'
  79. sed -i 's/#async_abor_enable=YES/async_abor_enable=YES/' '/etc/vsftpd/vsftpd.conf'
  80. sed -i 's/#ascii_upload_enable=YES/ascii_upload_enable=YES/' '/etc/vsftpd/vsftpd.conf'
  81. sed -i 's/#ascii_download_enable=YES/ascii_download_enable=YES/' '/etc/vsftpd/vsftpd.conf'
  82. sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' '/etc/vsftpd/vsftpd.conf'
  83. sed -i 's/#chroot_list_enable=YES/chroot_list_enable=YES/' '/etc/vsftpd/vsftpd.conf'
  84. sed -i 's/#chroot_list_file=/chroot_list_file=/' '/etc/vsftpd/vsftpd.conf'
  85. echo "local_root=$ftp_dir" >> /etc/rc.d/rc.local
  86. {
  87. cat <<'XUNLEI'
  88. allow_writeable_chroot=YES
  89. pasv_enable=YES
  90. pasv_min_port=61000
  91. pasv_max_port=61049
  92. #listen_port=9421
  93. pasv_address=172.16.0.7
  94. XUNLEI
  95. } >> /etc/vsftpd/vsftpd.conf
  96. echo "开始创建${ftp_name}用户"
  97. useradd -d $ftp_dir $ftp_name
  98. echo $ftp_pwd | passwd --stdin $ftp_name
  99. chmod 777 $ftp_dir
  100. if [ ! -f "/etc/vsftpd/chroot_list" ];then
  101. touch "/etc/vsftpd/chroot_list"
  102. fi
  103. echo $ftp_name >> "/etc/vsftpd/chroot_list"
  104. echo "启动ftp,并加入开机自启"
  105. systemctl restart vsftpd
  106. systemctl enable vsftpd
  107. echo ""
  108. echo "---------------------执行防火墙规则配置---------------------"
  109. echo "开启防火墙并添加端口规则"
  110. systemctl start firewalld
  111. firewall-cmd --add-port=8080/tcp --zone=public --permanent
  112. #firewall-cmd --add-port=8021/tcp--zone=public --permanent
  113. firewall-cmd --add-port=60000-60049/tcp --zone=public --permanent
  114. firewall-cmd --add-service=ftp --permanent
  115. firewall-cmd --reload

3、sh部署oracle11g

将linux.x64_11gR2_database_1of2.zip和linux.x64_11gR2_database_2of2.zip跟脚本放到同级目录 注意:如果脚本跑完测试发现 listenner监听 没启动,就查看host和配置文件的监听名是否一致

  1. #!/bin/bash
  2. oracle_SID="dzwl"
  3. oracle_pwd="oracle"
  4. oracle_basehome="/tmp"
  5. host_name="dzwl"
  6. host_ip="172.16.0.1"
  7. echo "---------------------1、执行host修改---------------------"
  8. yum -y install unzip
  9. yum -y install vim
  10. yum -y install expect
  11. hostnamectl set-hostname $host_name
  12. echo "$host_ip $host_name" >> /etc/hosts
  13. echo "---------------------2、执行防火墙,selinux修改---------------------"
  14. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' '/etc/selinux/config'
  15. systemctl stop firewalld
  16. echo "---------------------3、安装oracle依赖包---------------------"
  17. yum -y install gcc make binutils gcc-c++ compat-libstdc++-33 elfutils-libelf-devel elfutils-libelf-devel-static elfutils-libelf-devel ksh libaio libaio-devel numactl-devel sysstat unixODBC unixODBC-devel pcre-devel libXp*
  18. echo "---------------------4、新增oracle用户---------------------"
  19. groupadd oinstall
  20. groupadd dba
  21. useradd -g oinstall -G dba oracle
  22. echo $oracle_pwd | passwd --stdin oracle
  23. echo "---------------------5、修改内核参数---------------------"
  24. {
  25. cat <<'XUNLEI'
  26. fs.aio-max-nr = 1048576
  27. fs.file-max = 6815744
  28. kernel.shmall = 2097152
  29. kernel.shmmax = 1073741824
  30. kernel.shmmni = 4096
  31. kernel.sem = 250 32000 100 128
  32. net.ipv4.ip_local_port_range = 9000 65500
  33. net.core.rmem_default = 262144
  34. net.core.rmem_max = 4194304
  35. net.core.wmem_default = 262144
  36. net.core.wmem_max = 1048576
  37. XUNLEI
  38. } >> /etc/sysctl.conf
  39. sleep 1
  40. sysctl -p
  41. echo "---------------------6、修改限制文件---------------------"
  42. {
  43. cat <<'XUNLEI'
  44. oracle soft nproc 2047
  45. oracle hard nproc 16384
  46. oracle soft nofile 1024
  47. oracle hard nofile 65536
  48. oracle soft stack 10240
  49. XUNLEI
  50. } >> /etc/security/limits.conf
  51. {
  52. cat <<'XUNLEI'
  53. session required /lib64/security/pam_limits.so
  54. session required pam_limits.so
  55. XUNLEI
  56. } >> /etc/pam.d/login
  57. {
  58. cat <<'XUNLEI'
  59. if [ $USER = "oracle" ]; then
  60. if [ $SHELL = "/bin/ksh" ]; then
  61. ulimit -p 16384
  62. ulimit -n 65536
  63. else
  64. ulimit -u 16384 -n 65536
  65. fi
  66. fi
  67. XUNLEI
  68. } >> /etc/profile
  69. echo "---------------------7、创建安装目录、修改文件所属者和权限---------------------"
  70. mkdir -p /u01/app/oracle/product/11.2.0
  71. mkdir /u01/app/oracle/oradata
  72. mkdir /u01/app/oracle/inventory
  73. mkdir /u01/app/oracle/fast_recovery_area
  74. chown -R oracle:oinstall /u01/app/oracle
  75. chmod -R 777 /u01/app/oracle
  76. echo "---------------------8、解压oracle及配置文件修改---------------------"
  77. cd $oracle_basehome
  78. unzip linux.x64_11gR2_database_1of2.zip
  79. unzip linux.x64_11gR2_database_2of2.zip
  80. cp $oracle_basehome'/database/response/db_install.rsp' $oracle_basehome'/database/response/db_install.rsp.bak'
  81. sed -i 's/oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/' $oracle_basehome'/database/response/db_install.rsp'
  82. sed -i "s/ORACLE_HOSTNAME=/ORACLE_HOSTNAME=$oracle_SID/" $oracle_basehome'/database/response/db_install.rsp'
  83. sed -i 's/UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/' $oracle_basehome'/database/response/db_install.rsp'
  84. sed -i "s/INVENTORY_LOCATION=/INVENTORY_LOCATION=\/u01\/app\/oracle\/inventory/" $oracle_basehome'/database/response/db_install.rsp'
  85. sed -i 's/SELECTED_LANGUAGES=/SELECTED_LANGUAGES=en,zh_CN/' $oracle_basehome'/database/response/db_install.rsp'
  86. sed -i "s/ORACLE_HOME=/ORACLE_HOME=\/u01\/app\/oracle\/product\/11.2.0/" $oracle_basehome'/database/response/db_install.rsp'
  87. sed -i "s/ORACLE_BASE=/ORACLE_BASE=\/u01\/app\/oracle/" $oracle_basehome'/database/response/db_install.rsp'
  88. sed -i 's/oracle.install.db.InstallEdition=/oracle.install.db.InstallEdition=EE/' $oracle_basehome'/database/response/db_install.rsp'
  89. sed -i 's/oracle.install.db.DBA_GROUP=/oracle.install.db.DBA_GROUP=dba/' $oracle_basehome'/database/response/db_install.rsp'
  90. sed -i 's/oracle.install.db.OPER_GROUP=/oracle.install.db.OPER_GROUP=dba/' $oracle_basehome'/database/response/db_install.rsp'
  91. sed -i 's/DECLINE_SECURITY_UPDATES=/DECLINE_SECURITY_UPDATES=true/' $oracle_basehome'/database/response/db_install.rsp'
  92. echo "---------------------9、切换oracle用户继续执行---------------------"
  93. #su - oracle -s /bin/bash su.sh
  94. su - oracle -c "echo ORACLE_BASE=/u01/app/oracle >> ~/.bash_profile"
  95. su - oracle -c 'echo ORACLE_HOME=\$ORACLE_BASE/product/11.2.0 >> ~/.bash_profile'
  96. su - oracle -c "echo ORACLE_SID=$oracle_SID >> ~/.bash_profile"
  97. su - oracle -c 'echo PATH=\$PATH:\$ORACLE_HOME/bin >> ~/.bash_profile'
  98. su - oracle -c "echo export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH >> ~/.bash_profile"
  99. su - oracle -c 'grep -Ev "^#|^$" ~/.bash_profile'
  100. echo "---------------------10、安装数据库---------------------"
  101. su - oracle -c "$oracle_basehome/database/./runInstaller -silent -ignorePrereq -ignoreSysPrereqs -responseFile $oracle_basehome/database/response/db_install.rsp"
  102. sleep 5m
  103. #while true; do
  104. # if [ ! -f "/u01/app/oracle/inventory/orainstRoot.sh" ]; then
  105. # sleep 1
  106. # elif [ ! -f "/u01/app/oracle/product/11.2.0/root.sh" ]; then
  107. # sleep 1
  108. # else
  109. # break
  110. # fi
  111. #done
  112. echo -e "安装完成\n"
  113. sh /u01/app/oracle/inventory/orainstRoot.sh
  114. sh /u01/app/oracle/product/11.2.0/root.sh
  115. echo "---------------------11、配置监听---------------------"
  116. sed -i "s/GDBNAME = \"orcl11g.us.oracle.com\"/GDBNAME = \"$oracle_SID\"/" $oracle_basehome'/database/response/dbca.rsp'
  117. sed -i "s/SID = \"orcl11g\"/SID = \"$oracle_SID\"/" $oracle_basehome'/database/response/dbca.rsp'
  118. sed -i "s/#SYSPASSWORD = \"password\"/SYSPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
  119. sed -i "s/#SYSTEMPASSWORD = \"password\"/SYSTEMPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
  120. sed -i "s/#SYSMANPASSWORD = \"password\"/SYSMANPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
  121. sed -i "s/#DBSNMPPASSWORD = \"password\"/DBSNMPPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
  122. sed -i "s/#DATAFILEDESTINATION =/DATAFILEDESTINATION =\/u01\/app\/oracle\/oradata" $oracle_basehome'/database/response/dbca.rsp'
  123. sed -i "s/#RECOVERYAREADESTINATION=/RECOVERYAREADESTINATION=\/u01\/app\/oracle\/fast_recovery_area" $oracle_basehome'/database/response/dbca.rsp'
  124. sed -i 's/#CHARACTERSET = "US7ASCII"/CHARACTERSET = "ZHS16GBK"/' $oracle_basehome'/database/response/dbca.rsp'
  125. sed -i 's/#TOTALMEMORY = "800"/TOTALMEMORY = "1638"/' $oracle_basehome'/database/response/dbca.rsp'
  126. su - oracle -c "export DISPLAY=localhost:0.0"
  127. su - oracle -c "netca -silent -responseFile $oracle_basehome/database/response/netca.rsp"
  128. su - oracle -c "lsnrctl start"
  129. su - oracle -c "netstat -tnlup | grep 1521"
  130. su - oracle -c "dbca -silent -responseFile $oracle_basehome/database/response/dbca.rsp"
  131. echo -e "oracle的进程信息是:\n"
  132. su - oracle -c "ps -ef | grep ora_ | grep -v grep"
  133. echo -e "\n'oracle的实例名是:'$oracle_SID\n"
  134. echo -e "\n'oracle的管理密码是:'$oracle_pwd\n"
  135. echo -e "\n'oracle的路径是:'$oracle_basehome\n"
  136. echo "---12、请手动输入select * from v\$version;查询数据库版本,查询不到请startup启动数据库---"
  137. su - oracle -c "sqlplus / as sysdba"

4、防止攻击异常ip自动拦截

  1. #!/bin/bash
  2. Date=$(date +%Y-%m-%d" "%H:%M)
  3. Log_file="/var/log/deny_ip.log"
  4. deny_num=0
  5. deny_ip=$(netstat -tnlap | grep ESTABLISHED | grep -Ev "::" | awk '{print $5}' | awk -F : '{print $1}' | uniq -c | sort -rn | awk '{if ($1 > $deny_num)print $2}')
  6. touch $Log_file
  7. for ip in $deny_ip;
  8. do
  9. if !(firewall-cmd --list-rich | grep $ip );then
  10. echo "$Date加入IP黑名单:$ip"
  11. echo "$Date加入IP黑名单:$ip" >> $Log_file
  12. firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=$ip reject"
  13. else
  14. echo "已加入过IP黑名单:$ip"
  15. fi
  16. done
  17. firewall-cmd --reload

5、一键部署 LAMP 脚本实现

工作结果就是 每台服务器 都可以打开php 测试页面

  1. #!/bin/bash
  2. NGINX_V=1.15.6
  3. PHP_V=5.6.36
  4. MYSQL_V=5.7.26
  5. TMP_DIR=/tmp
  6. msyql_pwd="123"
  7. INSTALL_DIR=/usr/local
  8. echo
  9. echo -e "\tMenu\n"
  10. echo -e "1. Install Nginx"
  11. echo -e "2. Install PHP"
  12. echo -e "3. Install MySQL(需要手动刷新下/etc/profile)"
  13. echo -e "4. Deploy LNMP"
  14. echo -e "9. Quit"
  15. function command_status_check() {
  16. if [ $? -ne 0 ]; then
  17. echo $1
  18. exit
  19. fi
  20. }
  21. function install_mysql() {
  22. echo "开始安装mysql"
  23. cd $TMP_DIR
  24. #wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-${MYSQL_V}-linux-glibc2.12-x86_64.tar.gz
  25. tar zxf mysql-${MYSQL_V}-linux-glibc2.12-x86_64.tar.gz
  26. mv mysql-${MYSQL_V}-linux-glibc2.12-x86_64 $INSTALL_DIR/mysql-${MYSQL_V}
  27. useradd -s /sbin/nologin mysql
  28. mkdir $INSTALL_DIR/mysql-${MYSQL_V}/data
  29. chown -R mysql:mysql $INSTALL_DIR/mysql-${MYSQL_V}
  30. cd $INSTALL_DIR/mysql-${MYSQL_V}/bin
  31. ./mysqld --initialize-insecure --user=mysql \
  32. --basedir=$INSTALL_DIR/mysql-${MYSQL_V} \
  33. --datadir=$INSTALL_DIR/mysql-${MYSQL_V}/data
  34. command_status_check "Mysql - 平台环境检查失败!"
  35. yum -y remove mariadb*
  36. echo "导入配置参数"
  37. #导入配置参数
  38. cat > /etc/my.cnf <<EOF
  39. [mysqld]
  40. port=3306
  41. user=mysql
  42. basedir=$INSTALL_DIR/mysql-${MYSQL_V}
  43. datadir=$INSTALL_DIR/mysql-${MYSQL_V}/data
  44. socket=/tmp/mysql.sock
  45. server_id=6
  46. [mysql]
  47. socket=/tmp/mysql.sock
  48. EOF
  49. cat /etc/profile | awk 'END{print $0}' | grep "="
  50. if [ $? -eq 1 ];then
  51. echo 'export PATH=$PATH:'$INSTALL_DIR'/mysql-'${MYSQL_V}'/bin' >> /etc/profile
  52. else
  53. echo ':'$INSTALL_DIR'/mysql-'${MYSQL_V}'/bin' >> /etc/profile
  54. fi
  55. source /etc/profile
  56. cat /etc/rc.local | awk 'END{print $0}' | grep "source"
  57. if [ $? -eq 1 ];then
  58. echo 'source /etc/profile' >> /etc/rc.local
  59. fi
  60. bash /etc/rc.local
  61. yum -y install expect
  62. command_status_check "Mysql - expect安装失败!"
  63. /usr/bin/expect <<EOF
  64. mysqladmin -uroot -p password $msyql_pwd
  65. expect {
  66. "Enter passw*" { send "\r" }
  67. }
  68. expect eof
  69. EOF
  70. :'#serv方式
  71. ln -s $INSTALL_DIR/mysql-${MYSQL_V}/support-files/mysql.server /etc/init.d/mysql
  72. ln -s $INSTALL_DIR/mysql-${MYSQL_V}/bin/mysql /usr/bin/mysql
  73. service mysql restart
  74. '
  75. #systemd方式
  76. cat > /etc/systemd/system/mysqld.service <<EOF
  77. [Unit]
  78. Description=MySQL Server
  79. Documentation=man:mysqld(8)
  80. Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
  81. After=network.target
  82. After=syslog.target
  83. [Install]
  84. WantedBy=multi-user.target
  85. [Service]
  86. User=mysql
  87. Group=mysql
  88. ExecStart=$INSTALL_DIR/mysql-${MYSQL_V}/bin/mysqld --defaults-file=/etc/my.cnf
  89. LimitNOFILE=5000
  90. EOF
  91. systemctl start mysqld
  92. command_status_check "Mysql - 重启失败!"
  93. echo -e "Mysql - 安装完成 \n需要重启服务器,或者 source /etc/profile 来刷新变量\n初始的默认密码为空,你可以通过 mysqladmin -uroot -p password 新密码 来重置密码"
  94. }
  95. function install_nginx() {
  96. cd $TMP_DIR
  97. yum install -y gcc gcc-c++ make openssl-devel pcre-devel wget
  98. wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
  99. tar zxf nginx-${NGINX_V}.tar.gz
  100. cd nginx-${NGINX_V}
  101. ./configure --prefix=$INSTALL_DIR/nginx \
  102. --with-http_ssl_module \
  103. --with-http_stub_status_module \
  104. --with-stream
  105. command_status_check "Nginx - 平台环境检查失败!"
  106. make && make install
  107. command_status_check "Nginx - 安装失败!"
  108. rm -rf $INSTALL_DIR/nginx/html/*
  109. echo "ok" > $INSTALL_DIR/nginx/html/status.html
  110. cat > $INSTALL_DIR/nginx/html/status.php <<EOF
  111. <?php
  112. phpinfo();
  113. ?>
  114. EOF
  115. sed -i '40,50s/index index.html index.htm/index status.php/g' $INSTALL_DIR/nginx/conf/nginx.conf
  116. $INSTALL_DIR/nginx/sbin/nginx
  117. command_status_check "Nginx - 启动失败!"
  118. cat /etc/profile | awk 'END{print $0}' | grep "="
  119. if [ $? -eq 1 ];then
  120. echo 'export PATH=$PATH:'$INSTALL_DIR'/nginx/sbin:$PATH:/etc/init.d/' >> /etc/profile
  121. else
  122. echo ':'$INSTALL_DIR'/nginx/sbin:$PATH:/etc/init.d/' >> /etc/profile
  123. fi
  124. source /etc/profile
  125. echo "Nginx配置完毕"
  126. }
  127. function install_php() {
  128. cd $TMP_DIR
  129. yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
  130. libcurl-devel libjpeg-devel libpng-devel openssl-devel \
  131. libmcrypt-devel libxslt-devel libtidy-devel
  132. wget http://docs.php.net/distributions/php-${PHP_V}.tar.gz
  133. tar zxf php-${PHP_V}.tar.gz
  134. cd php-${PHP_V}
  135. ./configure --prefix=$INSTALL_DIR/php \
  136. --exec-prefix=$INSTALL_DIR/php \
  137. --with-mysqli --with-pdo-mysql \
  138. --with-gd --bindir=$INSTALL_DIR/php/bin \
  139. --sbindir=$INSTALL_DIR/php/sbin \
  140. --includedir=$INSTALL_DIR/php/include \
  141. --libdir=$INSTALL_DIR/php/lib/php \
  142. --mandir=$INSTALL_DIR/php/php/man \
  143. --with-config-file-path=$INSTALL_DIR/php/etc \
  144. --with-openssl --enable-mbstring --enable-fpm
  145. command_status_check "PHP - 平台环境检查失败!"
  146. make && make install
  147. command_status_check "PHP - 安装失败!"
  148. cp php.ini-production $INSTALL_DIR/php/etc/php.ini
  149. cp $INSTALL_DIR/php/etc/php-fpm.conf.default $INSTALL_DIR/php/etc/php-fpm.conf
  150. cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  151. chmod +x /etc/init.d/php-fpm
  152. /etc/init.d/php-fpm start
  153. command_status_check "PHP - 启动失败!"
  154. cat /etc/profile | awk 'END{print $0}' | grep "="
  155. if [ $? -eq 1 ];then
  156. echo 'export PATH=$PATH:'$INSTALL_DIR'/php/bin:$PATH' >> /etc/profile
  157. else
  158. echo ':'$INSTALL_DIR'/php/bin:$PATH' >> /etc/profile
  159. fi
  160. echo "source /etc/profile" >> ~/.bashrc
  161. source /etc/profile
  162. command_status_check "PHP - 环境变量刷新失败!"
  163. echo "PHP配置完毕"
  164. }
  165. read -p "请输入编号:" number
  166. case $number in
  167. 1)
  168. install_nginx;;
  169. 2)
  170. install_php;;
  171. 3)
  172. install_mysql;;
  173. 4)
  174. install_php
  175. install_nginx
  176. ;;
  177. 9)
  178. exit;;
  179. esac

6、批量解压 使用for循环实现

  1. for i in `ls`;do tar -zxvf $i;done

7、监控2台服务器硬盘利用率脚本实战

  1. IP="127.0.0.1"
  2. TMP_FILE=/tmp/disk.tmp
  3. df -h > $TMP_FILE
  4. USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
  5. for USE_RATE in $USE_RATE_LIST; do
  6. PART_NAME=${USE_RATE%=*}
  7. USE_RATE=${USE_RATE#*=}
  8. echo "$IP $PART_NAME Partition usage $USE_RATE%!"
  9. done
  10. HOST_INFO=host.info
  11. for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
  12. USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
  13. PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
  14. TMP_FILE=/tmp/disk.tmp
  15. ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
  16. USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
  17. for USE_RATE in $USE_RATE_LIST; do
  18. PART_NAME=${USE_RATE%=*}
  19. USE_RATE=${USE_RATE#*=}
  20. echo "$IP $PART_NAME Partition usage $USE_RATE%!"
  21. #if [ $USE_RATE -ge 80 ]; then
  22. # echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
  23. #fi
  24. done
  25. done

8、批量检查 5个网站域名是否正常

  1. #!/bin/bash
  2. url_list="www.baidu.com jd.com cao.com qq.com taobao.com"
  3. for url in $url_list;do
  4. num=0
  5. for ((i=1;i<=3;i++));do
  6. http_code=$(curl -I -o /dev/null --connect-timeout 3 -s -w %{http_code} $url)
  7. if [ $http_code -eq 200 ];then
  8. echo "$url is ok"
  9. break
  10. else
  11. echo "$url full is $num"
  12. let num++
  13. fi
  14. done
  15. if [ $num -eq 3 ];then
  16. echo "$url is loser"
  17. fi
  18. done

9、统计磁盘、内存使用率,使用率大于%5 就打印mail 小于则正常

  1. DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
  2. function check_disk(){
  3. for I in $DEV;do
  4. dev=`df -Ph | grep $I | awk '{print $1}'`
  5. size=`df -Ph | grep $I | awk '{print $2}'`
  6. used=`df -Ph | grep $I | awk '{print $3}'`
  7. free=`df -Ph | grep $I | awk '{print $4}'`
  8. rate=`df -Ph | grep $I | awk '{print $5}'`
  9. mount=`df -Ph | grep $I | awk '{print $6}'`
  10. echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
  11. F=`echo $rate | awk -F% '{print $1}'`
  12. if [ $F -ge 5 ];then
  13. echo "$mount mail"
  14. else
  15. echo "It's OK"
  16. fi
  17. done
  18. }
  19. function check_ram(){
  20. DATE=$(date +%F" "%H:%M)
  21. MAIL="example@mail.com"
  22. #TOTAL 内存总的大小
  23. TOTAL=$(free -m |awk '/Mem/{print $2}')
  24. #USE 使用的物理内存大小(used - buff-cache)
  25. USE=$(free -m |awk '/Mem/{print $3-$6}')
  26. #FREE 内存剩余量
  27. FREE=$(($TOTAL-$USE))
  28. if [ $USE -lt 0 ];then
  29. echo " Date: $DATE
  30. Total=$TOTAL,
  31. Use=$USE,
  32. Free=$FREE "
  33. elif [ $FREE -lt 1024 ]; then
  34.    echo " Date: $DATE
  35. Total=$TOTAL,
  36. Use=$USE,
  37. Free=$FREE "
  38. fi
  39. }
  40. echo "--------------------check_disk--------------------"
  41. check_disk;
  42. echo "--------------------check_ram--------------------"
  43. check_ram;
  44. echo "--------------------End--------------------"

10、批量检查ip 是否在线是否能ping 通 使用for循环实现

  1. #!/bin/bash
  2. ip_list="192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5"
  3. for i in $ip_list;do
  4. for ((n=1;n<=3;n++));do
  5. ping -c 1 $i &> /dev/null
  6. if [ $? -eq 0 ];then
  7. echo "$i is success"
  8. break
  9. else
  10. echo "$i louser $n"
  11. fi
  12. done
  13. done

11、批量管理redis脚本

  1. #!/bin/bash
  2. USAG(){
  3. echo "sh $0 {start|stop|restart|login|ps|tail} PORT"
  4. }
  5. if [ "$#" = 1 ]
  6. then
  7. REDIS_PORT='6379'
  8. elif
  9. [ "$#" = 2 -a -z "$(echo "$2"|sed 's#[0-9]##g')" ]
  10. then
  11. REDIS_PORT="$2"
  12. else
  13. USAG
  14. exit 0
  15. fi
  16. REDIS_IP=$(hostname -I|awk '{print $1}')
  17. PATH_DIR=/opt/redis_cluster/redis_${REDIS_PORT}/
  18. PATH_CONF=/opt/redis_cluster/redis_${REDIS_PORT}/conf/redis_${REDIS_PORT}.conf
  19. PATH_LOG=/opt/redis_cluster/redis_${REDIS_PORT}/logs/redis_${REDIS_PORT}.log
  20. CMD_START(){
  21. redis-server ${PATH_CONF}
  22. }
  23. CMD_SHUTDOWN(){
  24. redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} shutdown
  25. }
  26. CMD_LOGIN(){
  27. redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT}
  28. }
  29. CMD_PS(){
  30. ps -ef|grep redis
  31. }
  32. CMD_TAIL(){
  33. tail -f ${PATH_LOG}
  34. }
  35. case $1 in
  36. start)
  37. CMD_START
  38. CMD_PS
  39. ;;
  40. stop)
  41. CMD_SHUTDOWN
  42. CMD_PS
  43. ;;
  44. restart)
  45. CMD_START
  46. CMD_SHUTDOWN
  47. CMD_PS
  48. ;;
  49. login)
  50. CMD_LOGIN
  51. ;;
  52. ps)
  53. CMD_PS
  54. ;;
  55. tail)
  56. CMD_TAIL
  57. ;;
  58. *)
  59. USAG
  60. esac

12、快速回滚nginx业务

  1. #!/bin/bash
  2. cd /usr/local/nginx
  3. ls | grep "^web"
  4. read -p "请输入你要回滚的目录版本:" ver
  5. rm -rf html
  6. ln -s $ver html