企业Shell面试题及企业运维实战共30道案例


企业Shell面试题及企业运维实战共30道案例

 

1.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

 

解答:

方法1:

#!/bin/bash

DIR=/oldboy

. /etc/init.d/functions

[ ! -d $DIR ] || mkdir /oldboy -p

 

cd $DIR &>/dev/null || {

        echo "cd $DIR fail"

        exit 2

}

 

cd $DIR && {

        for i in {1..10}

        do

          touch $(echo $RANDOM|md5sum|tr [0-9] [a-z]|cut -c 1-10)_oldboy.html

          action 'file  create success' /bin/true

          exit 0

        done

}

echo "file  create fail"

 

  

 

方法2 :

  

#!/bin/bash

dir=/oldboy

[ -d $dir ] || mkdir -p $dir

 

cd $dir && \

for i in {1..10}

  do

    shu=$(uuidgen|tr '0-9-' 'a-z'|cut –c 1-10)

    touch ${shu}_oldboy.html

done

 

 

 

 

 

 

1.2 批量改名特殊案例

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

解答:

 

#!/bin/bash

dir=/oldboy

 

cd $dir && \

 

for i in $(ls $dir)

  do

    shu=$(echo $i |cut -c 1-10)

    mv  ${shu}_oldboy.html  ${shu}_oldgirl.HTML

done

 

  

 

 

 

1.3 批量创建特殊要求用户案例

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

不用for循环的实现思路:http://user.qzone.qq.com/49000448/blog/1422183723

解答:

 

#!/bin/bash

. /etc/init.d/functions

file=/tmp/user_pass.txt

for i in oldboy{01..10}

  do

    useradd $i &>/dev/null

     if [ $? -eq 0 ];then

       action "$i 创建成功"  /bin/true

       mima=$(echo $RANDOM|md5sum|cut -c 1-10)

       echo $mima|passwd --stdin $i &>/dev/null

       if [ $? -eq 0 ];then

         action "$i 设置密码成功"  /bin/true

         echo

       else

         action "$i 设置密码失败" /bin/false

         echo

         exit 2

       fi

         echo ${i}:$mima >>/tmp/user_pass.txt

     else

       action "$i 已经存在"   /bin/false

     fi

done

 

  

 

 

 

 

1.4 扫描网络内存活主机案例

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

解答:

 

nmap -sT 10.0.0.0/24  扫描网络

#!/bin/sh
#
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
function IP_count(){
  for n in 10.0.0.{0..255}
   do
    IP_check=`nmap -sP $n|grep "Host is up"|wc -l`
    if [ ${IP_check} -eq 1 ];then
      action "$n" /bin/true
      let i+=1
    fi
  done
}
function main(){
  IP_count
  echo "The total number of online IP Addresses is " $i
}
main

 

      

 

 

#!/bin/bash

source /etc/init.d/functions

 

for i in 10.0.0.{1..254}

  do

    {

    ping -c 1 -W 1 $i  &>/dev/null

    if [ $? -eq 0 ]

      then

        action "$i" /bin/true

    fi

    sleep 10

    } &             #并发执行 ping命令,同时开启254个进程

done

 

  

 

                

1.5 解决DOS攻击生产案例

写一个Shell脚本解决DOS攻击生产案例。

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

解答:

 

#!/bin/bash
web_log=/application/access.log

awk '{i[$1]++}END{for (IP in i) print IP,i[IP]}' $web_log|awk '$2>1000 {print $1,$2}'|column -t|sort -rnk2 >/tmp/web_log.txt

IP=$(awk '{i[$1]++}END{for (IP in i) print IP,i[IP]}' $web_log |awk '$2>1000{print $1}')

 echo "超过并发连接数IP"

 cat /tmp/web_log.txt

for i in $IP

  do

 iptables -I INPUT -s $i -j DROP

done

 

  

 

 

1.6 MySQL数据库分库备份

请实现对MySQL数据库进行分库备份,用脚本实现。

解答:

 

 

 

 

 

1.7 MySQL数据库分库分表备份

如何实现对MySQL数据库进行分库加分表备份,请用脚本实现。

