@老男孩Shell企业面试题
shell
2016年9月7日
本文来自于老男孩教育,未经本人同意,禁止转载!否则追究法律责任。
原文:http://oldboy.blog.51cto.com/2561410/1632876
企业面试题1:
(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
[root@oldboy~]# mysql -uroot -p'oldboy' -S /data/3307/mysql.sock -e "show slavestatus\G;"*************************** 1. row ***************************Slave_IO_State:Waiting for master to send eventMaster_Host:10.0.0.179 #当前的mysql master服务器主机Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File:mysql-bin.000013Read_Master_Log_Pos: 502547Relay_Log_File:relay-bin.000013Relay_Log_Pos:251Relay_Master_Log_File:mysql-bin.000013Slave_IO_Running:YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 502547Relay_Log_Space:502986Until_Condition:NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:
本脚本由李佳豪同学分享
[root@db02 tmp]# cat MySQL主从监控.sh#!/bin/bashMysql="mysql -uroot -poldboy123 -S /data/3307/mysql.sock"L_aaa(){Status=`$Mysql -e "show slave status\G" | grep -E "_Running: Yes|Seconds_Behind_Master: [0-2]" | wc -l`Code=`$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 0for i in ${S_Code[*]}do[ $Code -eq $i ] && {$Mysql -e "stop slave;set global sql_slave_skip_counter = 1;start slave;" && \return 0}donereturn 1}main(){while truedoL_aaaL_SkipError1=$?L_StatusError2=$?[ $Error1 -eq 1 -o $Error2 -eq 1 ] && echo "Error"done}main
企业面试题2:
使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:
[root@oldboy oldboy]# sh /server/scripts/oldboy.sh[root@oldboy 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
脚本如下:
[root@db02 ~]# sh html.sh[root@db02 ~]# 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[root@db02 ~]# cat html.sh#!/bin/bash. /etc/init.d/functions[ -d /oldboy ]|| mkdir -p /oldboycd /oldboyfor i in `seq 10`dotouch `echo $RANDOM|md5sum|cut -c 1-10|tr "[0-9]" "[a-z]"`_oldboy.htmldone
企业面试题3:
请用至少两种方法实现!
将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。
第一种方法:
[root@db02 ~]# sh html1.sh[root@db02 ~]# 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[root@db02 ~]# cat html1.sh#!/bin/bashcd /oldboyfor i in `cd /oldboy`dorename "oldboy" "oldgirl" *.html|rename "html" "HTML" *.htmldone
第二种方法:
[root@db02 ~]# sh html2.sh[root@db02 ~]# 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[root@db02 ~]# cat html2.sh#!/bin/bashcd /oldboyfor i in `ls *.HTML`domv $i `echo $i|sed -e 's#oldboy#oldgirl#g;s#html#HTML#g'`done
补充:QQ 1163718639
[root@db01 shell30]# cat 3.sh#!/bin/bashfor i in /oldboy/* do rename oldboy. oldgir. $i|echo oldgirlok done for x in /oldboy/* do rename html HTML $x|echo htmlok done[root@db01 shell30]#
企业面试题4:
批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
[root@db02 tmp]# cat useradd.sh#!/bin/bash[ $UID -ne 0 ]&& echo "only root run"&&exit 1[ -f /etc/init.d/functions ]&& . /etc/init.d/functionsisexist() {result=$(grep -w "^$1" /etc/passwd|wc -l)if [ $result -ne 0 ];thenecho "user $1 is exist!!"ret 1 "create user is "continuefi}ret() {if [ $1 -eq 0 ];thenaction "$2" /bin/trueelseaction "$2" /bin/falsefi}create() {for i in $(seq -w 10)douser="oldboy$i"isexist $userpass=$(cat /proc/sys/kernel/random/uuid|md5sum|cut -c 1-10)useradd $user&&echo $pass|passwd --stdin $user &>/dev/nullret $? "crate user $user"echo "$user $pass" >> /tmp/user.listdone}main() {create}main
企业面试题5:
写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)
本脚本由刘沈晨分享
注意:此脚本使用nmap,如果没有需要使用yum -y install nmap
[root@db02 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}doIP_check=`nmap -sP $n|grep "Host is up"|wc -l`if [ ${IP_check} -eq 1 ];thenaction "$n" /bin/truelet i+=1fidone}function main(){IP_countecho "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
[root@db02 scripts]# cat test_6.sh#!/bin/sh#[ -f /etc/init.d/functions ] && . /etc/init.d/functionsIP_file="/server/scripts/ddos.txt"IP_filter_command="iptables -I INPUT -j DROP -s"IP_recover_command="iptables -D INPUT -j DROP -s"function 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.txtwhile read linedoIP_count=`echo $line|awk '{print $1}'`IP=`echo $line|awk '{print $2}'`IP_fil=`iptables -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.txtaction "Filter ${IP}" /bin/truefidone}function IP_recover(){exec < /server/scripts/ip.txtwhile read linedoIP_count=`echo $line|awk '{print $1}'`IP=`echo $line|awk '{print $2}'`IP_fil=`iptables -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.txtaction "Recover ${IP}" /bin/truefidone}function main(){case "$1" infilter)IP_checkecho "$(date +%F-%H:%M:%S) filtered by $(whoami)" >> /server/scripts/ip_filtered.txtIP_filter;;recover)IP_checkecho "$(date +%F-%H:%M:%S) recovered by $(whoami)" >> /server/scripts/ip_filtered.txtIP_recover;;*)echo "USAGE:$0 {filter|recover}"exit 1esac}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服务器上[root@db02 mysql]# cat mysql.sh#!/bin/bash############################I am YuhongCong#qq:604419314##########################[ -f /etc/init.d/functions ]&& . /etc/init.d/functions#configprot=3306mysql_user=rootmysql_paswd=123456Path=/data/3306/mysql_start(){if [ -f ${Path}mysqld.pid ];thenecho "MySQL is running"exit 1elsemysqld_safe --defaults-file=${Path}my.cnf &>/dev/null &if [ $? -eq 0 ];thenaction "MySQL is starting" /bin/trueelseaction "MySQL no start" /bin/falsefifi}mysql_stop(){if [ ! -f ${Path}mysqld.pid ];thenecho "Mysql not running"elsemysqladmin -u ${mysql_user} -p${mysql_paswd} -S ${Path}mysql.sock shutdownif [ $? -eq 0 ];thenaction "mysql is stop" /bin/trueelseaction "mysql no stop" /bin/falsefifi}case "$1" instart)mysql_start;;stop)mysql_stop;;restart)mysql_stopsleep 3mysql_start;;*)echo "Please input {start|stop|restart}";;esac
第二种方法:应强哥及李啸宇、大欣等要求进行改版。
适用于管理多实例启动的脚本
[root@db02 tmp]# cat mysqlgood.sh#!/bin/bash###################I am yuhongcong#qq:604419314#################[ -f /etc/init.d/functions ]&& . /etc/init.d/functionsprot=3306mysql_user=rootmysql_paswd=123456Path=/data/3306/#############################################cat <<END############################# 3306# 3307# exit###########################ENDread -p "Please input {3306|3307}" amysql1_start(){if [ -f ${Path}mysqld.pid ];thenecho "MySQL is runningx"elsemysqld_safe --defaults-file=${Path}my.cnf &>/dev/null &if [ $? -eq 0 ];thenaction "mysql 3306 is starting" /bin/trueelseaction "mysql 3306 not start" /bin/falsefifi}mysql1_stop(){if [ ! -f ${Path}mysqld.pid ];thenecho "mysql 3306 not running"elsemysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3306/mysql.sock shutdownif [ $? -eq 0 ];thenaction "mysql 3306 is stopping" /bin/trueelseaction "mysql 3306 no stop" /bin/falsefifi}mysql2_start(){if [ -f /data/3307/mysqld.pid ];thenecho "MySQL 3307 is runningx"elsemysqld_safe --defaults-file=/data/3307/my.cnf &>/dev/null &if [ $? -eq 0 ];thenaction "mysql 3307 is starting" /bin/trueelseaction "mysql 3307 not start" /bin/falsefifi}mysql2_stop(){if [ ! -f /data/3307/mysqld.pid ];thenecho "mysql 3307 not running"elsemysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3307/mysql.sock shutdownif [ $? -eq 0 ];thenaction "mysql 3307 is stopping" /bin/trueelseaction "mysql 3307 no stop" /bin/falsefifi}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_stopsleep 3mysql1_start;;4)exit 2;;esaccase "$c" in1)mysql2_start;;2)mysql2_stop;;3)mysql2_stopsleep 3mysql2_start;;4)exit 2;;esac
企业实战题8:
如何实现对MySQL数据库进行分库备份,请用脚本实现
本文由刘康同学提供
[root@db02 tmp]# cat MoreDatabasesBak.sh#!/bin/bash. /etc/init.d/functionsPORT='3306'BAKUPDIR='/server/backup'MYSQLUSER='root'MYSQLPASS='oldboy'SOCK="/data/${PORT}/mysql.sock"CMDDIR="/application/mysql/bin"MYSQL="${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`AYYAYDB=($DBNAME)MYSQLDUMP="${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"function BAKDB(){for((n=0;n<${#AYYAYDB[*]};n++))do${MYSQLDUMP} --events -B ${AYYAYDB[$n]} |gzip >${BAKUPDIR}/${AYYAYDB[$n]}_$(date +%T-%F)_bak.sql.gzRETVAL=$?if [ $RETVAL -eq 0 ]thenecho "${AYYAYDB[$n]} bak successfull `date +%F-%T` " >>/tmp/DBbakstatus.logelseecho "${AYYAYDB[$n]} bak fail `date +%F-%T` " >>/tmp/DBbakstatus.logfidonemail -s "DB STATUS" www.abcdocker.com@qq.com < /tmp/DBbakstatus.logreturn}function DBstatus(){[ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p${MYSQL} -e "show full processlist;" &> /dev/nullRETVAL=$?if [ $RETVAL -eq 0 ]then>/tmp/DBbakstatus.logBAKDBelseecho "DB BAD!!! `date +%F-%T`" | mail -s "DB BAD!!!" www.abcdocker.com@qq.comexitfi}DBstatus
企业实战题9:
如何实现对MySQL数据库进行分库加分表备份,请用脚本实现
本脚本还是由刘康同学提供
[root@db02 tmp]# cat MoreTablesBak.sh#!/bin/bash. /etc/init.d/functionsPORT='3306'BAKUPDIR='/server/backup'MYSQLUSER='root'MYSQLPASS='oldboy'SOCK="/data/${PORT}/mysql.sock"CMDDIR="/application/mysql/bin"MYSQL="${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`AYYAYDB=($DBNAME)MYSQLDUMP="${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"function BAKDB(){DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`AYYAYDB=($DBNAME)for((n=0;n<${#AYYAYDB[*]};n++))doTABLE_BAK_DIR="${BAKUPDIR}/${AYYAYDB[$n]}"TABLENAME=`${MYSQL} -e "show tables from ${AYYAYDB[$n]};"|sed 1d`ARRAYTABLE=(${TABLENAME})for((i=0;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.gzRETVAL=$?if [ $RETVAL -eq 0 ]thenecho "${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak successfull `date +%F-%T` " >>/tmp/DB_table_bakstatus.logelseecho "${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak fail `date +%F-%T` " >>/tmp/DB_table_bakstatus.logfidonedonemail -s "DB STATUS" www.abcdocker.com@qq.com < /tmp/DB_table_bakstatus.logreturn}function DBstatus(){[ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p${MYSQL} -e "show full processlist;" &> /dev/nullRETVAL=$?if [ $RETVAL -eq 0 ]then>/tmp/DB_table_bakstatus.logBAKDBelseecho "DB BAD!!! `date +%F-%T`" | mail -s "DB BAD!!!" www.abcdocker.com@qq.comexitfi}DBstatus
企业面试题10:
请用至少两种方法实现!
bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。
I am oldboy teacher welcome to oldboy training class.
没讲数组之前自己做的:[root@web02 ~]# 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 ];thencontinue;fiecho $ndone
第一种方法:不使用数组
[root@db02 scripts]# cat 3.sh#!/bin/bashfor i in I am oldboy teacher welcome to oldboy training class.doif [ "${#i}" -le 6 ]thenecho $ifidone
第二种方法:使用数组
[root@db02 scripts]# cat 3.sh#!/bin/basharray=(I am oldboy teacher welcome to oldboy training class.)for ((i=0;i<${#array[@]};i++))doif [ "`echo ${array[i]}|wc -L`" -le 6 ]thenecho ${array[i]}fidone
第三种方法:命令拼接
echo "I am oldboy teacher welcome to oldboy training class." | awk '{for(i=1;i<=NF;i++){a=length($i);if(a <= 6){print $i}}}'
企业面试题11:
开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。
[root@db01 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服务:
[root@oldboyscripts]# sh menu.sh1.[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 in1)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(){count=`ps -ef|grep nginx|grep -v grep|wc -l`if [ "$count" -ne 0 ]thenaction "nginx is ok" /bin/trueelseaction "nginx is dead" /bin/falsefi}check_httpd_port(){count=`netstat -lntup|grep 8080|wc -l`if [ "$count" -ne 0 ]thenaction "nginx is ok" /bin/trueelseaction "nginx is dead" /bin/falsefi}
企业面试题14:
监控memcache服务是否正常,模拟用户(web客户端)检测。
使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率。
感谢乔飞翔同学的分享
[root@db02 scripts]# cat mem.sh#!/bin/bash#########################date:2016-07-17 ##author:fxqiao ##QQ:827724746 ##E-mail:qfx1995@163.com##########################echo -ne "\033[0;33m"echo -ne "\E[1;32m"cat<<EOT_oo0oo_08888888088" . "88(| -_- |)0\ = /0___/'---'\___.' \\\\| |// './ \\\\||| : |||// \\/_ ||||| -:- |||||- \\| | \\\\\\ - /// | || \_| ''\---/'' |_/ |\ .-\__ '-' __/-. /___'. .' /--.--\ '. .'___."" '< '.___\_<|>_/___.' >' "".| | : '- \'.;'\ _ /';.'/ - ' : | |\ \ '_. \_ __\ /__ _/ .-' / /====='-.____'.___ \_____/___.-'____.-'====='=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^佛祖保佑 iii 服务正常EOTecho -ne "\E[0m"[ -f /etc/init.d/functions ] && . /etc/init.d/functions###variablesRETVAL=0###colorRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'RES='\E[0m'####menu listmenu() {cat <<EOF############################1 memcached status2 memcached stop3 memcached get_hits4 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 ];thenNum=$(expr $Set + $Get)if [ $Num -eq 2 ];thenaction "Memcached server is running" /bin/trueelseaction "Memcached server is not normal" /bin/falsefielseaction "Memcached server is not running" /bin/falseread -t 10 -p "Memcached server whether open:[y/n]:" bif [ "$b" == "y" -o "$b" == "Y" ];then/usr/bin/memcached -m 16m -p 11211 -d -u root -c 8192action "Memcached server is starting" /bin/trueelif [ "$b" == "n" -o "$b" == "N" ];thenecho -e "$YELLOW_COLOR Goodbye.............$RES"fifireturn $RETVAL}stop() {Port=$(netstat -lntup|grep memcached|wc -l)if [ $Port -ge 4 ];thenpkill memcached &>/dev/nullRETVAL=$?if [ $RETVAL -eq 0 ];thenaction "Memcached server is stopping" /bin/trueelseaction "Memcached server is stopping" /bin/falsefielseaction "Memcached server not running" /bin/falsefi}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 ];thenecho "A total of cmd_get is $Cmd_get"sleep 1echo "A total of get_hits is $Get_hits"sleep 1echo "A total of get_misses is $Get_misses"elseaction "Memcached is not running" /bin/falsefireturn $RETVAL}main() {while truedomenuread -t 10 -p "Please input number:" aexpr $a + 1 &>/dev/nullif [ $? -ne 0 ];thenecho -e "$BLUE_COLOR Please input error,not int $RES"exit 1ficase "$a" in1)statusRETVAL=$?;;2)stopRETVAL=$?;;3)get_hitsRETVAL=$?;;4)clearecho "##############################################"echo -e "$GREEN_COLOR `figlet GoodBye` $RES"echo "##############################################"exit 0;;*)echo -e "$RED_COLOR input error $RES"esacdone}mainexit $RETVAL
企业面试题15:
面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。
[root@db02 tmp]# cat html.sh#!/bin/shhtml_dir=/var/html/wwwhtml_file=`find /var/html/www -type f`check_dir=/tmp/checkdir[ ! -d $check_dir ] && mkdir $check_dirfor n in $html_filedomd5sum $n >>$check_dir/1.txtdonewhile truedomd5sum -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" 18576749166@163.com>$check_dir/2.txtsleep 3done
企业面试题16:
企业案例:写网络服务独立进程模式下rsync的系统启动脚本
例如:/etc/init.d/rsyncd{start|stop|restart}。
要求:
1.要使用系统函数库技巧。
2.要用函数,不能一坨SHI的方式。
3.可被chkconfig管理。
[root@www 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.pid"start_rsync="rsync --daemon --config=/etc/rsync/rsyncd.conf"function rsyncstart() {if [ "${status1}X" == "X" ];thenrm -f $pidfile${start_rsync}status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')if [ "${status2}X" != "X" ];thenecho "rsync service start.......OK"fielseecho "rsync service is running !"fi}function rsyncstop() {if [ "${status1}X" != "X" ];thenkill -9 $(cat $pidfile)status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')if [ "${statusw2}X" == "X" ];thenecho "rsync service stop.......OK"fielseecho "rsync service is not running !"fi}function rsyncstatus() {if [ "${status1}X" != "X" ];thenecho "rsync service is running !"elseecho "rsync service is not running !"fi}function rsyncrestart() {if [ "${status1}X" == "X" ];thenecho "rsync service is not running..."rsyncstartelsersyncstoprsyncstartfi}case $1 in"start")rsyncstart;;"stop")rsyncstop;;"status")rsyncstatus;;"restart")rsyncrestart;;*)echoecho "Usage: $0 start|stop|restart|status"echoesac
企业面试题17:
老男孩教育天津项目学生实践抓阄题目:
好消息,老男孩培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。
因此需要挑选学生,因此需要一个抓阄的程序:
要求:
1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。
2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入。
[root@db02 tmp]# cat select17.sh#!/bin/sh> /tmp/temp.txtinput(){while truedoread -p "pls input your name:" nameif [ -z $name ]thencontinueelif [ $name == "0" ]thenbreakfirand=$((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 ((n=0;n<=32767;n++))dofor((i=0;i<${#MD5PASS[*]};i++))domd5=`echo $n | md5sum|cut -c 1-8`if [ "$md5" == ${MD5PASS[$i]} ]thenecho "$n" "${MD5PASS[$i]} "fidonedone
方法二:
[root@openvpn-server ~]# cat md5sum.sh#!/bin/sha=(2102929900205d1ca3da16771f6d12dd890684b)for i in `seq 32767`dosource=`echo $i|md5sum|cut -c 1-8`for j in ${a[@]}doif [ $source == $j ];thenecho $source "-->" $ifidonedone
方法三:
[root@m01 ~]# cat mianshiti6.sh#!/bin/bashmima=(2102929900205d1ca3da16771f6d12dd890684b)for i in {1..32767}donum=`echo $i | md5sum | cut -c -8`for n in ${mima[*]}doif [ "$num" == "$n" ]thenecho "$i match $n"continuefidonedone
企业面试题19:
批量检查多个网站地址是否正常
要求: shell数组方法实现,检测策略尽量模拟用户访问思路
http://www.etiantian.org
http://www.taobao.com
http://oldboy.blog.51cto.com
http://10.0.0.7
[root@db02 scripts]# cat aaa.sh#!/bin/sh#################Author:YuHongCong#604419314@qq.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 --tries=2 $1 &>/dev/nullif [ $? -eq 0 ]thenreturn 0elsereturn 1fi}MON_URL(){for url in ${URLS[*]}doCHECK_URL $urlif [ $? -eq 0 ]thenaction "$url" /bin/trueelseaction "$url" /bin/falsefidone}main(){while truedoMON_URLsleep 10done}main

老男孩推荐:(包含倒计时的方法)
[root@db01 shizhan]# cat oldboy01.sh#!/bin/sh#################Author:oldboy#31333741@qq.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}doif [ $n -eq 10 ]thenecho "start"elseecho -n .fisleep 1done}CHECK_URL(){wget -T 3 --spider --tries=2 $1 &>/dev/nullif [ $? -eq 0 ]thenreturn 0elsereturn 1fi}MON_URL(){for url in ${URLS[*]}doCHECK_URL $urlif [ $? -eq 0 ]thenaction "$url" /bin/trueelseaction "$url" /bin/falsefidone}main(){while truedoLTIMEMON_URLsleep 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/shstr="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"words(){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
脚本及内容请下载

lvs+keepalived集群部分Shell企业案例:
企业面试题23、
【LVS主节点】手工开发ipvsadm管理lvs的脚本ip_vs
实现:/etc/init.d/lvs {start|stop|restart}
[root@www tmp]# cat lvs.sh#!/bin/bashif [ $UID -ne 0 ];thenecho "Permission denied (you must be root)"exit 1fi[ -f /etc/init.d/functions ] && . /etc/init.d/functionsvip_netmask=10.0.0.3/24vip=10.0.0.3service_addr=10.0.0.3:80rs=(10.0.0.7:8010.0.0.8:80)start() {#add vipifconfig|grep $vip &>/dev/nullif [ $? -ne 0 ];thenip addr add $vip_netmask dev eth0 label eth0:0 && \action "add vip $vip_netmask" /bin/trueelseecho "vip $vip_netmask already exists."filvs_table=$(ipvsadm -ln|grep "$vip"|wc -l)if [ $lvs_table -eq 1 ];thenecho "virtual server already exists."else#add virtual serveripvsadm -A -t $service_addr -s wrr && \action "add virtual server $service_addr" /bin/truefifor ip in ${rs[@]};dors_num=$(ipvsadm -ln|grep "$ip"|wc -l)if [ $rs_num -eq 1 ];thenecho "real server $ip already exists."else#add real serveripvsadm -a -t $service_addr -r $ip -g -w 1 && \action "add real server $ip" /bin/truefidone#set tcp tcpfin udp connection timeoutipvsadm --set 30 5 60 && \action "set tcp tcpfin udp connection timeout values." /bin/true}stop() {ifconfig|grep $vip &>/dev/nullif [ $? -ne 0 ];thenecho "without vip $vip"else#delete vipip addr del $vip_netmask dev eth0 label eth0:0 && \action "delete vip $vip_netmask." /bin/truefilvs_table=$(ipvsadm -ln|grep "$vip"|wc -l)for ip in ${rs[@]};dors_num=$(ipvsadm -ln|grep "$ip"|wc -l)let lvs_table+=rs_numdoneif [ $lvs_table -ge 1 ];then#clear all tableipvsadm -C && \action "clear all lvs table." /bin/trueelseecho "lvs table is empty."fi}case "$1" instart)start;;stop)stop;;restart)stopsleep 2start;;*)echo "USAGE: $0 {start|stop|restart}"esac
企业面试题24
【LVS主节点】模拟keepalived健康检查功能管理LVS节点,
当节点挂掉(检测2次,间隔2秒)从服务器池中剔除,好了(检测2次,间隔2秒)加进来
提示:利用ipvsadm命令实现添加和减少LVS节点。
[root@www tmp]# cat lvs_check_rs.sh#!/bin/bashvip=10.0.0.3rs_ip=(10.0.0.810.0.0.7)remove_check() {for rs in ${rs_ip[@]};doipvsadm -ln|grep $rs &>/dev/nullif [ $? -eq 0 ];thenrs_donw=0a_group_down=0for i in 1 2;dors_down=$(nmap -p 80 $rs|grep open|wc -l)let a_group_down+=rs_downsleep 2doneif [ $a_group_down -eq 0 ];thenipvsadm -d -t ${vip}:80 -r ${rs}:80fifidone}join_check() {for rs in ${rs_ip[@]};doipvsadm -ln|grep $rs &>/dev/nullif [ $? -ne 0 ];thena_group_up=0rs_up=0for j in 1 2;dors_up=$(nmap -p 80 $rs|grep open|wc -l)let a_group_up+=rs_upsleep 2doneif [ $a_group_up -eq 2 ];thenipvsadm -a -t ${vip}:80 -r ${rs}:80 -g -w 1fifidone}check_rs() {while true;doremove_checksleep 1join_checksleep 1done}check_rs
企业面试题25
【LVS客户端节点】开发LVS客户端设置VIP以及抑制ARP的管理脚本
实现:/etc/init.d/lvsclient {start|stop|restart}
[root@web01-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() {VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -eq 0 ]thenip addr add 10.0.0.12/32 dev lo label lo:12route add -host 10.0.0.12 dev losleep 1VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -eq 0 ]thenip addr add 10.0.0.12/32 dev lo label lo:12route add -host 10.0.0.12 dev losleep 1VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -eq 0 ]thenaction "ip addr add 10.0.0.12/32 ..." /bin/falseexit 1elseaction "ip addr add 10.0.0.12/32 ..." /bin/truefielseaction "ip addr add 10.0.0.12/32 ..." /bin/truefielseaction "ip addr add 10.0.0.12/32 is exist."fiecho "1" > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce}function L_ipdel() {VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -ne 0 ]thenip addr del 10.0.0.12/32 dev loroute del -host 10.0.0.12 dev losleep 1VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -ne 0 ]thenaction "ip addr del 10.0.0.12/32 ..." /bin/falseexit 1elseaction "ip addr del 10.0.0.12/32 ..." /bin/truefielseaction "ip addr del 10.0.0.12/32 ..." /bin/truefiecho "0" > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "0" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" > /proc/sys/net/ipv4/conf/all/arp_announce}#Step 2function L_exec() {case "$1" instart)L_ipadd;;stop)L_ipdel;;status)VIPRES=`ip addr|grep 10.0.0.12|wc -l`sleep 1if [ $VIPRES -ne 0 ]thenecho "LVS Nginx is working"elseecho "LVS Nginx is not working"fi;;restart)VIPRES=`ip addr|grep 10.0.0.12|wc -l`if [ $VIPRES -ne 0 ]thenL_ipdelsleep 1L_ipaddelseecho "ip 10.0.0.12 is not working"sleep 1L_ipaddfi;;*)echo "USAGE: $0 {start|stop|restart|status}"esac}
企业面试题26
【LVS备节点】模拟keepalved vrrp功能,监听主节点,如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务(提醒:注意ARP缓存)
[root@lvs-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_ipvs=`rpm -qa ipvsadm|wc -l`if [ $Ch_ipvs -eq 0 ]thenyum install ipvsadm -yfi#Step 2 FunctionsVIP=10.0.0.12V_TAIL=12RIP=(910)function L_LVS_VIP_RIP() {VIPRES=`ip addr|grep $VIP|wc -l`if [ $VIPRES -eq 0 ]thenip addr add $VIP/24 dev eth0 label eth0:$V_TAILipvsadm -Cipvsadm -A -t $VIP:80 -s rrfor n in ${RIP[*]}doipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -gdoneelseipvsadm -Cipvsadm -A -t $VIP:80 -s rrfor n in ${RIP[*]}doipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -gdonefi}function L_check_vip() {VIPRES=`ip addr|grep $VIP|wc -l`if [ $VIPRES -eq 0 ]thenL_LVS_VIP_RIPsleep 5if [ $VIPRES -eq 0 ]thenL_LVS_VIP_RIPsleep 5if [ $VIPRES -eq 0 ]thenecho "Warning: lb01: $VIP is down."|mail -s txbevin@sina.comexit 1fififisleep 5}function L_check_rip() {for n in ${RIP[*]}doCurlRes=`curl -I -s 10.0.0.$n|grep -E "200|301|302"|wc -l`if [ $CurlRes -ne 0 ]thenRS=`ipvsadm -Ln|grep 10.0.0.$n|wc -l`if [ $RS -eq 0 ]thenipvsadm -a -t $VIP:80 -r 10.0.0.$n:80fielseipvsadm -d -t $VIP:80 -r 10.0.0.$n:80mail -s "10.0.0.$n nginx is down at $(date +%F-%T)." txbevin@sina.comcontinuefidonesleep 5}#Step 3L_LVS_VIP_RIPwhile truedoL_check_vipsleep 1L_check_ripsleep 1done
后面几道题如果大家会可以把答案发送给我
mail:new_oldboy@163.com
-------------------
企业面试题27
请用shell或Python编写一个正方形(oldboy_square.sh),接收用户输入的数字。
例如:
[root@oldboy ~]# sh oldboy_square1.shPlease Enter a number:5++++++++++++++++++++++++++++++++++++++++++++++++++[root@oldboy ~]# sh oldboy_square2.shPlease Enter a number:9■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
企业面试题28
请用shell或Python编写一个等腰三角形(oldboy2_triangle.sh),接收用户输入的数字。
例如:
[root@oldboy ~]# sh oldboy2_triangle.shPlease Enter a number:5*************************[root@oldboy ~]# sh oldboy2_triangle.shPlease Enter a number:8****************************************************************
企业面试题29
请用shell或Python编写一个画直角梯形程序(oldboy4.sh),接收用户输入的参数n,m
例如:
[root@oldboy ~]# sh oldboy4.sh 4 6***************
27,28,29三道题可以参考
http://oldboy.blog.51cto.com/2561410/1718607
本文出自 “老男孩linux培训”
请务必保留此出处http://oldboy.blog.51cto.com/2561410/1632876