目录:
1、监控MySQL主从同步
企业面试题1:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
模拟文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
[root@oldboy~] # mysql -uroot-p'oldboy' -S /data/3307/mysql.sock -e "show slavestatusG;" *************************** 1.row *************************** Slave_IO_State:Waiting formaster to send event Master_Host:10.0.0.179 #当前的mysql master服务器主机 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File:mysql-bin.000013 Read_Master_Log_Pos: 502547 Relay_Log_File:relay-bin.000013 Relay_Log_Pos:251 Relay_Master_Log_File:mysql-bin.000013 Slave_IO_Running:Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 502547 Relay_Log_Space:502986 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: 0 #和主库比同步延迟的秒数,这个参数很重要 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 脚本 [root@shell scripts] # vicheck_mysql_slave.sh #!/bin/sh #oldboy linux training #2015-05-17 #说明:本脚本来自老男孩linux21期学员张耀开发! # Source function library. . /etc/init .d /functions # Defined variables MysqlUser=root MysqlPass=oldboy123 MysqlPort=3307 Mysqlsock= /data/ $MysqlPort /mysql .sock ErrorNo=(1158 1159 1008 10071062) errorlog= /tmp/error_skip .log MysqlCmd= "/application/mysql/bin/mysql-u$MysqlUser -p$MysqlPass -S $Mysqlsock" # Judge mysql server is ok? [ -S $Mysqlsock ] ||{ echo "Maybe MySQL have sometingwrong" exit 1 } # Defined skip error Functions function error_skip(){ local flag flag=0 for num in ${ErrorNo[@]} do if [ "$1" == "$num" ]; then $MysqlCmd -e 'stop slave;set globalsql_slave_skip_counter=1;start slave;' echo "$(date +%F_%R) $1" >>$errorlog else echo "$(date +%F_%R) $1" >>$errorlog ((flag++)) fi done [ "$flag" == "${#ErrorNo[@]}" ] &&{ action "MySQL Slave" /bin/false uniq $errorlog|mail -s "MySQLSlave is error" 12345678@qq.com } } # Defined check slave Functions function check_slave(){ MyResult=`$MysqlCmd -e 'show slavestatusG' | egrep '_Running|Behind_Master|SQL_Errno' | awk '{print $NF}' ` array=($MyResult) if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ] then action "MySQL Slave" /bin/true else error_skip ${array[3]} fi } # Defined main Functions function main(){ while true do check_slave sleep 60 done } main |
2、批量创建文件及改名
企业面试题2:使用for循环在/oldboy目录下通过随机小写10个字母,批量创建10个html文件,名称例如为:
[root@oldboy oldboy]# sh/server/scripts/oldboy.sh
[root@oldboy oldboy]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 1511:34 coaolvajcq_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 gmkhrancxh_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 jdxexendbe_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 qcawgsrtkp_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 qnvuxvicni_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 tmdjormaxr_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 ugaywanjlm_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 vfrphtqjpc_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 vioesjmcbu_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 wzewnojiwe_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 xzzruhdzda_oldboy.html
请用至少两种方法实现,将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
2.1批量创建脚本 [root@shell scripts] # vitouch2.sh #!/bin/bash Path= /oldboy [ -d $Path ] || mkdir $Path for i in ` seq 10` do char=` echo $RANDOM|md5sum| cut -c 2-11| tr [0-9] [a-j]` touch $Path/${char}_oldboy.html done [root@shell scripts] # shtouch2.sh [root@shell scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1107:49 aeddbiacdj_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bccbacghba_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bdhijefefb_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bhfghjcgaa_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ddaacedijc_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ebcbbfabaf_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ecafccebbi_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 egcdafafad_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 iehfcgfaef_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ifddfaaicd_oldboy.html 2.2批量改名 [root@shell scripts] # vimv2.sh #!/bin/bash Path= /oldboy [ -d $Path ] && cd $Path for file in ` ls ` do mv $ file ` echo $ file | sed -e "s#oldboy#oldgirl#g" -e "s#html#HTML#g" ` done [root@shell scripts] # sh mv2.sh [root@shell scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1107:49 aeddbiacdj_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bccbacghba_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bdhijefefb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bhfghjcgaa_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ddaacedijc_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ebcbbfabaf_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ecafccebbi_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 egcdafafad_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 iehfcgfaef_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ifddfaaicd_oldgirl.HTML [root@shell scripts] # vimv3.sh #!/bin/bash Path= /oldboy [ -d $Path ] && cd $Path for file in ` ls ` do mv $ file ` echo ${ file /oldboy .html /oldgirl .HTML}` done [root@shell scripts] # sh mv3.sh [root@shell scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1108:12 abcaeacdbe_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 affabgbccg_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 badbifffbg_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 ccbcifibbe_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 cchbacgegb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 cdfbjfdiib_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 chjechdgab_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 ghibcfcbee_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 icafaafbdb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 igiijhbebj_oldgirl.HTML |
3、批量创建用户随机密码
企业面试题3:批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[root@shell scripts] # viuseradd.sh #! /bin/bash . /etc/init .d /functions Path= /server/scripts UserDb=$Path /user .db FailDb=$Path /fail_user .db [ -d "$Path" ] || mkdir -p $Path [ -f "$UserDb" ] || touch $UserDb [ -f "$FailDb" ] || touch $FailDb for n in $( seq -w 10) do passwd =` echo $( date +%t%N)$RANDOM|md5sum| cut -c 2-9` useradd oldboy$n >& /dev/null && user_status=$? echo "$passwd" | passwd --stdinoldboy$n >& /dev/null && pass_status=$? if [ $user_status - eq 0 -a $pass_status -eq0 ]; then action "adduser oldboy$n" /bin/true echo -e "user: oldboy$npass:$passwd" >>$UserDb else action "adduser oldboy$n" /bin/false echo -e "user: oldboy$npass:$passwd" >>$FailDb fi done [root@shell scripts] # shuseradd.sh adduser oldboy01 [ OK ] adduser oldboy02 [ OK ] adduser oldboy03 [ OK ] adduser oldboy04 [ OK ] adduser oldboy05 [ OK ] adduser oldboy06 [ OK ] adduser oldboy07 [ OK ] adduser oldboy08 [ OK ] adduser oldboy09 [ OK ] adduser oldboy10 [ OK ] [root@shell scripts] # cat user.db user: oldboy01 pass:f3291720 user: oldboy02 pass:457a9f30 user: oldboy03 pass:ff186389 user: oldboy04 pass:8f884b7c user: oldboy05 pass:26f831b4 user: oldboy06 pass:344e2300 user: oldboy07 pass:0736b278 user: oldboy08 pass:67c1fa76 user: oldboy09 pass:b11e7aa9 user: oldboy10 pass:9e0c3673 |
4、判断网络主机存活
企业面试题4:写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/sh for n in ` seq 254` do ping -c1 10.0.0.$n &> /dev/null if [ $? - eq 0 ] then echo "10.0.0.$n is up " >> /tmp/uplist .log else echo "10.0.0.$n is down " >> /tmp/downlist .log fi done |
5、解决DOS攻击生产案例
企业实战题5:请用至少两种方法实现!写一个脚本解决DOS攻击生产案例。
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables-AINPUT -s 10.0.1.10 -j DROP。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@shell scripts] # vi dos.sh #!/bin/bash log= /tmp/tmp .log [ -f $log ] || touch $log function add_iptables(){ whileread line do ip=` echo $line| awk '{print $2}' ` count=` echo $line| awk '{print $1}' ` if [ $count -gt 100 ] && [`iptables -L -n| grep "$ip" | wc -l` -lt 1 ] then iptables -I INPUT -s $ip -jDROP echo "$line isdropped" >> /tmp/droplist .log fi done <$log } function main(){ whiletrue do #awk '{print $1}' access.log|grep-v "^$"|sort|uniq -c >$log netstat -an| grep EST| awk -F '[:]+' '{print $6}' | sort | uniq -c >$log add_iptables sleep 180 done } main |
6、MySQL启动脚本
企业实战题6:开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf&
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
[root@shell scripts] # vi mysqld1.sh #!/bin/bash # chkconfig: 2345 65 37 # description: manager multiplemysqld server. # Warning: This script uses the/etc/init.d/functions system function. # System kernel version isCentOS6.6,2.6.32-504.el6.x86_64. # I'm not sure whether other systemscan be used normally. # Source function library. . /etc/init .d /functions Port=$2 datadir= /data/ $2 Path= "/application/mysql" CmdPath= "$Path/bin" MysqlCmd= "$CmdPath/mysqld_safe" prog=mysqld Port=$2 datadir= /data/ $2 pidfile=${PIDFILE-$datadir /mysqld .pid} lockfile=${LOCKFILE- /var/lock/subsys/mysqld $Port} MysqlConf= "$datadir/my.cnf" RETVAL=0 Usage1(){ echo -e " 33[33m USAGE: $0 {startport|stop port|status port|restart port} |