获取内网 IP 地址

Shell脚本中获取本机ip地址的3个方法

  1. # 获取内网 IP 地址
  2. function getInnerNet(){
  3. # /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
  4. innerIpList=`ip addr |grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" | awk -F '/' '{print $1}'`
  5. echo "内网 IP 地址(集合):$innerIpList"
  6. for ip in $innerIpList
  7. do
  8. echo "内网 IP 地址:$ip"
  9. done
  10. }
  11. # getInnerNet

获取外网地址(公网地址)

  1. # 获取外网 IP 地址
  2. function getOuterNet(){
  3. outIp=`curl members.3322.org/dyndns/getip`
  4. echo "外网 IP 地址为:$outIp"
  5. }
  6. # getOuterNet

Redis 开机启动

安全删除文件 rm -rf

  1. # 执行 rm 操作时会有二次确认提示,避免手抖造成重大操作事故。
  2. vim ~/.bashrc
  3. alias rm='read -p "请确认是否彻底删除?[输入 'y'/'Y' 进行确认,或者输入 'n'/'N' 取消。]" y && [ $y == "y" ] && rm -i'

保存退出后,在终端执行 source ~/.bashrc 使上面的修改生效。如仍未生效,重新连接服务即可。

登录执行 MySQL 文件/语句(密码免输入/免登录)

  1. function executeSql(){
  2. # 定义变量,用来保存数据库名
  3. dbName=DB_NEW
  4. # 注意事项,如果变量值是以特殊字符开头的,需要用"单引号"将其包起(见下),若传递过来的以特殊字符开头的变量,则不用做特殊处理。
  5. dbPwd='123456' # 使用'单引号' 括起来是为了避免有特殊字符引起错误。
  6. dbUser=$1
  7. sqlFilePath=$2
  8. mysql -u $dbUser -p$dbPwd --connect-expired-password <<EOF
  9. use $dbName;
  10. source ${sqlFilePath}.sql;
  11. exit
  12. EOF
  13. }
  14. # executeSql


复制当前服务器文件到远端服务器(密码免输入)

涉及知识点:sshpass

  1. # 远端服务器的 ip
  2. remoteIp=
  3. # 远端服务器的端口
  4. remotePort=
  5. # 远端服务器的密码
  6. remotePwd=
  7. # 远端服务器保存文件的路径
  8. remoteDirPath=
  9. # 本服务器需要复制的文件路径
  10. copyFilePath=
  11. # 安装 sshpass 命令
  12. yum -y install sshpass
  13. #sshpass -p 远端服务器密码 scp -o StrictHostKeyChecking=no -P 远端服务器端口 -r 本地要拷贝的路径 root@远端服务器IP:远端服务器要保存的路径
  14. sshpass -p ${remotePwd} scp -o StrictHostKeyChecking=no -P ${remotePort} -r ${copyFilePath} root@${remoteIp}:${remoteDirPath}