解答:

 

 

 

1.8 筛选符合长度的单词案例

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

I am oldboy teacher welcome to oldboy trainingclass

解答:

 

#!/bin/bash

for i in I am oldboy teacher welcome to oldboy training class

 do

   if [ "${#i}" -le 6 ];then

  echo $i

   fi

done

 

  

 

 

1.9 MySQL主从复制异常监控案例

开发一个守护进程脚本每30秒监控MySQL主从复制是否异常(包括不同步以及延迟),如果异常,则发送短信并发送邮件给管理员存档。提示:如果没主从复制的环境,可以把下面文本放到文件里读取来模拟主从复制状态:

解答:

 

 

 

 

1.10 比较整数大小经典案例

综合实战案例:开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。用条件表达式(禁止if)进行判断并以屏幕输出的方式提醒用户比较结果。

注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数不对给予提示。

解答:

 

  1.  脚本传参

#!/bin/bash
[ $# -ne 2 ] && echo "USAGE: sh $0 int1 int2" && exit 2

n1=$1

n2=$2

expr $n1 + 1 &>/dev/null

[ $? -eq 2 ] && echo "the first is not int"  && exit 3

expr $n2 + 1 &>/dev/null

[ $? -eq 2 ] && echo "the second is not int"  && exit 3

[ "$n1" -gt "$n2" ] && echo "$n1 > $n2" && exit

[ "$n1" -eq "$n2" ] && echo "$n1 = $n2" && exit

echo "$n1 < $n2"

 

  

  2. red读入

 

#!/bin/bash
read -p "the first num:"  n1
read -p "the second num:" n2
[ -z "$n1" -o -z "$n2" ] && echo "请输入数据"  && exit 2
expr $n1 + 1 &>/dev/null
[ $? -eq 2 ] && echo "the first is not int"  && exit 3
expr $n2 + 1 &>/dev/null
[ $? -eq 2 ] && echo "the second is not int"  && exit 3
[ "$n1" -gt "$n2" ] && echo "$n1 > $n2" && exit
[ "$n1" -eq "$n2" ] && echo "$n1 = $n2" && exit
echo "$n1 < $n2"

 

 

 

  

 

 

1.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/bash

cat <<EOF

==================

echo

1.[install lamp]

2.[install lnmp]

3.[exit]

==================

EOF

read -p "pls input the num you want:" install

 

case $install in

  1)

   echo "start install lamp"

   if [ -x /server/scripts/lamp.sh ];then

      /server/scripts/lamp.sh

    else

      echo "/server/scripts/lamp.sh 没有执行权限"

   fi

   ;;

  2)

   echo "start install lnmp"

   if [ -x /server/scripts/lnmp.sh ];then

      /server/scripts/lnmp.sh

    else

      echo "/server/scripts/lamp.sh 没有执行权限"

   fi

   ;;

  3)

  exit

   ;;

  *)

  echo "请选择[1-3]"

esac

 

  

 

 

 

 

 

 

1.12 Web及MySQL服务异常监测案例

用if条件语句实现对Nginx Web服务以及MySQL数据库服务是否正常进行检测,如果服务未启动,则启动相应服务。

解答:

    1. nginx异常检测

#!/bin/bash
. /etc/init.d/functions

nginx_server=$(netstat -lntup|grep -wc nginx)

 

 

if [ $nginx_server -eq 0 ];then

    echo "nginx 没有运行,启动nginx"

    /application/nginx/sbin/nginx

  if [ $? -eq 0 ];then

    action  'nginx start' /bin/true

  else

    action  'nginx start' /bin/falese

  fi

    else

    echo "nginx 已经运行" 

fi

##状态码

http=$(curl -I 10.0.0.5 -s -w %{http_code} -o /dev/null)

if [ "$http" -eq 200 ];then

    echo -e "状态码:$http\nnginx 访问正常"

  else

    echo -e "状态码:$http\nnginx 访问失败"

fi

 

  

 2. mysql异常检测

#!/bin/bash

. /etc/init.d/functions

