企业Shell面试题及企业运维实战案例(三)

文章转载自:http://www.pythonheidong.com/blog/article/2870/

1、企业Shell面试题1:批量生成随机字符文件名案例

使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串oldboy,名称示例如下:

[root@oldgirl C19]# ls /oldboy
apquvdpqbk_oldboy.html  mpyogpsmwj_oldboy.html  txynzwofgg_oldboy.html
bmqiwhfpgv_oldboy.html  mtrzobsprf_oldboy.html  vjxmlflawa_oldboy.html
jhjdcjnjxc_oldboy.html  qeztkkmewn_oldboy.html
jpvirsnjld_oldboy.html  ruscyxwxai_oldboy.html

解答:

#!/bin/sh
[ ! -d /oldboy ] && mkdir /oldboy -p
for i in {01..10}
do
  pass=`uuidgen|cut -c 1-10`
  touch /oldboy/${pass}_oldboy.html
done

2、企业Shell面试题2:批量改名特殊案例

将以上面试题中结果文件名中的oldboy字符串全部改成oldgirl(最好用for循环实现),并且将扩展名html全部改成大写

解答:

方法一:
#!/bin/sh for i in `ls /oldboy/*.html` do mv $i `echo $i|sed 's#oldboy.html#oldgirl.HTML#g'` done
方法二:
rename oldboy.html oldgirl.HTML *.html
方法三:
#!/bin/sh
cd /oldboy &&\
for i in `ls`
do
  str=`echo $i|cut -c -10`
  mv ${str}_oldboy.html ${str}_oldgril.HTML
done

3、企业Shell面试题3:批量创建特殊要求用户案例

批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机数,要求字符和数字等混合)

解答:

#!/bin/bash
for n in {01..10}
do
  pass=`uuidgen|cut -c 1-10`
  id oldboy$n &>/dev/null
  if [ $? -ne 0 ];then
    useradd oldboy$n
    echo $pass|passwd --stdin oldboy$n &>/dev/null
    echo -e "user: oldboy$n pass: $pass" >>/tmp/user.log
  else
        echo "oldboy$n 已存在"
  fi
done

4、企业Shell面试题4:扫描网络内存活主机案例

写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?

解答:

#!/bin/sh
. /etc/init.d/functions
for ip in {1..254}
do
  ping -c2 -W1 10.0.0.$ip &>/dev/null
  if [ $? -eq 0 ]
    then
    action "10.0.0.$ip" /bin/true
  fi
done

5、企业Shell面试题5:解决DOS攻击生产案例

    写一个Shell脚本解决DOS攻击生产案例。请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100(读者根据实际情况设定),即调用防火墙命令封掉对应的IP。防火墙命令为:iptables-I INPUT -s IP地址 -j DROP

解答:

#!/bin/sh
while true
do
awk '{print $1}' access.log |sort |uniq -c >/tmp/ip.log#分析web访问日志
#netstat -an|grep EST|awk -F "[ :]+" '{print $6}'|sort|uniq -c#通过网络连接线
  while read 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 -j DROP
    echo "$line is dropped" >>/tmp/drop_ip.log
    fi
  done </tmp/ip.log
  sleep 5
done

iptables查看结果