复制远端服务器文件到当前服务器(密码免输入)

  1. #sshpass -p 远端服务器密码 scp -o StrictHostKeyChecking=no -P 当前服务器的端口 -r 远端服务器的用户名@远端服务器IP:远端服务器要拷贝的文件路径 当前服务器要保存的路径
  2. sshpass -p ${remotePwd} scp -o StrictHostKeyChecking=no -P ${localPort} -r ${remoteUser}@${remoteIp}:${remoteDirPath}/* ${localDirPath}/

启动 war 包

  1. # 如果存在当前服务文件夹,则启动当前服务
  2. function startWarServer() {
  3. # 服务名称(new)
  4. proServer=$1
  5. if [ ! -d "${newBasePath}/${proServer}" ];then
  6. echo "没有该服务:[[ $proServer ]],无需启动" >> $logPath
  7. else
  8. # echo "有该文件,$proServer ..."
  9. echo "$proServer 判断执行,... " >> $logPath
  10. numServer=`ps -aux | grep ${newBasePath}/${proServer}/bin | grep -v "grep" | wc -l`
  11. if [[ ${numServer} -lt 1 ]];then
  12. echo "${proServer} `date "+%Y%m%d %T"` 没有启动 正在启动" >> $logPath
  13. cd ${newBasePath}/${proServer}
  14. ${newBasePath}/${proServer}/bin/startup.sh
  15. cd -
  16. else
  17. echo "${proServer} `date "+%Y%m%d %T"` 已启动" >> $logPath
  18. fi
  19. echo "$proServer 判断执行,end ... " >> $logPath
  20. fi
  21. }
  22. # startWarServer 服务名
  23. startWarServer ${pro_ftpServer}

启动 jar 包(限制内存启动)

  1. # 调用 jar 包服务(限制内存启动)
  2. JAVA_OPTS="-Xss512k -XX:MaxMetaspaceSize=1024M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=75 -XX:ParallelGCThreads=4 -XX:+HeapDumpOnOutOfMemoryError";
  3. function startJarServer() {
  4. # 服务名称(new)
  5. proServer=$1
  6. # jar 包名称
  7. jarName=$2
  8. # 限制内存大小
  9. limitXmx=$3
  10. serverPath=${newBasePath}/${proServer}
  11. echo "proServer=$1,jarName=$2,limitXmx=$3,serverPath=${newBasePath}/${proServer}" >> $logPath
  12. if [ ! -d "${newBasePath}/${proServer}" ];then
  13. echo "没有该服务:[[ $proServer ]],无需启动" >> $logPath
  14. else
  15. #num_MessageServer=`ps -aux | grep MessageServer.jar | grep -v "grep" | wc -l`
  16. numServer=`ps -aux | grep ${newBasePath}/${proServer}/${jarName}.jar | grep -v "grep" | wc -l`
  17. if [[ ${numServer} -lt 1 ]];then
  18. echo "`date "+%Y%m%d %T"` [[[ ${proServer} ]]] 未启动,正在启动" >> $logPath
  19. cd ${newBasePath}/${proServer}
  20. nohup java -server -Xms512m -Xmx${limitXmx}G $JAVA_OPTS -XX:HeapDumpPath=/logs/${proServer}.dump -jar ${newBasePath}/${proServer}/${jarName}.jar >/dev/null 2>&1 &
  21. cd -
  22. else
  23. echo "`date "+%Y%m%d %T"` [[ ${proServer} ]] 已启动" >> $logPath
  24. fi
  25. echo "execute_$proServer ,end ... " >> $logPath
  26. fi
  27. }
  28. # startJarServer 服务名 jar 包名 占用的最大内存
  29. startJarServer ${pro_httpServer} ${httpServer_jarName} 1

清理 catalina.out 日志

  1. # 检索所有的 catalina.out 日志文件并清理
  2. function cleanLog(){
  3. logFileList=`find / -name catalina.out`
  4. for logFile in $logFileList
  5. do
  6. echo "清理的文件为:$logFile,大小是:`du -sh $logFile`"
  7. echo "" > $logFile
  8. done
  9. }
  10. # 调用
  11. cleanLog

启动 redis

  1. # 启动 redis
  2. function start_redis(){
  3. echo "function start_redis 开始执行 ... " >> $logPath
  4. # path=${oldBasePath}/redis
  5. path=${newBasePath}/redis
  6. redis_file=${path}/redis-server
  7. redis_conf=${path}/redis.conf
  8. #判断对应 redis 进程是否已经启动
  9. if `ps -ef | grep ${path} | grep -q redis-server`; then
  10. echo "Redis state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep redis-server`"
  11. echo "redis 已经启动 ... " >> $logPath
  12. return
  13. fi
  14. _echo "--------------------------------------------------------"
  15. _echo "-------------starting Redis Server ------------"
  16. echo "redis 启动,开始 ... " >> $logPath
  17. cd ${path}
  18. _echo "changed to dir: `pwd`"
  19. echo "${redis_file} ${redis_conf} &"
  20. ${redis_file} ${redis_conf} &
  21. echo "redis 启动,完成 ... " >> $logPath
  22. cd -
  23. echo "start_redis 开始结束。 "
  24. }

启动 kafka

  1. function start_kafka_zookeeper(){
  2. #path=${oldBasePath}/kafka
  3. path=${newBasePath}/kafka
  4. zookeeper_start_file=${path}/bin/zookeeper-server-start.sh
  5. zookeeper_stop_file=${path}/bin/zookeeper-server-stop.sh
  6. zookeeper_cfg_file=${path}/config/zookeeper.properties
  7. #判断对应Kafka zookeeper进程是否已经启动
  8. # ps -ef | grep -v "grep"
  9. # ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka
  10. # ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka | grep -q "zookeeper.properties"
  11. # if `ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka | grep -q "zookeeper.properties"`; then
  12. if `ps -ef | grep -v "grep" | grep ${path} | grep -q "zookeeper.properties"`; then
  13. echo "zookeeper state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "zookeeper.properties" | cut -f-20 -d" "`"
  14. return
  15. fi
  16. echo "--------------------------------------------------------"
  17. echo "-------------Starting Kafka Zookeeper------------"
  18. echo "启动 zookeeper 开始,... " >> $logPath
  19. cd ${path}
  20. nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
  21. echo "启动 zookeeper 完成。。。" >> $logPath
  22. cd -
  23. sleep 10
  24. }
  25. function start_kafka(){
  26. #path=${oldBasePath}/kafka
  27. path=${newBasePath}/kafka
  28. Kafka_start_file=${path}/bin/kafka-server-start.sh
  29. Kafka_stop_file=${path}/bin/kafka-server-stop.sh
  30. Kafka_cfg_file=${path}/config/server.properties
  31. #判断对应 kafka Server 进程是否已经启动
  32. if `ps -ef | grep ${path} | grep -q "kafka.Kafka"`; then
  33. echo "Kafka state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "kafka.Kafka"| cut -f-20 -d" "`"
  34. return
  35. fi
  36. echo "--------------------------------------------------------"
  37. echo "-------------starting Kafka Server ------------"
  38. echo "启动 Kafka 开始,... " >> $logPath
  39. cd ${path}
  40. nohup bin/kafka-server-start.sh config/server.properties &
  41. echo "启动 Kafka 完成 ... " >> $logPath
  42. cd -
  43. }

卸载 MySQL

  1. #!/bin/bash
  2. function removerMySQL(){
  3. count=$(rpm -qa | grep mysql)
  4. for x in $count
  5. do
  6. rpm -e $x --nodeps
  7. done
  8. count1=$(find / -name mysql)
  9. for i in $count1
  10. do
  11. rm -rf $i
  12. done
  13. rm -rf /etc/my.cnf
  14. rm -rf /var/log/mysqld.log
  15. rm -rf /etc/my.cnf.rpmsave
  16. rpm -qa|grep mysql
  17. }
  18. # 调用
  19. removerMySQL

安装 MySQL

卸载和安装 MySQL

  1. #!/bin/bash
  2. # 此脚本未验证,方法可以参考
  3. function verb(){
  4. echo "
  5. ############################################################################################
  6. # ###### ###### # # ######### ########## # #
  7. # # # # # # # # # # # #
  8. # # # # # # # # # # # #
  9. # # # # # ########## ######### ########## # #
  10. # # # # # # # # #
  11. # # # # # # # #
  12. # # # # ######### # ######### #
  13. ############################################################################################
  14. "
  15. echo "
  16. ############################################################################################
  17. # # # ########## ########## # ######### #
  18. # # # # # # # # # #
  19. # # # # # # # # # #
  20. # # # ########## # # # ######### #
  21. # # # # # # # # # #
  22. # # # # # # # # # #
  23. # ## ########## ########## # ######### #
  24. ############################################################################################
  25. "
  26. }
  27. # 执行方法介绍
  28. function explanation(){
  29. echo "
  30. ############################################################################################
  31. 脚本说明:脚本进行可进行传参执行
  32. 1---install_mysql //安装mysql8.0.15
  33. 2---remove_mysql //卸载当前服务器的mysql,一定要谨慎使用
  34. ############################################################################################
  35. "
  36. }
  37. # 卸载mysql方法
  38. function remove_mysql(){
  39. count_mysql=`ps -ef|grep -e "mysql\|mysqld"|grep -v grep|wc -l`
  40. if [ $count_mysql -gt 1 ];then
  41. echo "本服务器上已经安装运行着mysql,是否需要卸载,一定要认真考虑!!!!!!"
  42. read -p "请问是否需要删除现在运行的mysql[yes|no]:" num
  43. if [ $num = 'yes'];then
  44. systemctl stop mysqld
  45. service mysql stop
  46. count_install_mysql=$(rpm -qa|grep -e "mysql\|mysqld")
  47. count_install_maindb=$(rpm -qa|grep "maindb")
  48. mysql_file=$(find / -name mysql*)
  49. for a in count_install_mysql
  50. do
  51. rpm -a $i --nodeps
  52. done
  53. for b in count_install_maindb
  54. do
  55. rpm -e $b --nodeps
  56. done
  57. for c in mysql_file
  58. do
  59. rm -rf $c
  60. done
  61. echo "卸载数据库完成"
  62. fi
  63. fi
  64. }
  65. # 安装mysql方法
  66. function install_mysql(){
  67. msg=`df -k | awk '{print $6}'` ; for i in /opt/* ; do echo $msg | grep $i 1>/dev/null || du -sk $i ; done | sort -n
  68. file="$msg/opt"
  69. file1="$file/software"
  70. file2="/tmp/mysql.log"
  71. file3="/etc/my.cnf"
  72. hostname=`hostname`
  73. if [ ! -f $file];then
  74. mkdir -p $file
  75. if [ ! -f $file1 ];then
  76. mkdir -p $file1
  77. if [ ! -f $file2 ];then
  78. touch $file2
  79. chmod 666 $file2
  80. fi
  81. fi
  82. cd $file
  83. wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.15-el7-x86_64.tar.gz
  84. tar -xvf *.tar.gz
  85. mv *.tar.gz $file1
  86. mv mysql* mysql
  87. groupadd mysql
  88. useradd -r -g mysql mysql
  89. cd mysql
  90. chown -R mysql:mysql ./
  91. bin/mysql --initialize --user=mysql --basedir=$file --datadir=$file/datadir > $file2
  92. str=$(cat $file2)
  93. mysql_passwd=${str##*root@localhost:}
  94. # 当/etc/my.cnf不存在的时候需要手动创建对应的my-defalut.cnf文件并重命名
  95. if [ ! -f $file3 ];then
  96. touch my-defalut.cnf
  97. chmod 755 my-defalut.cnf
  98. cp my-defalut.cnf $file3
  99. fi
  100. # 修改对应的mysql配置文件
  101. echo "
  102. [mysqld]
  103. basedir=$file
  104. datadir=$file/data
  105. port=3306
  106. socket=/tmp/mysql.sock
  107. pid-file=$file/$hostname.pid
  108. sql_mode=NO_ENGINE_SUBSTITUTION.STRICT_TRANS_TABLES" >> file3
  109. # mysql启动脚本中修改对应的安装路径和对应的数据存储路径
  110. sed -i "s#basedir=#basedir=$file#g" $file/support-files
  111. sed -i "s#datadir=#datadir=$file/datadir#" $file/support-files
  112. cp $file/support-files/mysql.server /etc/init.d/mysql
  113. chmod +x /etc/init.d/mysql
  114. chkconfig --add mysql
  115. chkconfig --list >> $file2
  116. # 该步骤一定需要核实,服务器重启,SELINUX=enforcing,会造成mysql无法启动
  117. sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
  118. # 临时关闭selinux
  119. setenforce 0
  120. service mysql start
  121. count=$?
  122. if [ $count -eq 0 ];then
  123. echo "mysql数据库启动成功" >> $file3
  124. else
  125. tail -f /var/log/mysqld.log
  126. exit
  127. fi
  128. # 重制mysql root@localhost密码,创建root@%用户
  129. mysql -u root -p $mysql_passwd <<EOF
  130. alter user 'root'@'localhost' identified by '!Fzb_dahua2019';
  131. CREATE USER 'root'@'%' IDENTIFIED BY '!Fzb_dahua2019';
  132. GRANT ALL PRIVILEGES ON DATABASENAME.TABLENAME TO 'root'@'%';
  133. FLUSH PRIVILEGES;
  134. quit;
  135. EOF
  136. }
  137. # 主方法
  138. function main(){
  139. verb
  140. explanation
  141. read -p "输入需要执行操作【 1|2 】:" num1
  142. case $num1 in
  143. 1|1)
  144. echo "欢迎进入安装mysql环节"
  145. install_mysql
  146. break
  147. ;;
  148. 2|2)
  149. echo "欢迎进入卸载mysql环节,该步骤请慎重操作!!"
  150. remove_mysql
  151. break
  152. ;;
  153. *)
  154. echo "输入错误,请按照重新运行script"
  155. break
  156. ;;
  157. esac
  158. }
  159. #
  160. main