1. 批量生成随机字符串文件名
#!/bin/bash path=/root/dirs [ -d "$path" ] || mkdir -p $path for i in `seq 10` do random=$(openssl rand -base64 40|sed 's/[^a-z]//g'|cut -c 2-11) touch $path/${random}_centos.html done
2. 批量改名
#!/bin/bash rename centos.html linux.html *.html
3. 批量创建特殊要求的用户
#!/bin/bash . /etc/init.d/functions user="user" passfile="/tmp/user-passwd.log" for num in `seq -w 1 05` do pass="`echo "test$RANDOM"|md5sum|cut -c3-11`" useradd $user$num &> /dev/null && echo "$pass"|passwd --stdin $user$num &> /dev/null && echo -e "user:$user$num\tpasswd:$pass" >> $passfile if [ $? -eq 0 ];then action "$user$num is ok." /bin/true else action "$user$num is fail." /bin/false fi done echo -e "\e[1;32m ------ userlist ----- \e[0m" cat $passfile && >$passfile
4. 扫描网络内存活的主机
#!/bin/bash CMD="ping -W 2 -c 2" ip="192.168.159." for i in $(seq 254) do { $CMD $ip$i &> /dev/null if [ $? -eq 0 ];then echo "$ip$i is ok." fi }& done
5. 解决DOS攻击
#!/bin/bash # 方法1:日志按小时切割,每小时IP的PV高于500的,封掉! file=$1 while true do awk '{print $1}' $1|grep -v "^$"|sort |uniq -c > /tmp/tmp.log exec < /tmp/tmp/log while read line do ip=`echo $line|awk '{print $2}'` count=`echo $4line|awk '{print $1}'` if [ $count -gt 500 ] && [ `iptables -L -n|grep "$ip" |wc -l` -lt 1 ] then iptables -I INPUT -s $ip -j DROP echo "$line is dropped." >> /tmp/droplist_$(date +%F).log fi done sleep 3600 done # 方法2:分析系统的网络连接数,每3分钟执行一次! file=$1 JudgeExt(){ if expr "$1" : ".*\.log" &> /dev/null then : else echo $"Usage:$0 xxx.log" exit 1 fi } IpCount(){ grep "ESTABLISHED" $1| awk -F "[ :]+" '{ ++S[$(NF-3)]}END {for(key in S print S[key], key)}'|sort -rn -k1|head -5 > /tmp/tmp.log } ipt(){ local ip=$1 if [ `iptables -L -n|grep "$ip"|wc -l` -lt 1 ] then iptables -I INPUT -s $ip -j DROP echo "$line is dropped." >> /tmp/droplist_$(date +%F).log fi } main(){ JudgeExt $file while true do IpCount $file while read line do ip=`echo $line|awk '{print $2}'` count=`echo $line|awk '{print $1}'` if [ $count -gt 3 ];then ipt $ip fi done</tmp/tmp.log sleep 180 done } main
6. MySQL数据库分库备份
#!/bin/bash PATH="$PATH:/usr/local/mysql" DBPATH=/tmp/mysql_backup MYUSER=root MYPASS=123456 SOCKET=/tmp/mysql.sock MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYDUMP="mysqldump -u$MYUSER -pMYPASS -S SOCKET" [ ! -d "$DBPATH" ] && mkdir $DBPATH for dbname in `$MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|schema"` do $MYDUMP $dbname|gzip > $DBPATH/${dbname}_$(date +%F).sql.gz done
7. MySQL数据库分库分表备份
#!/bin/bash PATH="$PATH://usr/local/mysql" DBPATH=/tmp/mysql_backup MYUSER=root MYPASS=123456 SOCKET=/tmp/mysql.sock MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET" [ ! -d $DBPATH ] && mkdir $DBPATH for dbname in `$MYCMD -e "show databses;"|sed '1,2d'|egrep -v "mysql|schema"` do mkdir -p $DBPATH/${dbname}_$(date +%F) for table in `$MYCMD -e "show tables from $dbname;"|sed '1d'` do $MYDUMP $dbname $table|gzip > $DBPATH/${dbname}_$(date +%F)/${dbname}_${tables}.sql.gz done done
8. 筛选符合长度的单词
#!/bin/bash chars="I am a handsome boy, learn to linux." echo $chars|awk '{for(i=1;i<=NF;i++) if(length($i)<=4) print $i}'
9. MySQL主从复制异常监控
#!/bin/bash path=/usr/local/sbin MAIL_GROUP="123@qq.com 456@163.qq.com" PHONE_GROUP="10086 10010" LOG_FILE="/tmp/mysql_check.log" USER=root PASSWD=123456 PORT=3306 MYSQLCMD="mysql -u$USER -p$PASSWD -S /tmp/mysql.sock" error=(1008 1007 1062) RETVAL=0 [ ! -d $path ] && mkdir -p $path JudgeError(){ for((i=0;i<${#error[*]};i++)) do if [ "$1" == "${error[$i]}" ] then echo "MySQL slave error is $1, auto repairing it." $MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;" fi done return $1 } CheckDB(){ status=($(awk -f ':' '/_Running|Last_Error|_Behind/{print $NF}' slave.log)) expr ${status[3]} + 1 &> /dev/null if [ $? -ne 0];then status[3]=300 fi if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a ${stauts[3]} -lt 120] then echo "Mysql slave is ok!" return 0 else echo "Mysql slave is failed." fi } MAIL(){ local SUBJECT_CONTENT=$1 for MAIL_USER in `echo $MAIL_GROUP` do mail -s "$SUBJECT_CONTENT" $MAIL_USER < $LOG_FILE done } PHONE(){ for PHONE_USER in `echo $PHONE_GROUP` do TITLE=$1 CONTACT=$PHONE_USER send_message # 发送信息的方法自行添加! done } SendMsg(){ if [ $1 -ne 1 ];then RETVAL=1 NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"` SUBJECT_CONTENT="mysql slave is error, error is $2, ${NOW_TIME}." echo -e "$SUBJECT_CONTENT"|tee $LOG_FILE MAIL $SUBJECT_CONTENT PHONE $SUBJECT_CONTENT $NOW_TIME else echo "Mysql slave status is ok." RETVAL=0 fi return $RETVAL } main(){ while true do CheckDB SendMsg $? sleep 30 done } main
10. 比较整数大小
#!/bin/bash read -p "Please input two number: " a b [ -z "$a" ] || [ -z "$b" ] && { echo "Please input two number again." exit 1 } expr $a + 1 &> /dev/null RETVAL1=$? expr $b + 1 &> /dev/null RETVAL2=$? test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ||{ echo "Please input two num again." exit 2 } [ $a -lt $b ]&& { echo "$a < $b" exit 0 } [ $a -eq $b ] &&{ echo "$a = $b" exit 0 } [ $a -gt $b ] && { echo "$a > $b" exit 0 }