shell 基础强化笔记 ( 二)

  |   0 评论   |   0 浏览

1.杂谈

  shift+> 缩进,v 进入选择模式

  seq 命令用于产生从某个数到另外一个数之间的所有整数。

2.批量 ping 存活主机

 1 #!/bin/bash  
 2 for i in {1..254}  
 3 do  
 4  {  
 5  ip=120.255.0.$i  
 6  ping -c 1 -W 1 $ip &>/dev/null  
 7  if [ $? -eq 0 ];then  
 8  echo "$ip is ok"  
 9  else  
10  echo "$ip is down"  
11  fi  
12  }&  
13 done  
14 wait  
15 echo "ping is done"

3.批量判断是否 22 端口开启

 1 #!/bin/bash  
 2 >/tmp/activIp.log  
 3 for i in {1..254}  
 4 do  
 5  {  
 6  ip=120.255.0.$i  
 7  ping -c 1 -W 1 $ip &>/dev/null  
 8  if [ $? -eq 0 ];then  
 9  echo "$i. $ip is ok">>/tmp/activIp.log  
10  else  
11  echo "$i. $ip is down"  
12  fi  
13  }&  
14 done  
1516 wait  
17 echo "Scan ping is done"  
18 echo "Scan port starting ..."  
1920 IFS=$'\n'  
21 for i in $(cat /tmp/activIp.log)  
22 do  
23  ip=$(echo $i |awk '{print $2}')  
24  echo $ip  
25  nmap $ip |grep "22" &>/dev/null  
2627  if [ $? -eq 0 ];then  
28  echo "$ip Port 22 is open!"  
29  else  
30  echo "$ip Port 22 is down!"  
31  fi  
32 done

4. MySQL 主从监测恢复脚本

  其中 for 循环中的{}&表示并发编程的意思

 1 #!/bin/bash  
 2 IO_Status=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Slave_IO_Running:/ {print $2}'|sed 's/\r//')  
 3 SQL_Status=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Slave_SQL_Running:/ {print $2}'|sed 's/\r//')  
 4 5 if [ "$IO_Status" == "Yes" ] && [ "$SQL_Status" == "Yes" ];then  
 6  echo "数据库主从同步正常!"  
 7 else  
 8  echo "数据库主从同步失败!"  
 9  if [ ! "$IO_Status" == "Yes" ];then  
10  IO_Err=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Last_IO/')  
11  echo -e "\033[31m Mysql slave 链接异常 \033[0m \n $IO_Err"  
12  fi  
13  if [ ! "$SQL_Status" == "Yes" ];then  
14  IO_Err=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Last_SQL_Errno:/ {print $2}'|sed 's/\r//')  
15  echo -e "\033[31m Mysql slave Sql异常 \033[0m \n $IO_Err"  
16  case $IO_Err in  
17  1007)  
18  sleep 2;  
19  docker exec -it msmysql mysql -u root -p123456 -e "stop slave;set global sql_slave_skip_counter=1;start slave;"  
20  IO_Err=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Last_SQL_Errno:/ {print $2}'|sed 's/\r//')  
21  if [ $IO_Err -eq 0 ];then  
22  echo "尝试跳过一次,主从恢复正常"  
23  else  
24  IO_Err2=$(docker exec -it msmysql mysql -u root -p123456 -e "show slave status\G"|awk '/Last_SQL_Err/')  
25  echo "$IO_Err2"  
26  echo "发邮件!"  
27  fi  
28  ;;  
29  1008)  
30  echo "发邮件!"  
31  ;;  
32  *)  
33  echo "发邮件!"  
34  esac  
35  fi  
36 fi

  快速搭建 MySQL 主从测试环境参考:
https://www.cnblogs.com/songwenjie/p/9371422.html

---------------------------------------------------------------
>> 博客地址:https://blog.mufengs.com
>> 邮箱地址:[email protected]
>> 微信帐号:Do8080
>> Github : https://github.com/mufengcoding
---------------------------------------------------------------