1.能够设置检查服务器的台数 和 要检查哪台服务器,把不在线主机的ip地址和对应不在线的时间,并统计不在线主机的台数保存的到文件里。然后把文件内容显示到屏幕上。
1 #! /bin/bash 2 read -p "请输入检测台数:" x 3 s=0 4 b=0 5 for ((i=1;i<=x;i++)) 6 do 7 read -p "请输入要检测的主机地址,输入后请稍后:" ip[$i] 8 9 ping -c 1 192.168.1.${ip[$i]} &> /dev/null 10 if [ $? -ne 0 ] 11 then 12 date >> /root/shell/offline.txt 13 echo "ip:192.168.1.${ip[$i]} is offline" >>/root/shell/offline.txt 14 let s++ 15 else echo "192.168.1.${ip[$i]} is online" 16 fi 17 done 18 19 echo "本次不在线数:" $s >>/root/shell/offline.txt 20 tail -10 /root/shell/offline.txt
2.给源码http服务编写启动脚本
1 #! /bin/bash 2 # chkconfig: 2345 75 25 3 # description: apache http 4 # processname: httpd 5 path="/usr/local/httpd/bin/apachectl" 6 case $1 in 7 start) 8 $path start &> /dev/null 9 sleep 1 10 echo -n "apache is start..ok";; 11 stop) 12 $path stop 13 sleep 2 14 echo -n "apache is stop..ok";; 15 restart) 16 $path stop 17 echo -n "apache is stop..ok" 18 sleep 3 19 $path start 20 echo -n "apache is start..ok";; 21 status) 22 netstat -anptu | grep httpd$ 23 if [ $? -eq 0 ];then 24 echo "apache is runing" 25 else echo "apache is not run" 26 fi;; 27 *) 28 echo "apache {start/stop/restart/status}" 29 esac 30 echo
3.当系统根分区的可以空间低于15G时,向系统所有终端发送警告信息“根分区磁盘空间不足”
1 #! /bin/bash 2 i=`df -h|grep /$|awk ‘{print $4}‘|awk -F "G" ‘{print $1}‘` 3 if [ $i -lt 15 ];then 4 wall "根分区可用空间为 $i G,空间不足" 5 fi
4.添加用户时 ,用户可以设置添加用户的个数,用户名前缀、用户的密码,用户的有效期。
1 #! /bin/bash 2 read -p "请输入用户个数" count 3 read -p "请输入用户名前缀" user 4 read -p "用户名密码" passwd 5 read -p "有效期" shi 6 s=85 7 for((i=$s;i<=$s+$count-1;i++)) 8 do 9 if [ $i -lt 10 ];then 10 useradd "$user"0$i 11 echo "$passwd" | passwd --stdin "$user"0$i &>/dev/null 12 chage -d 0 "$user"0$i 13 chage -M $shi "$user"0$i 14 else 15 useradd $user$i 16 echo "$passwd" | passwd --stdin $user$i &>/dev/null 17 chage -d 0 $user$i 18 chage -M $shi $user$i 19 fi 20 done
5.在mysql主从同步中 slave服务器上运行脚本,监控 IO 进程 和 SQL 进程的状态 ,当有一个进程的状态是NO时,向终端发送警告信息
1 #! /bin/bash 2 a=`mysql -uroot -p123456 -e "show slave status\G"|grep -iE "*io_runing"|awk ‘{print $2}‘` 3 b=`mysql -uroot -p123456 -e "show slave status\G"|grep -iE "*sql_runing"|awk ‘{print $2}‘` 4 while : 5 do 6 if [ $a = "NO" -o $b = "NO" ];then 7 wall "mysql runing_status is NO" 8 fi 9 done
6.实用正则表达
mac:([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2} ip:([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]\.){3}[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
实用shell编写案例
时间: 2024-10-12 21:22:45