mysqld=$(netstat -lntup|grep -c mysqld)

 

function db_Start (){

  /etc/init.d/mysqld start &>/dev/null

  if [ $mysqld -eq 0 ];then

    action "Starting MySQL. SUCCESS!" /bin/true

  else

    action Starting MySQL. fail!

  fi

}

 

  if [ $mysqld -eq 0 ];then

    echo "MySQL not running Starting MySQL"

    db_Start

  else

    echo "MySQL  running!"

  fi

 

  

 

1.13 监控Memcached缓存服务是否正常

监控Memcached缓存服务是否正常,模拟用户(web客户端)检测。

使用nc命令加上set/get来模拟检测。
解答:

 

#!/bin/bash

. /etc/init.d/functions

cache_server=$(netstat -lntup|grep -wc memcached)

 

#判断是否运行

if [ "$cache_server" -eq 0 ];then

    echo "mecached 没有运行,启动memcached"

   systemctl start memcached.service

    if [ $? -eq 0 ];then

     action 'memcached start' /bin/true

    fi

  else

   echo "memcached 运行正常"

fi

 

#测试读写

set_zhi=$(uuidgen|cut -c -8)

set=${#set_zhi}

 

printf "set key008 0 0 $set\r\n${set_zhi}\r\n"|nc 127.0.0.1 11211 &>/dev/null

if [ $? -eq 0 ];then

   get=$(printf "get key008\r\n"|nc 127.0.0.1 11211|awk 'NR==2'|tr -d '\r')

  else

    echo "set 赋值失败"

    exit 2

fi

 

 

if [ "$set_zhi" == "$get" ];then

    echo "memcached 读写正常"

  else

    echo "memcached 读写失败"

fi

 

  

 

 

 

 

1.14 开发脚本入侵检测与报警案例

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

解答:

 

#!/bin/bash

html_dir=/var/www/html

file_data=/tmp/html_file.txt

md5_data=/tmp/html_md5zhi.txt

. /etc/init.d/functions

status=$1

case $status in

  1)

  find $html_dir -type f >$file_data

  [ $? -eq 0 ] && action '文件库生成:$file_data' /bin/true

  find $html_dir -type f|xargs md5sum >$md5_data

  [ $? -eq 0 ] && action 'md5值生成:$md5_data' /bin/true

  ;;

  2)

while true

do

  md5_zhi=$(md5sum -c $md5_data 2>/dev/null |grep  "失败")

  if [ -n "$md5_zhi" ];then

   echo "文件内容被修改或删除的文件:$md5_zhi  $(date +%F_%H:%M)"

 #echo "文件内容被修改或删除的文件:$md5_zhi  $(date +%F_%H:%M)" |mail -s "${html_dir}恶意篡改" 243541755@qq.com

    sleep 30

  fi

 

  hthl_file=$(find $html_dir -type f >/tmp/html_file-yanzheng.txt)

  file_yanzheng=$(diff /tmp/html_file*)

  if [ -n "$file_yanzheng" ];then

   echo "文件被改动: $file_yanzheng   $(date +%F_%H:%M)"

    sleep 30

  fi

done

  ;;

  *)

  echo -e "$0:{1|2}\n1:刷新文件库和md5值\n2:实时监控站点目录$html_dir"

esac

 

  

 

 

 

1.15 开发Rsync服务启动脚本案例

写网络服务独立进程模式下Rsync的系统启动脚本,例如:/etc/init.d/rsyncd {start|stop|restart}。
要求:
1.要使用系统函数库技巧。
2.要用函数,不能一坨SHI的方式。
3.可被chkconfig管理。

解答:

 

 

#!/bin/bash 
source /etc/init.d/functions

rsync=$(netstat -lntup|grep -wc rsync)

[ ! -f /etc/rsyncd.conf ] && echo "rsync /etc/rsyncd.conf not" && exit 2

status=$1