[root@m01 ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  140.205.225.206      0.0.0.0/0           
DROP       all  --  140.205.225.205      0.0.0.0/0           
DROP       all  --  140.205.225.200      0.0.0.0/0           
DROP       all  --  140.205.225.188      0.0.0.0/0           
DROP       all  --  140.205.201.43       0.0.0.0/0           
DROP       all  --  140.205.201.40       0.0.0.0/0           
DROP       all  --  140.205.201.37       0.0.0.0/0           
DROP       all  --  140.205.201.30       0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     

6、企业Shell面试题6:脚本实现MySQL数据库分库备份

#!/bin/sh
Myuser=root
Mypass=123456
Mycmd="mysql -u$Myuser -p$Mypass"
Mydump="mysqldump -u$Myuser -p$Mypass -x -B -F -R"
Dblist=`$Mycmd -e "show databases;"|sed '1,2d'|egrep -v "_schema|mysql"`
for database in $Dblist
do
  $Mydump $database|gzip >/tmp/${database}_$(date +%F).sql.gz &>/dev/null
done

7、企业Shell面试题7:脚本实现MySQL数据库分库分表备份

#!/bin/sh
Myuser=root
Mypass=123456
Mycmd="mysql -u$Myuser -p$Mypass"
Mydump="mysqldump -u$Myuser -p$Mypass -x -F -R"
Dblist=`$Mycmd -e "show databases;"|sed '1,2d'|egrep -v "_schema|mysql"`
for database in $Dblist
do
  Tablist=`$Mycmd -e "show tables from $database;"|sed 1d`
  for table in $Tablist
  do
  mkdir -p /tmp/${database}
  $Mydump $database $table|gzip >/tmp/${database}/${table}_$(date +%F).sql.gz
  done
done

8、企业Shell面试题8:筛选符合长度的单词案例

利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。

  I am oldboy teacher welcome to oldboy training class

解答:

方法一:
#!/bin/sh for word in I am oldboy teacher welcome to oldboy training class do if [ ${#word} -le 6 ] then echo $word fi done
方法二:数组
#!/bin/sh
word=(I am oldboy teacher welcome to oldboy training class)
for((i=0;i<${#word[*]};i++))
do 
  if [ `echo ${word[i]}|wc -L` -le 6 ]
    then
      echo ${word[i]}
  fi
done
方法三:awk
echo "I am oldboy teacher welcome to oldboy training class"|awk '{for(i=1;i<=NF;i++)if(length($i)<=6)print $i}'

9、企业Shell面试题9:MySQL主从复制异常监控案例

10、企业Shell面试题10:比较整数大小经典案例

       综合实战案例:开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。用条件表达式(禁止if)进行判断并以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数不对给予提示

解答:

传参的方法:
#!/bin/sh
num1=$1
num2=$2
[ $# -ne 2 ] && echo "请输入两个数字:" && exit 1
expr 1 + $num1 &>/dev/null
[ $? -eq 2 ] && echo "输入的第一个数字不是整数" && exit 2

expr 1 + $num2 &>/dev/null
[ $? -eq 2 ] && echo "输入的第二个数字不是整数" && exit 2

[ $num1 -lt $num2 ] && echo "$num1<$num2" && exit 0
[ $num1 -eq $num2 ] && echo "$num1=$num2" && exit 0
echo "$num1>$num2"
read读入方法:
#!/bin/sh
read -p "请输入第一个数字:" num1
read -p "请输入第二个数字:" num2
expr 1 + $num1 &>/dev/null
[ $? -eq 2 ] && echo "输入的第一个数字不是整数" && exit 2

expr 1 + $num2 &>/dev/null
[ $? -eq 2 ] && echo "输入的第二个数字不是整数" && exit 2

[ $num1 -lt $num2 ] && echo "$num1<$num2" && exit 0
[ $num1 -eq $num2 ] && echo "$num1=$num2" && exit 0
echo "$num1>$num2"

11、企业Shell面试题11:菜单自动化软件部署经典案例

综合实例:打印选择菜单,按照选择一键安装不同的Web服务。
示例菜单:
[root@oldboy scripts]# shmenu.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"后退出脚本,工作中就是正式lamp一键安装脚本;
2、当用户输入2时,输出“startinstalling lnmp.提示” 然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本,工作中就是正式lnmp一键安装脚本;
3、当输入3时,退出当前菜单及脚本;
4、当输入任何其它字符,给出提示“Input error”后退出脚本;
5、要对执行的脚本进行相关的条件判断,例如:脚本文件是否存在,是否可执行等判断,尽量用上前面讲解的知识点。

解答:

#!/bin/sh
. /etc/init.d/functions
cat<<EOF
===================================
    1.[install lamp]
    2.[install lnmp]
    3.[install nfs]
    4.[exit]
===================================
EOF
while true
do
read -p "pls input the num you want:" num
case $num in
  1)
    /bin/sh /server/scripts/lamp.sh
    action "startinstalling lamp..." /bin/true
    ;;
  2)
    /bin/sh /server/scripts/lnmp.sh
    action "startinstalling lnmp..." /bin/true
    ;;
  3)
    /bin/sh /server/scripts/nfs.sh
    action "startinstalling nfs..." /bin/true
    ;;
  4)
    exit
    ;;
  *)
    echo "对不起,输入错误,请重新输入"
esac
done

12、企业Shell面试题12:Web及MySQL服务异常监测案例

①通过端口:
#!/bin/sh
. /etc/init.d/functions
port=`lsof -i:80|grep nginx|wc -l`
if [ $port -ge 2 ];then
  action "Nginx is running" /bin/true
else 
  action "Nginx is not running" /bin/false
  /application/nginx/sbin/nginx
  action "starting Nginx" /bin/true
fi 
②通过进程:
#!/bin/sh
. /etc/init.d/functions
port=`ps -ef|grep nginx|grep -v grep|wc -l`
if [ $port -ge 2 ];then
  action "Nginx is running" /bin/true
else 
  action "Nginx is not running" /bin/false
  /application/nginx/sbin/nginx
  action "starting Nginx" /bin/true
fi
③wget返回内容:
#!/bin/sh
. /etc/init.d/functions
port=`wget -T 5 --spider http://172.19.5.8 &>/dev/null`
if [ $? -eq 0 ];then
  action "Nginx is running" /bin/true
else 
  /application/nginx/sbin/nginx
  action "starting Nginx" /bin/true
fi
④curl返回值200:
#!/bin/sh
. /etc/init.d/functions
port=`curl -s -I -w "%{http_code}\n" 172.19.5.8 -o /dev/null`
if [ "$port" == "200" ];then
  action "Nginx is running" /bin/true
else 
  /application/nginx/sbin/nginx
  action "starting Nginx" /bin/true
fi

13、企业Shell面试题13:监控Memcached缓存服务是否正常

监控Memcached缓存服务是否正常,模拟用户(web客户端)检测。
使用nc命令加上set/get来模拟检测。 

14、企业Shell面试题14:开发脚本入侵检测与报警案例

面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次。

15、企业Shell面试题15:开发Rsync服务启动脚本案例

#!/bin/bash
. /etc/init.d/functions
port=`netstat -lntup|grep 873|wc -l`
if [ ! -f /etc/rsyncd.conf ]
  then
    action "rsync配置" /bin/false
  exit 1
elif [ ! -f /usr/bin/rsync ]
    then
       action "rsync命令" /bin/false
   exit
fi

case "$1" in
  start)
     if [ $port -lt 2 ];then
       rsync --daemon
       action "Starting rsync..." /bin/true
     else
       action "Rsync is running..." /bin/true
     fi
     ;;
  stop)
     if [ $port -ge 2 ];then
     killproc rsync
     action "Stopping rsync..." /bin/true
     else
     action "Rsync is not running..." /bin/true
     fi
     ;;
  restart)
     killproc rsync
     rsync --daemon
     action "Rsync is restarting." /bin/true
     ;;
  *)
     echo "USAGE:$0 {start|stop|restart}"
esac

 16、企业Shell面试题16:开发MySQL多实例启动脚本

开发MySQL多实例启动脚本:
已知MySQL多实例启动命令为:mysqld_safe --defaults-file=/data/3306/my.cnf&
停止命令为:mysqladmin -u root -poldboy123 -S/data/3306/mysql.sock shutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。

解答:

#!/bin/bash
. /etc/init.d/functions
Port="3306"
Myuser="root"
Mypass="123456"
Mysock="/data/$Port/mysql.sock"
function_start(){
if [ ! -e "$Mysock" ];then
   /application/mysql/bin/mysqld_safe --defaults-file=/data/$Port/my.cnf &>/dev/null &
   action "Starting MySQL..." /bin/true
else
   action "MySQL is running..." /bin/true
   exit
fi
}
function_stop(){
if [ ! -e "$Mysock" ];then
   action "MySQL is not running..." /bin/false
   exit
else
   action "Stopping MySQL..." /bin/true
   /application/mysql/bin/mysqladmin -u$Myuser -p$Mypass -S $Mysock shutdown
fi
}
function_restart(){
  function_stop 
  sleep 2
  function_start
  action "MySQL is restarting..." /bin/true
}

case "$1" in
  start)
       function_start
     ;;
  stop)
       function_stop
     ;;
  restart)
       function_restart
     ;;
  *)
     echo "USAGE: /data/$Port/mysql{start|stop|restart}"
esac

17、企业Shell面试题17:破解RANDOM随机数案例

已知下面的字符串是通过RANDOM随机数变量md5sum后,再截取一部分连续字符串的结果,请破解这些字符串对应的使用md5sum处理前的RANDOM对应的数字?
21029299
00205d1c
a3da1677
1f6d12dd
890684b

解答:生成密码字典

#!/bin/sh
for i in {0..32767}
do
  screct=`echo $i|md5sum`
  echo "$i $screct" >>/tmp/file.txt
done

过滤出对应密码字符串

