码迷,mamicode.com
首页 > 系统相关 > 详细

Shell之企业实际工作案例

时间:2018-03-30 16:28:48      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:shell

案例一:

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

监控脚本内容:

[root@nfs01 scripts]# cat web-file.sh
#!/bin/bash
##############################################################
# File Name: web-file.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
Email='155555@163.com'
Find_file='find /tmp/ -type f'
Dir='/opt/web_file.log'
Old_file=(`cat $Dir|sed -r 's#[a-Z,0-9]+  (.*)#\1#g'|paste -s`)
New_file=(`$Find_file|paste -s`)
function check_md5(){
  if [ ! -f "$Dir" ];then
     md5sum $Find_file >>$Dir
  else
     md5sum -c "$Dir" >/opt/check.log 2>&1
     if [ $? -ne 0 ];then
      mail -s 'Html modify' $Email </opt/check.log
     fi
  fi
}
function check_file(){
  if [ "${#Old_file[*]}" != "${#New_file[*]}"  ];then
     echo "$New_file"|mail -s "Html modify" $Email
  fi
}
function main(){
  check_md5
  check_file
}
main


案例二:

编写rsync的服务管理脚本

脚本内容:

#!/bin/bash
##############################################################
# File Name: web-file.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
. /etc/init.d/functions
 
if [ ! -f /etc/rsyncd.conf ];then
  action 'rsyncd.conf not exists' /bin/false
  exit
fi
start_cmd='rsync --daemon'
stop_cmd='pkill rsync'
port_cmd=`ss -tunlp|grep 873|wc -l`
START() {
    if [ "$port_cmd" -eq 2 ];then
      action 'rsync service is already exists' /bin/true
      exit
    else
      $start_cmd
      action 'rsync started' /bin/true
    fi
}
STOP() {
    $stop_cmd
    action 'rsync is stoped' /bin/true
}
RESTART() {
    $stop_cmd
    action 'rsync is stoped' /bin/true
    sleep 1
    $start_cmd
    action 'rsync is started' /bin/true
}
case $1 in
start)
    START
    ;;
stop)
    STOP
    ;;
restart)
    RESTART
    ;;
esac


案例三:

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

使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率。

脚本内容:

#!/bin/bash
##############################################################
# File Name: memcache.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
. /etc/init.d/functions
date_cmd=`date +%F-%s`
port_cmd=`nmap 172.16.1.21 -p 11211|awk 'NR==6{print $2}'`
set_cmd=`printf "set key009 0 10 10\r\njiang12345\r\n"|nc 172.16.1.21 11211`
get_cmd=`printf "get key009 \r\n"|nc 172.16.1.21 11211|tr -d '\r'|awk 'NR==2{print $1}'`
restart_cmd=`systemctl restart memcached.service`
if [ "$port_cmd" != "cloesd" ];then
      if [ "$get_cmd" != "jiang12345" ];then
          action 'memcache get key is successfull' /bin/true
      fi
   else
      $restart_cmd
fi


案例四:

编写nginx服务管理脚本,并监控web服务是否正常

管理服务脚本:

#!/bin/bash
##############################################################
# File Name: nginx.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
. /etc/init.d/functions
start_cmd='/application/nginx/sbin/nginx'
stop_cmd='pkill nginx'
restart_cmd='/application/nginx/sbin/nginx -s reload'
if [ $# -ne 1 ];then
    echo "please input $0 start|stop|restart"
    exit
fi
case $1 in 
1|start|START)
   $start_cmd
   action 'nginx started' /bin/true
;;
stop|STOP)
   $stop_cmd
   action 'nginx stoped' /bin/true
;;
restart|RESTART)
   $restart_cmd
   action 'nginx restarted' /bin/true
;;
esac


监控web服务脚本:

#!/bin/bash
##############################################################
# File Name: monitor.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
start_cmd='/application/nginx/sbin/nginx'
stop_cmd='pkill nginx'
port_cmd=`ss -tunlp|grep 80|wc -l`
curl_cmd=`curl -s -I 10.0.0.31 -w "%{http_code}\n" -o /dev/null`
if [ "$port_cmd" -ne 1 ];then
    $stop_cmd
    sleep 2
    $start_cmd
      if [ "$curl_cmd" -ne 200 ];then
        echo "$curl_cmd"|mail -s "web service failed" 155555@163.com
      fi
fi


案例四:

实现对MySQL数据库进行分库备份

脚本内容:

#!/bin/bash
##############################################################
# File Name: monitor.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018
# Description:
##############################################################
start_cmd='/application/nginx/sbin/nginx'
stop_cmd='pkill nginx'
port_cmd=`ss -tunlp|grep 80|wc -l`
curl_cmd=`curl -s -I 10.0.0.31 -w "%{http_code}\n" -o /dev/null`
if [ "$port_cmd" -ne 1 ];then
    $stop_cmd
    sleep 2
    $start_cmd
      if [ "$curl_cmd" -ne 200 ];then
        echo "$curl_cmd"|mail -s "web service failed" 155555@163.com
      fi
fi


案例五:

利用for循环打印下面字符串中字母数小于6的单词

I am oldboy teacher welcome to oldboy training class.

脚本内容:

#!/bin/bash
 
for i in I am oldboy teacher welcome to oldboy training class
  do
    if [ "${#i}" -gt 6 ];then
      echo $i:${#i}
    else
      continue
    fi
done


案例六:

批量创建多个账号并生成随机密码,创建成功后要求账号名称和对应密码记录到文件中

脚本内容:

技术分享图片

技术分享图片

案例七:

[LVS主节点]手工开发ipvsadm管理lvs的脚本ip_vs

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

脚本内容:

#!/bin/bash
##############################################################
# File Name: lvs.sh
# Version: V1.0
# Author: da ya
# Organization: 12345@qq.com
# Created Time : 2018-03-24 00:47:17
# Description:
##############################################################
. /etc/init.d/functions
ip_cmd=`ip addr show eth0|grep 10.0.0.13|wc -l`
START() {
   if [ "$ip_cmd" -eq 0 ];then
      ip addr add 10.0.0.13/24 dev eth0
      ipvsadm -C
      ipvsadm --set 30 5 60
      ipvsadm -A -t 10.0.0.13:80 -s wrr -p 20
      ipvsadm -a -t 10.0.0.13:80 -r 10.0.0.17:80 -g -w 1
      ipvsadm -a -t 10.0.0.13:80 -r 10.0.0.18:80 -g -w 1
      action 'LVS is started' /bin/true
   else
      action 'LVS is already exists' /bin/true
   fi
}
STOP() {
      ipvsadm -C
      ip addr del 10.0.0.13/24 dev eth0
      action 'LVS is stoped' /bin/true
}
RESTART() {
      ipvsadm -C
      ip addr del 10.0.0.13/24 dev eth0
      action 'LVS is stoped' /bin/true
      ip addr add 10.0.0.13/24 dev eth0
      ipvsadm --set 30 5 60
      ipvsadm -A -t 10.0.0.13:80 -s wrr -p 20
      ipvsadm -a -t 10.0.0.13:80 -r 10.0.0.17:80 -g -w 1
      ipvsadm -a -t 10.0.0.13:80 -r 10.0.0.18:80 -g -w 1
      action 'LVS is restarted' /bintrue
}
 
case $1 in
 start)
   START
   ;;
 stop)
   STOP
   ;;
 restart)
   RESTART
   ;;
 *)
   echo 'Usage: input { start|stop|restart }'
   ;;
esac

Shell之企业实际工作案例

标签:shell

原文地址:http://blog.51cto.com/13520772/2092979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!