case $status in

   start)

     if [ $rsync -ne 0 ];then

       echo "rsync 正在运行"

     else

       rsync --daemon

       action 'rsync start' /bin/true

     fi

     ;;

    stop)

       pkill rsync

     if [ $? -eq 0 ];then

       action 'rsync stop' /bin/true

     else

       echo "停止失败,rsync没有运行"

     fi

     ;;

  restart)

     if [ $rsync -ne 0 ];then

        pkill rsync

        if [ $? -eq 0 ];then

          sleep 1

          rsync --daemon

          action 'rsync restart' /bin/true

        else

          action 'rsync stop' /bin/false

        fi

        else

        echo "重启失败,rsync没有运行"

     fi

     ;;

        *)

   echo "$0:{start|stop|restart}"

esac

 

  

 

 

 

 

 

 

1.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语句等实现。

解答:

 

 

 

 

1.17 天津项目学生实践抓阄案例

好消息,培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。

因此需要挑选学生,因此需要一个抓阄的程序:

要求:

1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。

2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入
 解答:

echo  $(($RANDOM%99+1))  生成1-99随机数
 #!/bin/bash
log_file=/tmp/zhajiu.txt

[ -f $log_file ] && rm -f "$log_file" || touch "$log_file"

[ -f $log_file ] && > $log_file || touch $log_file

function wait()

{  

    echo -en '\033[33m随机抽取中,请稍等.\033[0m';

    for ((i=0;i<3;i++))

     do

      echo -n ".";sleep 1

    done

}

 

function name()

{

while true

do

  read  -p  "请输入学生姓名:" name

if [ "$name" == "exit" ];then

    echo -en '\033[33m正在退出程序,拜拜.\033[0m';

    for ((i=0;i<3;i++))

     do

      echo -n ".";sleep 1

      done

     echo

     exit

fi

  name_test=$(egrep -wc $name $log_file)

  if [ $name_test -eq 0 ];then

    name_panduan=1

    break

  else

    echo "已经参加,请重新输入"

    continue

  fi

done

}

function shuzi()

{

while true

do

  name

  if [ $name_panduan -eq 1 ];then

    shu=$(echo $(($RANDOM%99+1)))

    shuzi_test=$(egrep -wc $shu $log_file)

    if [ $shuzi_test -eq 0 ] ;then

     echo "$name:$shu" >> $log_file

     wait

     echo  "                         "

      echo

     echo -e "\033[34m抓阄结果\033[0m"

     echo  "$name:$shu"

     echo

     echo -e "\033[34m获奖名单\033[0m"

     echo -e "\033[34m------------------------\033[0m"

      awk -F ":" "{print $2}" /tmp/zhajiu.txt|column -t|sort -rnk 2 -t:|head -3

     echo -e "\033[34m------------------------\033[0m"

     echo

     echo

    fi

  fi

done

}

echo

echo