[root@m01 ~]# egrep '21029299|00205d1c|a3da1677|1f6d12dd|890684b' /tmp/file.txt
1346 00205d1cbbeb97738ad5bbdde2a6793d  -
7041 1f6d12dd61b5c7523f038a7b966413d9  -
10082 890684ba3685395c782547daf296935f  -
25345 a3da1677501d9e4700ed867c5f33538a  -
25667 2102929901ee1aa769d0f479d7d78b05  -

18、企业Shell面试题18:批量检查多个网站地址是否正常

企业面试题:批量检查多个网站地址是否正常
要求:
1、使用shell数组方法实现,检测策略尽量模拟用户访问。
2、每10秒钟做一次所有的检测,无法访问的输出报警。
3、待检测的地址如下
http://blog.oldboyedu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7

解答:

#!/bin/sh
. /etc/init.d/functions
Url=(
http://blog.oldboyedu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
for i in ${Url[*]}
do
  port=`curl -s -I -w "%{http_code}\n" $i -o /dev/null`
  if [ $port -eq 200 ];then
    action "$i" /bin/true
  else
    action "$i" /bin/false
  fi
done

19、企业Shell面试题19:单词及字母去重排序案例

用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
the squid project provides a number ofresources to assist users design,implement and support squid installations.Please browse the documentation and support sections for more infomation,byoldboy training.

解答:

20、企业Shell面试题20:开发脚本管理服务端LVS案例

请在LVS负载均衡主节点上,开发管理LVS服务的脚本ip_vs。
实现:利用ipvsadm可以启动并配置好LVS服务,脚本形式:/etc/init.d/lvs{start|stop|restart}

 解答:

  

21、企业Shell面试题21:LVS节点健康检查及管理脚本案例

请在LVS负载均衡主节点上,模拟keepalived健康检查功能管理LVS节点,
当节点挂掉从服务器池中剔除,好了再加到服务器池中来。 

解答:

22、企业Shell面试题22:LVS客户端配置脚本案例

请在LVS客户端节点上,开发LVS客户端设置VIP以及抑制ARP的管理脚本
实现:/etc/init.d/lvsclient{start|stop|restart}

解答:

23、企业Shell面试题23:模拟keepalved软件高可用案例

请在LVS服务端备用节点上,模拟keepalved vrrp功能,监听主节点,如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务(提醒:注意ARP缓存),提示此题要借助19.1.21的功能

解答:

24、企业Shell面试题24:Nginx负载节点状态监测案例

开发通过Web界面展示监控Nginx代理节点状态,效果图如下,当节点宕机时,以红色展示,当节点正常时以绿色展示

  

解答:

25、企业Shell面试题30:企业代码上线发布系统案例

写一套简单的企业代码上线发布系统案例,利用SVN对代码及配置文件进行管理,在办公室服务器上从svn取出指定版本的代码和配置,发布到IDC机房分发机服务器上,在分发服务器或者负载均衡器上或者应用服务器本地实现代码平滑发布、上线、回滚脚本(具体设计请参考课堂讲解的企业代码发布方案)。

解答:

 

26、企业Shell面试题26:51CTO博文爬虫案例

获取51CTO博客列表倒序排序考试题
老男孩教育培训机构需求:需求入下:
请把http://oldboy.blog.51cto.com地址中的所有博文,按照时间倒序列表如下:
2013-09-13运维就是一场没有硝烟的战争
http://oldboy.blog.51cto.com/2561410/1296694
2016-04-17运维人员写项目方案及推进项目的基本流程思路
http://oldboy.blog.51cto.com/2561410/1764820
附加:高级要求:
生成html页面,并设置超链接。
结果如下:
http://oldboy.blog.51cto.com/2561410/1862041

 解答:

#!/bin/sh
Uri="http://blog.51cto.com/oldboy/p"
Html_list=/tmp/html/oldboy_blog.html
[ ! -d /tmp/html ] && mkdir -p /tmp/html

for i in {1..29}
  do
  curl -s $Uri$i|grep -A 5 "time fl"|sed '/^.*zan fr.*/,+2d'|sed 's#^--$#<br>#g'|sed 's#<a.*fl">发布于:#<a>#g'|sed 's#<sp.*an>##g' >>$Html_list
  echo '<br>' >>$Html_list
done

 

文章转载自:http://www.pythonheidong.com/blog/article/2870/

猜你喜欢

转载自www.cnblogs.com/xiongbatianxiaskjdskjdksjdskdtuti/p/11356767.html