功能型网站开发价格,wordpress多域名图标,沧州企业网站制作的,dede怎么做商城网站老男孩Shell企业面试题 shell 2016年9月7日 本文来自于老男孩教育,未经本人同意#xff0c;禁止转载#xff01;否则追究法律责任。 原文#xff1a;http://oldboy.blog.51cto.com/2561410/1632876 企业面试题1#xff1a; #xff08;生产实战案例#xff09;#x… 老男孩Shell企业面试题 shell 2016年9月7日 本文来自于老男孩教育,未经本人同意禁止转载否则追究法律责任。 原文http://oldboy.blog.51cto.com/2561410/1632876 企业面试题1 生产实战案例监控MySQL主从同步是否异常如果异常则发送短信或者邮件给管理员。提示如果没主从同步环境,可以用下面文本放到文件里读取来模拟 阶段1开发一个守护进程脚本每30秒实现检测一次。 阶段2如果同步出现如下错误号1158,1159,1008,1007,1062则跳过错误。 阶段3请使用数组技术实现上述脚本获取主从判断及错误号部分 [rootoldboy~]# mysql -uroot -poldboy -S /data/3307/mysql.sock -e show slavestatus\G;*************************** 1. row *************************** Slave_IO_State:Waiting for master 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: 本脚本由李佳豪同学分享 [rootdb02 tmp]# cat MySQL主从监控.sh#!/bin/bashMysqlmysql -uroot -poldboy123 -S /data/3307/mysql.sockL_aaa(){Status$Mysql -e show slave status\G | grep -E _Running: Yes|Seconds_Behind_Master: [0-2] | wc -lCode$Mysql -e show slave status\G | awk /Last_SQL_Errno:/{print $2}}L_Status(){ [ $Status -ne 3 ] { return 1 } || { return 0 }}S_Code(11581159100810071062)L_Skip(){ [ $Code -eq 0 ] return 0 for i in ${S_Code[*]} do [ $Code -eq $i ] { $Mysql -e stop slave;set global sql_slave_skip_counter 1;start slave; \ return 0 } done return 1}main(){while true do L_aaa L_Skip Error1$? L_Status Error2$? [ $Error1 -eq 1 -o $Error2 -eq 1 ] echo Errordone}main 企业面试题2 使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件名称例如为 [rootoldboy oldboy]# sh /server/scripts/oldboy.sh[rootoldboy oldboy]# lscoaolvajcq_oldboy.html qnvuxvicni_oldboy.html vioesjmcbu_oldboy.htmlgmkhrancxh_oldboy.html tmdjormaxr_oldboy.html wzewnojiwe_oldboy.htmljdxexendbe_oldboy.html ugaywanjlm_oldboy.html xzzruhdzda_oldboy.htmlqcawgsrtkp_oldboy.html vfrphtqjpc_oldboy.html 脚本如下 [rootdb02 ~]# sh html.sh[rootdb02 ~]# ls /oldboy/aebccciiaj_oldboy.html fffabecgbc_oldboy.htmlafffebcchb_oldboy.html ffghcffegb_oldboy.htmldbccddabbj_oldboy.html hffbhfgdff_oldboy.htmlehbdaedach_oldboy.html jadafhbaaf_oldboy.htmlfbaacihehi_oldboy.html jgfebjbebd_oldboy.html[rootdb02 ~]# cat html.sh#!/bin/bash. /etc/init.d/functions[ -d /oldboy ]|| mkdir -p /oldboycd /oldboyfor i in seq 10do touch echo $RANDOM|md5sum|cut -c 1-10|tr [0-9] [a-z]_oldboy.htmldone 企业面试题3 请用至少两种方法实现 将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。 第一种方法 [rootdb02 ~]# sh html1.sh[rootdb02 ~]# ll /oldboy/total 0-rw-r--r-- 1 root root 0 Jul 20 20:45 aebccciiaj_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 afffebcchb_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 dbccddabbj_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 ehbdaedach_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 fbaacihehi_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 fffabecgbc_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 ffghcffegb_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 hffbhfgdff_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 jadafhbaaf_oldboy.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 jgfebjbebd_oldboy.HTML[rootdb02 ~]# cat html1.sh#!/bin/bashcd /oldboyfor i in cd /oldboydo rename oldboy oldgirl *.html|rename html HTML *.htmldone 第二种方法 [rootdb02 ~]# sh html2.sh[rootdb02 ~]# ll /oldboy/total 0-rw-r--r-- 1 root root 0 Jul 20 20:45 aebccciiaj_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 afffebcchb_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 dbccddabbj_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 ehbdaedach_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 fbaacihehi_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 fffabecgbc_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 ffghcffegb_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 hffbhfgdff_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 jadafhbaaf_oldgirl.HTML-rw-r--r-- 1 root root 0 Jul 20 20:45 jgfebjbebd_oldgirl.HTML[rootdb02 ~]# cat html2.sh#!/bin/bashcd /oldboyfor i in ls *.HTMLdomv $i echo $i|sed -e s#oldboy#oldgirl#g;s#html#HTML#gdone 补充QQ 1163718639 [rootdb01 shell30]# cat 3.sh #!/bin/bash for i in /oldboy/* do rename oldboy. oldgir. $i|echo oldgirlok done for x in /oldboy/* do rename html HTML $x|echo htmlok done [rootdb01 shell30]# 企业面试题4 批量创建10个系统帐号oldboy01-oldboy10并设置密码密码为随机8位字符串。 [rootdb02 tmp]# cat useradd.sh#!/bin/bash[ $UID -ne 0 ] echo only root runexit 1[ -f /etc/init.d/functions ] . /etc/init.d/functionsisexist() { result$(grep -w ^$1 /etc/passwd|wc -l) if [ $result -ne 0 ];then echo user $1 is exist!! ret 1 create user is continue fi}ret() { if [ $1 -eq 0 ];then action $2 /bin/true else action $2 /bin/false fi}create() { for i in $(seq -w 10) do useroldboy$i isexist $user pass$(cat /proc/sys/kernel/random/uuid|md5sum|cut -c 1-10) useradd $userecho $pass|passwd --stdin $user /dev/null ret $? crate user $user echo $user $pass /tmp/user.list done}main() { create}main 企业面试题5 写一个脚本实现判断10.0.0.0/24网络里当前在线用户的IP有哪些方法有很多 本脚本由刘沈晨分享 注意此脚本使用nmap如果没有需要使用yum -y install nmap [rootdb02 scripts]# cat test_5.sh#!/bin/sh#[ -f /etc/init.d/functions ] . /etc/init.d/functionsfunction IP_count(){ for n in 10.0.0.{0..255} do IP_checknmap -sP $n|grep Host is up|wc -l if [ ${IP_check} -eq 1 ];then action $n /bin/true let i1 fi done}function main(){ IP_count echo The total number of online IP Addresses is $i}main 企业实战题6 写一个脚本解决DOS攻击生产案例 提示根据web日志或者或者网络连接数监控当某个IP并发连接数或者短时内PV达到100即调用防火墙命令封掉对应的IP监控频率每隔3分钟。防火墙命令为iptables -I INPUT -s 10.0.1.10 -j DROP。 本脚本使用测试文件进行编写模拟文件下载 链接http://pan.baidu.com/s/1gfqeHaz 密码3usn [rootdb02 scripts]# cat test_6.sh#!/bin/sh#[ -f /etc/init.d/functions ] . /etc/init.d/functionsIP_file/server/scripts/ddos.txtIP_filter_commandiptables -I INPUT -j DROP -sIP_recover_commandiptables -D INPUT -j DROP -sfunction IP_check(){ grep EST ${IP_file}|awk -F [ |:] {print $6}|sort |uniq -c|sort -rn -k1 /server/scripts/ip.txt}function IP_filter(){ exec /server/scripts/ip.txt while read line do IP_countecho $line|awk {print $1} IPecho $line|awk {print $2} IP_filiptables -L -n|grep \b${IP}\b|wc -l if [ ${IP_count} -gt 25 -a ${IP_fil} -eq 0 ];then ${IP_filter_command} ${IP} echo ${IP} /server/scripts/ip_filtered.txt action Filter ${IP} /bin/true fi done}function IP_recover(){ exec /server/scripts/ip.txt while read line do IP_countecho $line|awk {print $1} IPecho $line|awk {print $2} IP_filiptables -L -n|grep \b${IP}\b|wc -l if [ ${IP_count} -le 25 -a ${IP_fil} -eq 1 ];then ${IP_recover_command} ${IP} echo ${IP} /server/scripts/ip_filtered.txt action Recover ${IP} /bin/true fi done}function main(){ case $1 in filter) IP_check echo $(date %F-%H:%M:%S) filtered by $(whoami) /server/scripts/ip_filtered.txt IP_filter ;; recover) IP_check echo $(date %F-%H:%M:%S) recovered by $(whoami) /server/scripts/ip_filtered.txt IP_recover ;; *) echo USAGE:$0 {filter|recover} exit 1 esac}main $* 企业实战题7 开发mysql多实例启动脚本 已知mysql多实例启动命令为mysqld_safe--defaults-file/data/3306/my.cnf 停止命令为mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown 请完成mysql多实例启动启动脚本的编写 要求用函数case语句、if语句等实现 1.第一种方法比较简单适用于一台MySQL服务器上[rootdb02 mysql]# cat mysql.sh#!/bin/bash############################I am YuhongCong#qq:604419314##########################[ -f /etc/init.d/functions ] . /etc/init.d/functions#configprot3306mysql_userrootmysql_paswd123456Path/data/3306/mysql_start(){ if [ -f ${Path}mysqld.pid ];then echo MySQL is running exit 1 else mysqld_safe --defaults-file${Path}my.cnf /dev/null if [ $? -eq 0 ];then action MySQL is starting /bin/true else action MySQL no start /bin/false fi fi}mysql_stop(){ if [ ! -f ${Path}mysqld.pid ];then echo Mysql not running else mysqladmin -u ${mysql_user} -p${mysql_paswd} -S ${Path}mysql.sock shutdown if [ $? -eq 0 ];then action mysql is stop /bin/true else action mysql no stop /bin/false fi fi}case $1 instart) mysql_start ;;stop) mysql_stop ;;restart) mysql_stop sleep 3 mysql_start ;;*) echo Please input {start|stop|restart} ;;esac 第二种方法应强哥及李啸宇、大欣等要求进行改版。 适用于管理多实例启动的脚本 [rootdb02 tmp]# cat mysqlgood.sh#!/bin/bash###################I am yuhongcong#qq:604419314#################[ -f /etc/init.d/functions ] . /etc/init.d/functionsprot3306mysql_userrootmysql_paswd123456Path/data/3306/#############################################cat END############################# 3306# 3307# exit###########################ENDread -p Please input {3306|3307} amysql1_start(){ if [ -f ${Path}mysqld.pid ];then echo MySQL is runningx else mysqld_safe --defaults-file${Path}my.cnf /dev/null if [ $? -eq 0 ];then action mysql 3306 is starting /bin/true else action mysql 3306 not start /bin/false fi fi}mysql1_stop(){ if [ ! -f ${Path}mysqld.pid ];then echo mysql 3306 not running else mysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3306/mysql.sock shutdown if [ $? -eq 0 ];then action mysql 3306 is stopping /bin/true else action mysql 3306 no stop /bin/false fi fi}mysql2_start(){ if [ -f /data/3307/mysqld.pid ];then echo MySQL 3307 is runningx else mysqld_safe --defaults-file/data/3307/my.cnf /dev/null if [ $? -eq 0 ];then action mysql 3307 is starting /bin/true else action mysql 3307 not start /bin/false fi fi}mysql2_stop(){ if [ ! -f /data/3307/mysqld.pid ];then echo mysql 3307 not running else mysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3307/mysql.sock shutdown if [ $? -eq 0 ];then action mysql 3307 is stopping /bin/true else action mysql 3307 no stop /bin/false fi fi}aaa(){cat END############################ 1.start# 2.stop# 3.restart# 4.exit###########################ENDread -p please input {1|2|3|4} b}bbb(){cat END############################ 1.start# 2.stop# 3.restart# 4.exit###########################ENDread -p please input {1|2|3|4} c}case $a in3306) aaa;;3307) bbb;;exit) exit;;esaccase $b in1) mysql1_start;;2) mysql1_stop;;3) mysql1_stop sleep 3 mysql1_start;;4) exit 2;;esaccase $c in1) mysql2_start;;2) mysql2_stop;;3) mysql2_stop sleep 3 mysql2_start;;4) exit 2;;esac 企业实战题8: 如何实现对MySQL数据库进行分库备份请用脚本实现 本文由刘康同学提供 [rootdb02 tmp]# cat MoreDatabasesBak.sh#!/bin/bash. /etc/init.d/functionsPORT3306BAKUPDIR/server/backupMYSQLUSERrootMYSQLPASSoldboySOCK/data/${PORT}/mysql.sockCMDDIR/application/mysql/binMYSQL${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}DBNAME${MYSQL} -e show databases;|sed 1d|egrep -v _schema|mysqlAYYAYDB($DBNAME)MYSQLDUMP${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}function BAKDB(){ for((n0;n${#AYYAYDB[*]};n)) do ${MYSQLDUMP} --events -B ${AYYAYDB[$n]} |gzip ${BAKUPDIR}/${AYYAYDB[$n]}_$(date %T-%F)_bak.sql.gz RETVAL$? if [ $RETVAL -eq 0 ] then echo ${AYYAYDB[$n]} bak successfull date %F-%T /tmp/DBbakstatus.log else echo ${AYYAYDB[$n]} bak fail date %F-%T /tmp/DBbakstatus.log fi done mail -s DB STATUS www.abcdocker.comqq.com /tmp/DBbakstatus.log return}function DBstatus(){[ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p${MYSQL} -e show full processlist; /dev/nullRETVAL$?if [ $RETVAL -eq 0 ] then /tmp/DBbakstatus.log BAKDB else echo DB BAD!!! date %F-%T | mail -s DB BAD!!! www.abcdocker.comqq.com exitfi}DBstatus 企业实战题9: 如何实现对MySQL数据库进行分库加分表备份请用脚本实现 本脚本还是由刘康同学提供 [rootdb02 tmp]# cat MoreTablesBak.sh#!/bin/bash. /etc/init.d/functionsPORT3306BAKUPDIR/server/backupMYSQLUSERrootMYSQLPASSoldboySOCK/data/${PORT}/mysql.sockCMDDIR/application/mysql/binMYSQL${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}DBNAME${MYSQL} -e show databases;|sed 1d|egrep -v _schema|mysqlAYYAYDB($DBNAME)MYSQLDUMP${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}function BAKDB(){DBNAME${MYSQL} -e show databases;|sed 1d|egrep -v _schema|mysqlAYYAYDB($DBNAME) for((n0;n${#AYYAYDB[*]};n)) do TABLE_BAK_DIR${BAKUPDIR}/${AYYAYDB[$n]} TABLENAME${MYSQL} -e show tables from ${AYYAYDB[$n]};|sed 1d ARRAYTABLE(${TABLENAME}) for((i0;i${#ARRAYTABLE[*]};i)) do [ -d ${TABLE_BAK_DIR} ] || mkdir ${TABLE_BAK_DIR} -p ${MYSQLDUMP} ${AYYAYDB[$n]} ${ARRAYTABLE[$i]} |gzip ${TABLE_BAK_DIR}/${ARRAYTABLE[$i]}_$(date %T-%F)_bak.sql.gz RETVAL$? if [ $RETVAL -eq 0 ] then echo ${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak successfull date %F-%T /tmp/DB_table_bakstatus.log else echo ${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak fail date %F-%T /tmp/DB_table_bakstatus.log fi done done mail -s DB STATUS www.abcdocker.comqq.com /tmp/DB_table_bakstatus.log return}function DBstatus(){[ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p${MYSQL} -e show full processlist; /dev/nullRETVAL$?if [ $RETVAL -eq 0 ] then /tmp/DB_table_bakstatus.log BAKDB else echo DB BAD!!! date %F-%T | mail -s DB BAD!!! www.abcdocker.comqq.com exitfi}DBstatus 企业面试题10 请用至少两种方法实现 bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。 I am oldboy teacher welcome to oldboy training class. 没讲数组之前自己做的[rootweb02 ~]# cat /server/scripts/28/bash.sh#!/bin/bashfor n in echo I am oldboy teacher welcome to oldboy training class.doAAA$( echo $n|wc -c)if [ $AAA -gt 7 ];then continue;fiecho $ndone 第一种方法不使用数组 [rootdb02 scripts]# cat 3.sh#!/bin/bashfor i in I am oldboy teacher welcome to oldboy training class.do if [ ${#i} -le 6 ] then echo $i fidone 第二种方法使用数组 [rootdb02 scripts]# cat 3.sh#!/bin/basharray(I am oldboy teacher welcome to oldboy training class.)for ((i0;i${#array[]};i))do if [ echo ${array[i]}|wc -L -le 6 ] then echo ${array[i]} fidone 第三种方法命令拼接 echo I am oldboy teacher welcome to oldboy training class. | awk {for(i1;iNF;i){alength($i);if(a 6){print $i}}} 企业面试题11 开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。 [rootdb01 scripts]# cat read.sh#!/bin/sh#no.1read -p Pls input two num: a b[ -z $a -o -z $b ]{ echo must be two num. exit 1}#no.2expr $a 1 /dev/null[ $? -ne 0 ]{ echo First arg must be int. exit 2}expr $b 1 /dev/null[ $? -ne 0 ]{ echo Second arg must be int. exit 3}#no.3[ $a -gt $b ]{ echo $a $b exit 0}[ $a -eq $b ]{ echo $a $b exit 0}[ $a -lt $b ]{ echo $a $b exit 0} 企业面试题12 打印选择菜单一键安装Web服务 [rootoldboyscripts]# 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、要对执行的脚本进行相关条件判断例如脚本是否存在是否可执行等。 由10组黑哥提供吴依 黑哥博客www.dockerwy.com #!/bin/bashcat EOF1.[install lamp]2.[install lnmp]3.[exit]pls input the num you want:EOFread -p please input number 1-3: alamp() { if [ -f /server/scripts/lamp.sh ];thenecho startinstalling lamp.sleep 2sh /server/scripts/lamp.shelseecho file does not exist fiexit 0}lnmp () { if [ -f /server/scripts/lnmp.sh ];thenecho startinstalling lnmp.sleep 3sh /server/scripts/lnmp.shelseecho file does not exist fiexit 0}case $a in 1)lamp;; 2)lnmp;; 3)exit 0;; *)echo pls input the num you want:;;esac 企业面试题13 1、监控web服务是否正常不低于3种监控策略。 2、监控db服务是否正常不低于3种监控策略。 要求间隔1分钟持续监控。 思路 1、监控的三种方式 通过进程名监控 ps -ef|grep name 通过端口监控 netstat -lntup|grep port/lsof -i:80 通过链接信息上的进程名 netstat -lntup|grep port 2、等待一分钟 3、后台执行 nohup #!/bin/bash[ -f /etc/init.d/functions ] . /etc/init.d/functionscheck_httpd(){countps -ef|grep nginx|grep -v grep|wc -l if [ $count -ne 0 ] then action nginx is ok /bin/true else action nginx is dead /bin/false fi } check_httpd_port(){ countnetstat -lntup|grep 8080|wc -l if [ $count -ne 0 ] then action nginx is ok /bin/true else action nginx is dead /bin/false fi } 企业面试题14 监控memcache服务是否正常模拟用户web客户端检测。 使用nc命令加上set/get来模拟检测以及监控响应时间及命中率。 感谢乔飞翔同学的分享 [rootdb02 scripts]# cat mem.sh#!/bin/bash#########################date:2016-07-17 ##author:fxqiao ##QQ:827724746 ##E-mail:qfx1995163.com##########################echo -ne \033[0;33mecho -ne \E[1;32mcatEOT _oo0oo_ 088888880 88 . 88 (| -_- |) 0\ /0 ___/---\___ . \\\\| |// . / \\\\||| : |||// \\ /_ ||||| -:- |||||- \\ | | \\\\\\ - /// | | | \_| \---/ |_/ | \ .-\__ - __/-. / ___. . /--.--\ . .___ . .___\_|_/___. . | | : - \.;\ _ /;./ - : | | \ \ _. \_ __\ /__ _/ .- / / -.____.___ \_____/___.-____.- --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 佛祖保佑 iii 服务正常EOTecho -ne \E[0m[ -f /etc/init.d/functions ] . /etc/init.d/functions###variablesRETVAL0###colorRED_COLOR\E[1;31mGREEN_COLOR\E[1;32mYELLOW_COLOR\E[1;33mBLUE_COLOR\E[1;34mRES\E[0m####menu listmenu() {cat EOF############################ 1 memcached status 2 memcached stop 3 memcached get_hits 4 quitEOF}status() {Set$(printf set key1 0 0 6\r\noldboy\r\n|nc 127.0.0.1 11211|grep STORED|wc -l)Get$(printf get key1\r\n|nc 127.0.0.1 11211|grep oldboy|wc -l)Port$(netstat -lntup|grep memcached|wc -l) if [ $Port -ge 4 ];then Num$(expr $Set $Get) if [ $Num -eq 2 ];then action Memcached server is running /bin/true else action Memcached server is not normal /bin/false fi else action Memcached server is not running /bin/false read -t 10 -p Memcached server whether open:[y/n]: b if [ $b y -o $b Y ];then /usr/bin/memcached -m 16m -p 11211 -d -u root -c 8192 action Memcached server is starting /bin/true elif [ $b n -o $b N ];then echo -e $YELLOW_COLOR Goodbye.............$RES fi fi return $RETVAL}stop() {Port$(netstat -lntup|grep memcached|wc -l) if [ $Port -ge 4 ];then pkill memcached /dev/null RETVAL$? if [ $RETVAL -eq 0 ];then action Memcached server is stopping /bin/true else action Memcached server is stopping /bin/false fi else action Memcached server not running /bin/false fi}get_hits() {Cmd_get$(printf stats\r\n|nc 127.0.0.1 11211|grep cmd_get|awk {print $3})Get_hits$(printf stats\r\n|nc 127.0.0.1 11211|grep get_hits|awk {print $3})Get_misses$(printf stats\r\n|nc 127.0.0.1 11211|grep get_misses|awk {print $3})Port$(netstat -lntup|grep memcached|wc -l) if [ $Port -ge 4 ];then echo A total of cmd_get is $Cmd_get sleep 1 echo A total of get_hits is $Get_hits sleep 1 echo A total of get_misses is $Get_misses else action Memcached is not running /bin/false fi return $RETVAL}main() {while truedomenuread -t 10 -p Please input number: aexpr $a 1 /dev/nullif [ $? -ne 0 ];then echo -e $BLUE_COLOR Please input error,not int $RES exit 1ficase $a in 1) status RETVAL$? ;; 2) stop RETVAL$? ;; 3) get_hits RETVAL$? ;; 4) clear echo ############################################## echo -e $GREEN_COLOR figlet GoodBye $RES echo ############################################## exit 0 ;; *) echo -e $RED_COLOR input error $RESesacdone}mainexit $RETVAL 企业面试题15 面试及实战考试题监控web站点目录/var/html/www下所有文件是否被恶意篡改文件内容被改了如果有就打印改动的文件名发邮件定时任务每3分钟执行一次(10分钟时间完成)。 [rootdb02 tmp]# cat html.sh#!/bin/shhtml_dir/var/html/wwwhtml_filefind /var/html/www -type fcheck_dir/tmp/checkdir[ ! -d $check_dir ] mkdir $check_dirfor n in $html_filedo md5sum $n $check_dir/1.txtdonewhile truedo md5sum -c $check_dir/1.txt|grep FAILED $check_dir/2.txt [ -s $check_dir/2.txt ] \ echo cat $check_dir/2.txt|mail -s date:date %F-%H:%M:%S Web is dangerous 18576749166163.com $check_dir/2.txtsleep 3done 企业面试题16 企业案例:写网络服务独立进程模式下rsync的系统启动脚本 例如/etc/init.d/rsyncd{start|stop|restart}。 要求 1.要使用系统函数库技巧。 2.要用函数不能一坨SHI的方式。 3.可被chkconfig管理。 [rootwww tmp]# cat 123.sh#!/bin/bash###################I am yuhongcong#qq:604419314################## Comments to support chkconfig on RedHat Linux# chkconfig: 2345 64 36status1$(ps -ef | egrep rsync --daemon.*rsyncd.conf | grep -v grep)pidfile/etc/rsync/rsyncd.pidstart_rsyncrsync --daemon --config/etc/rsync/rsyncd.conffunction rsyncstart() { if [ ${status1}X X ];then rm -f $pidfile ${start_rsync} status2$(ps -ef | egrep rsync --daemon.*rsyncd.conf | grep -v grep) if [ ${status2}X ! X ];then echo rsync service start.......OK fi else echo rsync service is running ! fi}function rsyncstop() { if [ ${status1}X ! X ];then kill -9 $(cat $pidfile) status2$(ps -ef | egrep rsync --daemon.*rsyncd.conf | grep -v grep) if [ ${statusw2}X X ];then echo rsync service stop.......OK fi else echo rsync service is not running ! fi}function rsyncstatus() { if [ ${status1}X ! X ];then echo rsync service is running ! else echo rsync service is not running ! fi}function rsyncrestart() { if [ ${status1}X X ];then echo rsync service is not running... rsyncstart else rsyncstop rsyncstart fi } case $1 in start) rsyncstart ;; stop) rsyncstop ;; status) rsyncstatus ;; restart) rsyncrestart ;; *) echo echo Usage: $0 start|stop|restart|status echoesac 企业面试题17 老男孩教育天津项目学生实践抓阄题目 好消息老男孩培训学生外出企业项目实践机会第6次来了本月中旬但是名额有限队员限3人班长带队。 因此需要挑选学生因此需要一个抓阄的程序 要求 1、执行脚本后想去的同学输入英文名字全拼产生随机数01-99之间的数字数字越大就去参加项目实践前面已经抓到的数字下次不能在出现相同数字。 2、第一个输入名字后屏幕输出信息并将名字和数字记录到文件里程序不能退出继续等待别的学生输入。 [rootdb02 tmp]# cat select17.sh#!/bin/sh /tmp/temp.txtinput(){while truedo read -p pls input your name: name if [ -z $name ] then continue elif [ $name 0 ] then break fi rand$((RANDOM%100)) echo -e $rand\t$name /tmp/temp.txtdone}output(){cat /tmp/temp.txt |sort -n -k1 -r|sed 3a#################}main(){inputoutput}main 企业面试题18 已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果请破解这些字符串对应的md5sum前的RANDOM对应数字
21029299
00205d1c
a3da1677
1f6d12dd
890684b方法一 #!/bin/bash. /etc/init.d/functionsMD5PASS(2102929900205d1ca3da16771f6d12dd890684b)for ((n0;n32767;n))dofor((i0;i${#MD5PASS[*]};i))do md5echo $n | md5sum|cut -c 1-8 if [ $md5 ${MD5PASS[$i]} ] then echo $n ${MD5PASS[$i]} fidonedone 方法二 [rootopenvpn-server ~]# cat md5sum.sh#!/bin/sha(2102929900205d1ca3da16771f6d12dd890684b)for i in seq 32767do sourceecho $i|md5sum|cut -c 1-8 for j in ${a[]} do if [ $source $j ];then echo $source -- $i fi donedone 方法三 [rootm01 ~]# cat mianshiti6.sh#!/bin/bashmima(2102929900205d1ca3da16771f6d12dd890684b)for i in {1..32767}do numecho $i | md5sum | cut -c -8 for n in ${mima[*]} do if [ $num $n ] then echo $i match $n continue fi donedone 企业面试题19 批量检查多个网站地址是否正常 要求 shell数组方法实现检测策略尽量模拟用户访问思路 http://www.etiantian.org http://www.taobao.com http://oldboy.blog.51cto.com http://10.0.0.7 [rootdb02 scripts]# cat aaa.sh#!/bin/sh#################Author:YuHongCong#604419314qq.com################[ -f /etc/init.d/functions ] . /etc/init.d/functionsURLS(http://www.etiantian.orghttp://www.taobao.comhttp://oldboy.blog.51cto.comhttp://10.0.0.70)CHECK_URL(){ wget -T 3 --spider --tries2 $1 /dev/null if [ $? -eq 0 ] then return 0 else return 1 fi}MON_URL(){for url in ${URLS[*]}do CHECK_URL $url if [ $? -eq 0 ] then action $url /bin/true else action $url /bin/false fidone}main(){while truedo MON_URL sleep 10done}main 老男孩推荐包含倒计时的方法 [rootdb01 shizhan]# cat oldboy01.sh #!/bin/sh#################Author:oldboy#31333741qq.com################[ -f /etc/init.d/functions ] . /etc/init.d/functionsURLS(http://www.etiantian.orghttp://www.taobao.comhttp://oldboy.blog.51cto.comhttp://10.0.0.70)LTIME(){echo -n time;for n in {1..10}do if [ $n -eq 10 ] then echo start else echo -n . fi sleep 1done}CHECK_URL(){ wget -T 3 --spider --tries2 $1 /dev/null if [ $? -eq 0 ] then return 0 else return 1 fi}MON_URL(){for url in ${URLS[*]}do CHECK_URL $url if [ $? -eq 0 ] then action $url /bin/true else action $url /bin/false fidone}main(){while truedo LTIME MON_URL sleep 10done}main 企业面试题20(中企动力) 用shell处理以下内容 1、按单词出现频率降序排序 2、按字母出现频率降序排序
the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation#!/bin/shstrthe squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomationwords(){echo $str|sed s#[^a-zA-Z]# #g|tr \n|grep -v ^$|sort|uniq -c|sort -r -n}letters(){echo $str|grep -o .|sort|egrep -v |^$|[^a-zA-Z]|uniq -c|sort -n -r}case $1 inwords) words ;;letters) letters ;;*) echo usage:$0 {words|letters}esac 企业面试题22: 开发通过web界面展示监控Nginx代理节点状态效果图如下。 文件打包 密码xso0 脚本及内容请下载 lvskeepalived集群部分Shell企业案例 企业面试题23、 【LVS主节点】手工开发ipvsadm管理lvs的脚本ip_vs 实现/etc/init.d/lvs {start|stop|restart} [rootwww tmp]# cat lvs.sh #!/bin/bashif [ $UID -ne 0 ];then echo Permission denied (you must be root) exit 1fi[ -f /etc/init.d/functions ] . /etc/init.d/functionsvip_netmask10.0.0.3/24vip10.0.0.3service_addr10.0.0.3:80rs(10.0.0.7:8010.0.0.8:80)start() { #add vip ifconfig|grep $vip /dev/null if [ $? -ne 0 ];then ip addr add $vip_netmask dev eth0 label eth0:0 \ action add vip $vip_netmask /bin/true else echo vip $vip_netmask already exists. fi lvs_table$(ipvsadm -ln|grep $vip|wc -l) if [ $lvs_table -eq 1 ];then echo virtual server already exists. else #add virtual server ipvsadm -A -t $service_addr -s wrr \ action add virtual server $service_addr /bin/true fi for ip in ${rs[]};do rs_num$(ipvsadm -ln|grep $ip|wc -l) if [ $rs_num -eq 1 ];then echo real server $ip already exists. else #add real server ipvsadm -a -t $service_addr -r $ip -g -w 1 \ action add real server $ip /bin/true fi done #set tcp tcpfin udp connection timeout ipvsadm --set 30 5 60 \ action set tcp tcpfin udp connection timeout values. /bin/true}stop() { ifconfig|grep $vip /dev/null if [ $? -ne 0 ];then echo without vip $vip else #delete vip ip addr del $vip_netmask dev eth0 label eth0:0 \ action delete vip $vip_netmask. /bin/true fi lvs_table$(ipvsadm -ln|grep $vip|wc -l) for ip in ${rs[]};do rs_num$(ipvsadm -ln|grep $ip|wc -l) let lvs_tablers_num done if [ $lvs_table -ge 1 ];then #clear all table ipvsadm -C \ action clear all lvs table. /bin/true else echo lvs table is empty. fi}case $1 in start) start ;; stop) stop ;; restart) stop sleep 2 start ;; *) echo USAGE: $0 {start|stop|restart}esac 企业面试题24 【LVS主节点】模拟keepalived健康检查功能管理LVS节点 当节点挂掉检测2次间隔2秒从服务器池中剔除好了检测2次间隔2秒加进来 提示利用ipvsadm命令实现添加和减少LVS节点。 [rootwww tmp]# cat lvs_check_rs.sh#!/bin/bashvip10.0.0.3rs_ip(10.0.0.810.0.0.7)remove_check() { for rs in ${rs_ip[]};do ipvsadm -ln|grep $rs /dev/null if [ $? -eq 0 ];then rs_donw0 a_group_down0 for i in 1 2;do rs_down$(nmap -p 80 $rs|grep open|wc -l) let a_group_downrs_down sleep 2 done if [ $a_group_down -eq 0 ];then ipvsadm -d -t ${vip}:80 -r ${rs}:80 fi fi done}join_check() { for rs in ${rs_ip[]};do ipvsadm -ln|grep $rs /dev/null if [ $? -ne 0 ];then a_group_up0 rs_up0 for j in 1 2;do rs_up$(nmap -p 80 $rs|grep open|wc -l) let a_group_uprs_up sleep 2 done if [ $a_group_up -eq 2 ];then ipvsadm -a -t ${vip}:80 -r ${rs}:80 -g -w 1 fi fi done}check_rs() { while true;do remove_check sleep 1 join_check sleep 1 done}check_rs 企业面试题25 【LVS客户端节点】开发LVS客户端设置VIP以及抑制ARP的管理脚本 实现/etc/init.d/lvsclient {start|stop|restart} [rootweb01-lnmp-09 scripts]# cat /etc/init.d/lvsclient#!/bin/bash#chkconfig: 2345 37 57#Write by Bevin 2016#LVS_Nginxexport PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin[ -f /etc/init.d/functions ] source /etc/init.d/functions#Step 1function L_ipadd() { VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -eq 0 ] then ip addr add 10.0.0.12/32 dev lo label lo:12 route add -host 10.0.0.12 dev lo sleep 1 VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -eq 0 ] then ip addr add 10.0.0.12/32 dev lo label lo:12 route add -host 10.0.0.12 dev lo sleep 1 VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -eq 0 ] then action ip addr add 10.0.0.12/32 ... /bin/false exit 1 else action ip addr add 10.0.0.12/32 ... /bin/true fi else action ip addr add 10.0.0.12/32 ... /bin/true fi else action ip addr add 10.0.0.12/32 is exist. fi echo 1 /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 /proc/sys/net/ipv4/conf/all/arp_announce}function L_ipdel() { VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -ne 0 ] then ip addr del 10.0.0.12/32 dev lo route del -host 10.0.0.12 dev lo sleep 1 VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -ne 0 ] then action ip addr del 10.0.0.12/32 ... /bin/false exit 1 else action ip addr del 10.0.0.12/32 ... /bin/true fi else action ip addr del 10.0.0.12/32 ... /bin/true fi echo 0 /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 /proc/sys/net/ipv4/conf/lo/arp_announce echo 0 /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 /proc/sys/net/ipv4/conf/all/arp_announce}#Step 2function L_exec() { case $1 in start) L_ipadd ;; stop) L_ipdel ;; status) VIPRESip addr|grep 10.0.0.12|wc -l sleep 1 if [ $VIPRES -ne 0 ] then echo LVS Nginx is working else echo LVS Nginx is not working fi ;; restart) VIPRESip addr|grep 10.0.0.12|wc -l if [ $VIPRES -ne 0 ] then L_ipdel sleep 1 L_ipadd else echo ip 10.0.0.12 is not working sleep 1 L_ipadd fi ;; *) echo USAGE: $0 {start|stop|restart|status} esac} 企业面试题26 【LVS备节点】模拟keepalved vrrp功能监听主节点如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务提醒注意ARP缓存 [rootlvs-lb01-05 scripts]# cat lvs.sh#!/bin/bash#Write by Bevin 2016export PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin[ -f /etc/init.d/functions ] source /etc/init.d/functions#Step 1 Instal ip_vsCh_ipvsrpm -qa ipvsadm|wc -lif [ $Ch_ipvs -eq 0 ] then yum install ipvsadm -yfi#Step 2 FunctionsVIP10.0.0.12V_TAIL12RIP(910)function L_LVS_VIP_RIP() { VIPRESip addr|grep $VIP|wc -l if [ $VIPRES -eq 0 ] then ip addr add $VIP/24 dev eth0 label eth0:$V_TAIL ipvsadm -C ipvsadm -A -t $VIP:80 -s rr for n in ${RIP[*]} do ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -g done else ipvsadm -C ipvsadm -A -t $VIP:80 -s rr for n in ${RIP[*]} do ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -g done fi}function L_check_vip() { VIPRESip addr|grep $VIP|wc -l if [ $VIPRES -eq 0 ] then L_LVS_VIP_RIP sleep 5 if [ $VIPRES -eq 0 ] then L_LVS_VIP_RIP sleep 5 if [ $VIPRES -eq 0 ] then echo Warning: lb01: $VIP is down.|mail -s txbevinsina.com exit 1 fi fi fi sleep 5}function L_check_rip() { for n in ${RIP[*]} do CurlRescurl -I -s 10.0.0.$n|grep -E 200|301|302|wc -l if [ $CurlRes -ne 0 ] then RSipvsadm -Ln|grep 10.0.0.$n|wc -l if [ $RS -eq 0 ] then ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 fi else ipvsadm -d -t $VIP:80 -r 10.0.0.$n:80 mail -s 10.0.0.$n nginx is down at $(date %F-%T). txbevinsina.com continue fi done sleep 5}#Step 3L_LVS_VIP_RIPwhile truedo L_check_vip sleep 1 L_check_rip sleep 1done 后面几道题如果大家会可以把答案发送给我 mail:new_oldboy163.com 企业面试题27 请用shell或Python编写一个正方形(oldboy_square.sh)接收用户输入的数字。 例如 [rootoldboy ~]# sh oldboy_square1.shPlease Enter a number:5[rootoldboy ~]# sh oldboy_square2.sh Please Enter a number:9■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 企业面试题28 请用shell或Python编写一个等腰三角形(oldboy2_triangle.sh)接收用户输入的数字。 例如 [rootoldboy ~]# sh oldboy2_triangle.shPlease Enter a number:5 * *** *********************[rootoldboy ~]# sh oldboy2_triangle.shPlease Enter a number:8 * *** ***** ******* ********* *************************************** 企业面试题29 请用shell或Python编写一个画直角梯形程序(oldboy4.sh)接收用户输入的参数nm 例如 [rootoldboy ~]# sh oldboy4.sh 4 6*************** 27,28,29三道题可以参考 http://oldboy.blog.51cto.com/2561410/1718607 本文出自 “老男孩linux培训” 请务必保留此出处http://oldboy.blog.51cto.com/2561410/1632876