echo -e "\033[33m

     --------------- 好消息,好消息------------------

        学生外出企业项目实践机会(第6次)来了(本月中旬)

         名额有限,队员限3人(班长带队)。

     --------------- 同学们来抓阄吧------------------\033[0m"

echo

echo

echo  -e "                                    \033[31m退出程序请输入:exit\033[0m"

echo

 

 

shuzi

 


  

 

 

 

 

1.18 破解RANDOM随机数案例

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

21029299

00205d1c

a3da1677

1f6d12dd

890684b

解答:

 

  生成字典包

for i in {0..32767}

 do

  echo "${i}:$(echo $i|md5sum)" >>/tmp/zidian.txt

done

 

  

 

 

  跑字典

[root@backup oldboy]# egrep "21029299|00205d1c|a3da1677|1f6d12dd|890684b"  /tmp/zidian.txt

1346:00205d1cbbeb97738ad5bbdde2a6793d  -

7041:1f6d12dd61b5c7523f038a7b966413d9  -

10082:890684ba3685395c782547daf296935f  -

25345:a3da1677501d9e4700ed867c5f33538a  -

25667:2102929901ee1aa769d0f479d7d78b05  -

 

  

 

 

 

1.19 批量检查多个网站地址是否正常

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

要求:

1、使用shell数组方法实现,检测策略尽量模拟用户访问。

2、每10秒钟做一次所有的检测,无法访问的输出报警。

3、待检测的地址如下

http://blog.oldboyedu.com

http://blog.etiantian.org

http://oldboy.blog.51cto.com

http://10.0.0.7

解答:

 

curl -I http://oldboy.blog.51cto.com -s -w %{http_code} -o /dev/null  -L 

 -L 跟随跳转

 

#!/bin/bash

. /etc/init.d/functions

 

#定义url数组

checa_count=0

url_list=(

  http://blog.oldboyedu.com

  http://blog.etiantian.org

  http://oldboy.blog.51cto.com

  http://10.0.0.7

)

 

function wait()

{  

    echo -n '3秒后,执行检查URL操作.';

    for ((i=0;i<3;i++))

     do

      echo -n ".";sleep 1

    done

}

function checa_url()

{

 wait

 echo

 for ((i=0;i<"$(echo ${#url_list[*]})";i++))

 do

  status_code=$(curl -I ${url_list[$i]} -s -w %{http_code} -o /dev/null  -L)

  if [ $status_code -eq 200 ];then

   action "${url_list[$i]}"  /bin/true

   else

   action "${url_list[$i]}"  /bin/false

  fi

done

   ((checa_count++))

}

function main()

{

  while true

  do

    checa_url

    echo "-----Checa Count:${checa_count}-----"

    sleep 10

  done

}

main

 

  

 

 

1.20 单词及字母去重排序案例

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.

课堂实战考察某企业shell面试考试题

http://oldboy.blog.51cto.com/2561410/1686891

解答:

 

[root@backup scripts]# cat /tmp/zimu.txt

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.

 

 

 

 

1.21 开发脚本管理服务端LVS案例

请在LVS负载均衡主节点上,开发管理LVS服务的脚本ip_vs。

实现:利用ipvsadm可以启动并配置好LVS服务,脚本形式:/etc/init.d/lvs{start|stop|restart}

解答:

 

 

 

 

1.22 LVS节点健康检查及管理脚本案例

请在LVS负载均衡主节点上,模拟keepalived健康检查功能管理LVS节点,

当节点挂掉从服务器池中剔除,好了再加到服务器池中来。

解答:

 

 

 

 

1.23 LVS客户端配置脚本案例

请在LVS客户端节点上,开发LVS客户端设置VIP以及抑制ARP的管理脚本

实现:/etc/init.d/lvsclient{start|stop|restart}

解答:

 

 

 

 

 

1.24 模拟keepalved软件高可用案例

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

解答:

 

 

 

1.25 编写正(或长)方形图形案例

请用shell或Python编写一个正(或长)方形,接收用户输入的数字。

解答:

#!/bin/bash

function integer()

{

 expr 1 + $1 &>/dev/null

 if [ $? -eq 2 ];then

   echo "usage $0:请输入整数"

   exit

 fi

}

  read -p "please enter a number:" num

   integer $num

  for ((i=1;i<=$num;i++))

  do

    for ((m=1;m<=$num;m++))

    do

      echo -n ""

    done

    echo

   done

 

  

 

 

1.26 编写等腰三角形图形字符案例

请用shell或Python编写一个等腰三角形,接收用户输入的数字。

解答:

#!/bin/bash

function integer()

{

 expr 1 + $1 &>/dev/null

 if [ $? -eq 2 ];then

   echo "usage $0:请输入整数"

   exit

 fi

}

  read -p "please enter a number:" num

   integer $num

  for ((i=1;i<=num;i++))

  do

    for ((m=$num-$i;m>0;m--))

    do

      echo -n " "

    done

     for ((h=1;h<=$((2*$i-1));h++))

     do

        echo -n '*'

     done

    echo

   done

 

  

 

 

 

 

1.27 编写直角梯形图形字符案例

请用shell或Python编写一个画直角梯形程序,接收用户输入的参数n(n>2),m。

解答

 

#!/bin/bash
if [ $# -ne 2 ];then

  echo "USAGE:$0 num1(>2) num2"

  exit

fi

 

for n in `seq $1 $2`

do

  for ((m=1;m<=$n;m++))

  do

    echo -n "*"

  done

  echo

done

 

  

 

 

 

 

 

 

 

 

1.28 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

解答:

grep 取

#!/bin/bash

url="http://blog.51cto.com/oldboy/p"

html=/tmp/blog.html

[ -f $html ] && rm -f $html

for i in {1..29}

  do

   curl -s ${url}$i |grep -A 1 "\"tit\"" |sed -r 's#<span.*span>#<br>#' >>$html

done

 

  

 

awk 取

#!/bin/bash

LANG="zh_CN.UTF-8"

url="http://blog.51cto.com/oldboy/p"

html=/tmp/blog.html

[ -f /tmp/time.txt ] &&  rm -f /tmp/time.txt

[ -f /tmp/url.txt ] &&  rm -f /tmp/url.txt

[ -f /tmp/name.txt ] &&  rm -f /tmp/name.txt

[ -f /tmp/blog.html ] &&  rm -f /tmp/blog.txt

for i in 1

  do

   curl -s ${url}$i|awk  -F ":|<" '/<a href="javascript:;" class="time fl/ {print $3}' >>/tmp/time.txt

   curl -s ${url}$i|awk -F "\"" '/<a class="tit" href="/{print $4}' >>/tmp/url.txt

   curl -s ${url}$i|awk '/ <\/a>/{print $1}'|grep -v "</a>" >>/tmp/name.txt

   paste -d ' ' /tmp/time.txt /tmp/url.txt /tmp/name.txt >>/tmp/blog.html

done

 

     paste –d

paste命令用于将文本文件或标准输出中的内容粘贴到新的文件,它可以将来自于不同文件的数据粘贴到一起,形成新的文件

 

 

 

 

 

 

 

1.29 Nginx负载节点状态监测案例

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

 

解答:

#!/bin/bash

html_file=/application/nginx/html/index.html

port=80

web_IP=(

172.16.1.9

172.16.1.18

)

 

function web_check()

{

  for ((i=0;i<"$(echo ${#web_IP[*]})";i++))

  do

   web_Status[$i]=$(nmap ${web_IP[$i]} -p $port|grep -c open)

  done

}

function html_tihuan()

{

  if [ "${web_Status[0]}" -eq 0 ];then

    #echo ${web_IP[0]} Down!

     sed '27s#green#red#;27s#OK!#Down!#' -i $html_file

  else

    #echo ${web_IP[0]} OK!

     sed '27s#red#green#;27s#Down!#OK!#' -i $html_file

  fi

 

  if [ "${web_Status[1]}" -eq 0 ];then

    #echo ${web_IP[1]} Down!

     sed '33s#green#red#;33s#OK!#Down!#' -i $html_file

  else

    #echo ${web_IP[1]} OK!

     sed '33s#red#green#;33s#Down!#OK!#' -i $html_file

  fi

}

function main()

{

  while true

   do

   web_check

   html_tihuan

  done

}

   main

 

  

 

 

html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<meta http-equiv="refresh" content="1">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>

  h4{text-align: center;}

</style>

</head>

<body>

<h4> - </h4>

<h4> 老男孩教育43期 </h4>

<h4> web节点健康检查 </h4>

<table width="400" height="200" border="1" align="center">

  <tr align="center" valign="middle">

    <td bgcolor="gray">主机名</td>

    <td bgcolor="gray">IP地址</td>

    <td bgcolor="gray">端口</td>

    <td bgcolor="gray">状态</td>

      

  </tr>

  <tr align="center" valign="middle">

    <td >web01</td>

    <td >172.16.1.9</td>

    <td >80</td>

    <td bgcolor="green">OK!</td>

  </tr>

  <tr align="center" valign="middle">

    <td >web04</td>

    <td >172.16.1.18</td>

    <td >80</td>

    <td bgcolor="green">OK!</td>

  </tr>

 

</table>

<body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1522434774423&di=bf83edcd87b36ce3b6572231a1749e9a&imgtype=0&src=http%3A%2F%2Fwww.blended-html.com%2Fbackground-images%2Fbi-background-frame.png"></body>

</html>

  

1.30 企业代码上线发布系统案例

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM