1、企业实用脚本1
(生产实用案例)监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
[[email protected] shell]# mysql -uroot -pli123456 -S /data/3308/mysql.sock mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.2.10 Master_User: rep Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 188 Relay_Log_File: relay-bin.000008 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1007 Last_Error: Error ‘Can‘t create database ‘zzz‘; database exists‘ on query. Default database: ‘zzz‘ Query: ‘create database zzz‘ Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 2171 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1007 Last_SQL_Error: Error ‘Can‘t create database ‘zzz‘; database exists‘ on query. Default database: ‘zzz‘. Query: ‘create database zzz‘ Replicate_Ignore_Server_Ids: Master_Server_Id: 2
#!/bin/sh array=( 0 1158 1159 1008 1007 1062) Port=3308 password=li123456 check_slave(){ master=`mysql -uroot -p$password -S /data/$Port/mysql.sock -e "show slave status\G;"|awk -F ‘[ :]+‘ ‘NR==3 {print $3}‘` io=`mysql -uroot -p$password -S /data/$Port/mysql.sock -e "show slave status\G;"|awk -F ‘[ :]+‘ ‘NR==12 {print $3}‘` sql=`mysql -uroot -p$password -S /data/$Port/mysql.sock -e "show slave status\G;"|awk -F ‘[ :]+‘ ‘NR==13 {print $3}‘` errorno=`mysql -uroot -p$password -S /data/$Port/mysql.sock -e "show slave status\G;"|awk -F ‘[ :]+‘ ‘NR==20 {print $3}‘` second=`mysql -uroot -p$password -S /data/$Port/mysql.sock -e "show slave status\G;"|awk -F ‘[ :]+‘ ‘NR==34 {print $3}‘` for((i=0;i<${#array[@]};i++)) do [ "$errorno" = "${array[$i]}" ]&&no=1 done [ $no -ne 1 ]&&echo "$master error:$errorno"&&exit 100 if [ "$io" = "Yes" -a "$sql" = "Yes" -a "$second" = "0" ];then echo "$master slave ok." else echo "$master slave error,please check." fi } main(){ while true do check_slave sleep 30 done } main
企业实用脚本2
使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件
#!/bin/sh [ -f /etc/init.d/functions ]&& . /etc/init.d/functions mkdir -p /oldboy for((i=1;i<=10;i++)) do A=`head -c 500 /dev/urandom | tr -dc a-z | head -c 10` touch /oldboy/${A}_oldboy.html [ $? -eq 0 ]&& action "/oldboy/${A}_oldboy.html" /bin/true done
企业实用脚本3
将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写
方法一:
#!/bin/bash cd /oldboy/ array=(`ls`) for i in ${array[*]} do rename oldboy oldgirl $i rename html HTML $i done
方法二:
#!/bin/bash cd /oldboy/ array=(`ls`) for i in ${array[*]} do echo $i|awk -F ‘[_]‘ ‘{print "mv",$1"_"$2,$1"_oldgirl.HTML"}‘|bash done
方法三:
#!/bin/bash cd /oldboy/ ls>test.txt exec < test.txt while read line do rename oldbirl oldboy $line line=`rename oldbirl oldboy $line` rename HTML html $line done
企业实用脚本4
批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)
方法一
#!/bin/bash for i in `seq -w 10` do echo $i|awk ‘{print "useradd oldboy"$0 " && echo `echo $RANDOM|md5sum|cut -c 1-8`|tee -a pass.txt|passwd --stdin oldboy" $0 }‘|bash done
方法二
#!/bin/bash for i in `seq -w 10` do echo oldboy$i|xargs -n1 useradd && echo oldboy$i":"`echo $RANDOM|md5sum|cut -c 1-8`|xargs -n1|tee -a aa.txt|chpasswd done
企业实用脚本5
写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)
#!/bin/bash [ -f /etc/init.d/functions ]&& . /etc/init.d/functions trap "echo "cancle...";exit" 2 check_ip(){ ping -w 1 -c 1 10.0.0.$i>/dev/null return $? } for i in {1..254} do { action "10.0.0.$i" check_ip }& done
企业实用脚本6
写一个脚本解决DOS攻击生产案例
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP
#!/bin/bash check_ddos(){ netstat -antu|grep ESTABLISHED|awk -F ‘[ :]+‘ ‘{print $6}‘|sort|uniq -c > ESTABLISHED.log [ $? -eq 0 ]&&exec < ESTABLISHED.log||exit 1 while read line do pv=`echo $line|awk ‘{print $1}‘` ip=`echo $line|awk ‘{print $2}‘` if [ $pv -gt 100 ]&&[ `/etc/init.d/iptables -L -n|grep $ip|wc -l` -gt 0 ];then iptables -I INPUT -s $ip -j DROP fi done } main(){ while true do check_ddos sleep 3m done } main
企业实用题7
开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf &
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
要求:用函数,case语句、if语句等实现。
#!/bin/sh [ -f /etc/init.d/functions ]&& . /etc/init.d/functions user=root password=li123456 CMDpath=/application/mysql/bin USAGE(){ echo -e "USAGE:$0 {port:3306|3307|3308...} {start|stop}" exit 1 } function_start_mysql(){ if [ `lsof -i:$1|wc -l` -eq 0 ];then $CMDpath/mysqld_safe --defaults-file=/data/$1/my.cnf & action "mysql $1 start success..." /bin/true else echo "mysql $1 is running..." fi } function_stop_mysql(){ if [ `lsof -i:$1|wc -l` -eq 0 ];then echo "mysql $1 is not running..." else $CMDpath/mysqladmin -u $user -p$password -S /data/$1/mysql.sock shutdown action "mysql $1 stop..." /bin/true fi } case "$2" in start) function_start_mysql $1 $2 ;; stop) function_stop_mysql $1 $2 ;; *) USAGE ;; esac
企业实用题8
如何实现对MySQL数据库进行分库备份,请用脚本实现
#!/bin/sh USER=root PASSWD=li123456 SOCKET=/data/3306/mysql.sock LOGIN="mysql -u$USER -p$PASSWD -S $SOCKET" DUMP="mysqldump -u$USER -p$PASSWD -S $SOCKET" DATABASE=$($LOGIN -e "show databases;"|grep -Ev "_schema|mysql|test"|sed ‘1d‘) for database in $DATABASE do $DUMP $database -R -F --master-data=2 --events|gzip > /server/backup/$database/${database}_${table}_$(date +%F).sql.gz done done
企业实用题9
如何实现对MySQL数据库进行分库加分表备份,请用脚本实现
#!/bin/sh USER=root PASSWD=li123456 SOCKET=/data/3306/mysql.sock LOGIN="mysql -u$USER -p$PASSWD -S $SOCKET" DUMP="mysqldump -u$USER -p$PASSWD -S $SOCKET" DATABASE=$($LOGIN -e "show databases;"|grep -Ev "_schema|mysql|test"|sed ‘1d‘) for database in $DATABASE do TABLE=$($LOGIN -e "use $database;show tables;"|sed ‘1d‘) for table in $TABLE do [ ! -d /server/backup/$database ] && mkdir -p /server/backup/$database $DUMP $database $table|gzip > /server/backup/$database/${database}_${table}_$(date +%F).sql.gz done done
企业实用脚本10
bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维实用脚本)。
I am oldboy teacher welcome to oldboy training class.
方法一:
#!/bin/bash word="I am oldboy teacher welcome to oldboy training class" echo $word|sed ‘s# #\n#g‘> a.log exec < a.log while read line do [ `echo $line|wc -c` -le 7 ]&&echo $line done
企业实用脚本11
开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断
方法一:
#!/bin/bash usage(){ echo "usage:$0 int1 int2" exit 1 } compare(){ [ $1 -gt $2 ]&&echo "$1 > $2" [ $1 -eq $2 ]&&echo "$1 = $2" [ $1 -lt $2 ]&&echo "$1 < $2" } main(){ [ -z "$1" ]&&usage [ -z "$2" ]&&usage expr $1 + $2 + 1 &>/dev/null [ $? -ne 0 ]&&usage compare $1 $2 } main $1 $2
方法二:
#!/bin/bash read -p "Input a:" a read -p "Input b:" b [ -z "$a" ]&&echo "usage:$0 int1 int2"&&exit 1 [ -z "$b" ]&&echo "usage:$0 int1 int2"&&exit 2 expr $a + $b + 1 &>/dev/null [ $? -ne 0 ]&&echo "usage:$0 int1 int2"&&exit 3 [ $a -gt $b ]&&echo "$a > $b" [ $a -eq $b ]&&echo "$a = $b" [ $a -lt $b ]&&echo "$a < $b"
企业实用脚本12
打印选择菜单,一键安装Web服务:
[[email protected]ipts]# sh menu.sh
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
要求:
1、当用户输入1时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本;
2、当用户输入2时,输出“startinstalling lnmp.”然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本;
3、当输入3时,退出当前菜单及脚本;
4、当输入任何其它字符,给出提示“Input error”后退出脚本。
5、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。
#!/bin/bash menu(){ cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit] END } menu file1=/server/scripts/lamp.sh file2=/server/scripts/lnmp.sh read -p "Select num which you want:" a [ -z "$a" ]&&{ echo "You shoult input one num..." exit 1 } [ $a -ne "1" -a $a -ne "2" -a $a -ne "3" ]&&{ echo "Input num with 1 or 2 or 3..." exit 2 } [ $a -eq "1" ]&&{ if [ -f "$file1" -a -x "$file1" ] then echo "start install lamp..." /bin/sh $file1 else echo "$file1 is not exist or can not be execute!" exit 3 fi } [ $a -eq "2" ]&&{ if [ -f "$file2" -a -x "$file2" ] then echo "start install lnmp..." /bin/sh $file2 else echo "$file2 is not exist or can not be execute!" exit 4 fi } [ $a -eq 3 ]&&{ echo "Exit install..." exit 5 }
企业实用脚本13
1、监控web服务是否正常,不低于3种监控策略。
2、监控db服务是否正常,不低于3种监控策略。
要求间隔1分钟,持续监控。
#!/bin/bash url=wwchengbi.cn Port=80 check_web(){ [ -f /etc/init.d/functions ]&& . /etc/init.d/functions if [ "`nc -w 5 $url $Port &&echo ok`" = "ok" ];then #if [ "`echo -e ‘\n‘|telnet $url $Port|grep Connected|wc -l`" = "1" ];then #if [ "`nmap $url -p $Port|grep open|wc -l`" = "1" ];then #if [ "`curl -I http://$url 2>/dev/null|head -1|egrep "200|302|301"|wc -l`" = "1" ];then #if [ "`curl -I -s -o /dev/null -w ‘%{http_code}\n‘ http://$url`" = "200" ];then action "$url $Port" /bin/true else action "$url $Port" /bin/false fi } main(){ while true do check_web sleep 1m done } main #!/bin/bash Port=3306 check_db(){ [ -f /etc/init.d/functions ]&& . /etc/init.d/functions #if [ `lsof -i:$Port|wc -l` -gt 1 ];then #if [ "`netstat -tunlp|grep 3306|wc -l`" = "1" ];then if [ `mysql -umha -pmha -h 172.16.2.10 -P $Port -e "show databases;"|wc -l` -gt 1 ];then action "MySQL $Port online" /bin/true else action "MySQL $Port down" /bin/false fi } main(){ while true do check_db sleep 1m done } main
企业实用脚本14
监控memcache服务是否正常,模拟用户(web客户端)检测。
使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率
企业实用脚本15
面试及实用考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)
#!/bin/bash check_www(){ md5sum -c /tmp/md5_www.log > /tmp/result_www.log [ ! -f /tmp/result_www.log ]&&echo "/tmp/result_www.log not exist."exit2 exec < /tmp/result_www.log while read line do file=`echo $line|awk -F ‘ ‘ ‘{printf $1}‘` result=`echo $line|awk -F ‘ ‘ ‘{printf $2}‘` #echo $file #echo $result [ ! "$result" = "OK" ]&&action "$file" /bin/false done } main(){ while true do LANG=en [ -f /etc/init.d/functions ]&& . /etc/init.d/functions [ ! -f /tmp/md5_www.log ]&& echo "/tmp/md5_www.log not exsit."&&exit 1 check_www action "Alii check done." /bin/true sleep 3m done } main