设为首页 收藏本站
查看: 1267|回复: 0

shell-脚本集合

  [复制链接]

尚未签到

发表于 2018-8-16 13:50:13 | 显示全部楼层 |阅读模式
  shell-脚本集合
  shell-脚本集合2
  1 web server status
  

  


  • #!/bin/bash
  • #test web server http status
  • #20111011 by dongnan

  • for i in $(cat test);do
  •    #echo $i
  •    if curl -IL -m 2 $i 2>&1 | grep '200 OK' > /dev/null ;then
  •       echo "$i status is 200"
  •    elif curl -IL -m 2 $i 2>&1 | grep '403 Forbidden' > /dev/null ;then
  •       echo "$i status is 403"
  •    else
  •       echo "$i is bad"
  •    fi
  • done
  


  • #./domain.sh
  • www.aa.com status is 200
  • www.gunduzi.sb is bad
  • www.sx.sb is bad
  

  2 test url 502
  

  


  • #!/bin/bash
  • #20111015 by dongnan
  • #test the url 502

  • for((i=1;i&1 | grep '502' > /dev/null ;then
  •         echo "echo $i 502"
  •     fi
  •     sleep 1;
  • done
  

  3 test cdn ip loop
  


  • #!/bin/bash
  • #20111101 by dongnan
  • #test the cdn ip loop


  • for((i=1;i> bbs_vhost.txt
  • while read line;do dig $line | grep -E 'IN.*A|IN.*CNAME'|grep -v '^;';done < bbs_vhost.txt

  • bbs.a.com.      956 IN  A   *.*.*.*
  • msn.bbs.a.com.  807 IN  CNAME   bbs.a.com.
  • bbs.a.com.      956 IN  A   *.*.*.*
  • bbsimg.a.com.   1460    IN  A   *.*.*.*
  • pq.a.com.       861 IN  A   *.*.*.*
  • wenda.a.com.        1794    IN  A   *.*.*.*
  


  5 批量上传ssh>  自动: 需要使用 expect 命令
  


  • cat auto_ssh_key.sh
  • #!/bin/bash
  • #auto ssh key
  • #20111125 by dongnan

  • #variables
  • hosts='2 3 4 5 6 7 8 9'
  • ip=10.100.100.
  • user=dn
  • password=mima

  • #function
  • 参考
  • auto_ssh_copy_id () {
  • expect -c &quot;set timeout -1;
  • spawn ssh-copy-id -i /home/dn/.ssh/id_rsa.pub $1;
  • expect {
  • *(yes/no)* {send -- yes\r;exp_continue;}
  • *assword:* {send -- $2\r;exp_continue;}
  • eof        {exit 0;}
  • }&quot;;
  • }

  • for i in $hosts;do
  •     echo &quot;${ip}$i&quot;
  • #    cat ~/.ssh/id_rsa.pub | ssh dn@${ip}$i &quot;mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat - >~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys&quot;
  •     auto_ssh_copy_id $user@${ip}$i $password
  • done
  

  6 批量部署 .bash_profile ,前提需要 ssh通过私匙登陆ssh服务器
  关于如何配置ssh key,请参考这里!
  


  • $ grep -E 'space.$|bbs.$' /etc/hosts | awk '{print $1}' > hosts

  • $ while read line;do echo $line;scp ~/.bash_profile dn@${line}:/home/dn/;done < hosts
  • 10.100.100.2
  • .bash_profile                                 100%  622     0.6KB/s   00:00
  • 10.100.100.3
  • .bash_profile                                 100%  622     0.6KB/s   00:00
  • 10.100.100.4
  • .bash_profile                                 100%  622     0.6KB/s   00:00
  • ......
  

  7 stdin and while
  


  • cat install.log  | while read line;do echo $line ; wc -c;done
  • while read line;do echo $line | wc -c;done < install.log
  

  8 自定义.bash_profile 文件!
  


  • #将以下行添加到.bash_profile
  • ip=$(/sbin/ifconfig eth1 | grep Bcast | sed -e 's/^.* addr:\(.*\) Bcast.*$/\1/')

  • if [ $ip ];then
  •     PS1=&quot;\[\e[32;1m\]$ip\[\e[0m\][\W]\$ &quot;
  •     export PS1
  • fi

  • echo &quot;[LOAD]&quot;;cat /proc/loadavg;echo &quot;[DISK]&quot;;df -Th
  

  9 统计用户登陆次数与 ip 地址
  


  • #!/bin/bash
  • #chek login user
  • #

  • #variables
  • user=`id -u`


  • #
  • if [ $user -ne 0 ];then
  •     echo &quot;must root !!&quot;
  •     exit 1
  • fi


  • for i in `ls /var/log/secure*`;do
  •     echo &quot;$i&quot;
  •     #cat /var/log/secure | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c
  •     cat &quot;$i&quot; | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c
  • done
  

  效果如下
  


  • ./login.sh
  • /var/log/secure
  •       6 dn 100.100.100.99
  • /var/log/secure.1
  •      15 dn 100.100.100.99
  

  10 统计bbs 文件夹大小,需要ssh-key 参考上面!
  


  • #!/bin/bash
  • #du -sh

  • #variables
  • ip=100.100.100.
  • #hosts='24'
  • hosts='22 23 24 25 26 27 28 29'

  • for host in $hosts;do
  •    echo ${ip}$host
  •    ssh ${ip}$host &quot;find /var/www/bbs/ -type d -name 'day*'  | xargs du -h | awk '{print \$1}'&quot;
  •    ssh ${ip}$host &quot;du -sh /var/www/bbs/&quot;
  • done
  

  #ssh 传递 awk 命令时 &quot;$&quot; 需要使用&quot;\&quot;转义字符,例如:
  


  • ssh ${ip}$host &quot;df -h | grep -E '^/dev.*(/|/usr|/var)$' | awk '{print \$5,\$6}'&quot;
  

  #统计硬盘使用情况
  


  • cat bbs_df.sh

  • #!/bin/bash
  • #vdisk usage
  • #20111129 by dongnan

  • #variables
  • ip=10.100.100.
  • #hosts='102'
  • hosts='102 103 104 105 106 107 108 109'

  • for host in $hosts;do
  •    echo -en &quot;${ip}$host &quot;
  •    ssh ${ip}$host &quot;df -h | grep -E '^/dev.*(/|/usr|/var)$' | awk '{print \$6,\$5}'&quot;
  • done
  

  11 haproxy 服务脚本
  


  • # cat /usr/local/sbin/haproxy.sh
  • #!/bin/bash
  • #haproxy command
  • #ver:0.1bate
  • #20111129 by dongnan

  • #/usr/local/haproxy/sbin/haproxy
  • #HA-Proxy version 1.4.18 2011/09/16
  • #Copyright 2000-2011 Willy Tarreau
  • #
  • #Usage : haproxy [-f ]* [ -vdVD ] [ -n  ] [ -N  ]
  • #        [ -p  ] [ -m  ]
  • #        -v displays version ; -vv shows known build options.
  • #        -d enters debug mode ; -db only disables background mode.
  • #        -V enters verbose mode (disables quiet mode)
  • #        -D goes daemon
  • #        -q quiet mode : don't display messages
  • #        -c check mode : only check config files and exit
  • #        -n sets the maximum total # of connections (2000)
  • #        -m limits the usable amount of memory (in MB)
  • #        -N sets the default, per-proxy maximum # of connections (2000)
  • #        -p writes pids of all children to this file
  • #        -de disables epoll() usage even when available
  • #        -ds disables speculative epoll() usage even when available
  • #        -dp disables poll() usage even when available
  • #        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.

  • #variables
  • haproxy_dir=/usr/local/haproxy/
  • haproxy_conf=${haproxy_dir}haproxy.cfg
  • haproxy_pid=${haproxy_dir}logs/haproxy.pid
  • haproxy_cmd=${haproxy_dir}sbin/haproxy
  • #test variables
  • #file $haproxy_dir; file $haproxy_conf; file $haproxy_cmd; file $haproxy_pid



  • if [ &quot;$#&quot; -eq &quot;0&quot; ];then
  •     echo &quot;usage: $0 {start|stop|restart}&quot;
  •     exit 1
  • fi

  • if [ &quot;$1&quot; = &quot;start&quot; ];then
  • #echo $1
  •     $haproxy_cmd -f $haproxy_conf
  • elif [ &quot;$1&quot; = &quot;stop&quot; ];then
  • #echo $1
  •     kill `cat $haproxy_pid`
  • elif [ &quot;$1&quot; = &quot;restart&quot; ];then
  • #echo $1
  •     $haproxy_cmd -f $haproxy_conf -st `cat $haproxy_pid`

  • else
  •    echo &quot;usage: $0 arguments only start and stop or restart !&quot;
  • fi
  

  12 cpu load 测试
  


  • $ cat load.sh

  • #! /bin/sh
  • # filename killcpu.sh
  • for i in `seq $1`
  • do
  •   echo -ne &quot;
  • i=0;
  • while true
  • do
  • ii=i+1;
  • done&quot; | /bin/sh &
  •   pid_array[$i]=$! ;
  • done
  • for i in &quot;${pid_array[@]}&quot;; do
  •   echo 'kill ' $i ';';
  • done

  • 使用方法很简单,参数3表示消耗3颗CPU的资源,运行后,会有一堆 kill 命令,方便 kill 进程:
  • [root@test02 ~]# ./killcpu.sh 3
  • kill  30104 ;
  • kill  30106 ;
  • kill  30108 ;
  

  13 haproxy 安装脚本
  


  • cat haproxy_install.sh

  • #!/bin/bash
  • #install haproxy
  • #20111207 by dongnan

  • #variables
  • dir=/usr/local
  • ha_dir=${dir}/haproxy
  • ha_cfg=${ha_dir}/haproxy.cfg
  • kernel=`uname -r | grep '2.6'`
  • pcre=$(rpm -qa | grep 'pcre' | wc -l)
  • echo &quot;$dir, $ha_dir, $ha_cfg, $kernel, $pcre&quot;


  • #check
  • if [ ! &quot;$kernel&quot; -o &quot;$pcre&quot; -lt &quot;2&quot; ];then
  •     echo -e &quot;the script need linux 2.6 kernel and pcre pcre-devel \nyou can usage 'yum install pcre pcre-devel' or 'rpm -ivh pcre-devel-6.6-2.el5_1.7.x86_64.rpm'&quot;
  •     exit 1
  • fi

  • #function

  • install_ha_cfg (){
  • #configure haproxy.cfg
  • #default configure file for test,but need your change the frontend server and backend server ip address,
  • #good luck!

  • echo '
  • global
  •     log 127.0.0.1   local0
  •     maxconn 4096              #最大连接数
  •     chroot /usr/local/haproxy #安装目录
  •     uid 99                    #用户haproxy
  •     gid 99                    #组haproxy
  •     daemon                    #守护进程运行
  •     nbproc 1                  #进程数量
  •     pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid

  • defaults
  •    log     global
  •    mode    http               #7层 http;4层tcp
  •    option  httplog            #http 日志格式
  •    option  httpclose          #主动关闭http通道
  •    option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器

  •    option  dontlognull
  •    maxconn 2000               #最大连接数
  •    contimeout      5000       #连接超时(毫秒)
  •    clitimeout      50000      #客户端超时(毫秒)
  •    srvtimeout      50000      #服务器超时(毫秒)

  • frontend haproxy_test         #定义前端服务器(haproxy)
  •         bind 10.0.1.251:80    #监听地址
  •         default_backend server_pool  #指定后端服务器群
  •         #errorfile 502 /usr/local/haproxy/html/maintain.html
  •         #errorfile 503 /usr/local/haproxy/html/maintain.html
  •         #errorfile 504 /usr/local/haproxy/html/maintain.html

  • backend server_pool           #定义后端服务器群(web server/apache/nginx/iis..)
  •         mode http
  •         option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
  •         #balance roundrobin    #负载均衡的方式,轮询方式
  •         balance leastconn     #负载均衡的方式,最小连接
  •         cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义
  •         option  httpchk HEAD /check.html #用来做健康检查html文档
  •         server server1 10.0.1.252:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
  •         server server2 10.0.1.253:80 cookie server2 check inter 2000 rise 3 fall 3 maxconn 120 weight 3
  •         server server3 10.0.1.254:80 cookie server3 check maxconn 90 rise 2 fall 3 weight 3
  • #服务器定义:
  • #cookie server1表示serverid为server1;
  • #check inter 2000 是检测心跳频率(check 默认 );
  • #rise 3 表示 3次正确认为服务器可用;
  • #fall 3 表示 3次失败认为服务器不可用;
  • #weight 表示权重。

  • listen admin_stat                   #status
  •     bind *:8080                     #监听端口
  •     mode http                       #http的7层模式
  •     stats refresh 30s               #统计页面自动刷新时间
  •     stats uri /haproxy-stats        #统计页面URL
  •     stats realm Haproxy\ Statistics #统计页面密码框上提示文本
  •     stats auth admin:admin          #统计页面用户名和密码设置
  •     stats hide-version              #隐藏统计页面上HAProxy的版本信息
  •     stats admin if TRUE             #手工启用/禁用,后端服务器
  • ' > &quot;$ha_cfg&quot; && sed -i '1 d' &quot;$ha_cfg&quot;
  • }


  • #install
  • if [ ! -e &quot;$ha_dir&quot; ];then
  •    tar zxf haproxy*.tar.gz
  •    cd haproxy*/
  •    make TARGET=linux26 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && mkdir /usr/local/haproxy/{html,logs}
  •    cd ../
  • #
  •    if [ ! -e &quot;$ha_dir&quot; ];then
  •        echo &quot;error! can't install haproxy  please check ! Will now out of the script !&quot;
  •        exit 1
  •    else
  •        ! grep 'haproxy' /etc/syslog.conf && echo 'local1.*            /var/log/haproxy.log' >> /etc/syslog.conf
  •        sed -ir 's/SYSLOGD_OPTIONS=&quot;-m 0&quot;/SYSLOGD_OPTIONS=&quot;-r -m 0&quot;/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart
  •        install_ha_cfg
  •        rm -rf haproxy*/
  •    fi
  • else
  •    echo &quot;haproxy is already exists!&quot;
  • fi
  

  14 keepalived 安装脚本
  


  • $ cat keepalived_install.sh

  • #!/bin/bash
  • #install keepalived
  • #20111209 by dongnan

  • #variables
  • keep_dir=&quot;/usr/local/keepalived/&quot;
  • keep_conf=&quot;${keep_dir}keepalived.conf&quot;
  • haproxy_dir=&quot;/usr/local/haproxy/&quot;
  • #echo &quot;$keep_dir $keep_conf $haproxy_dir&quot;


  • #function
  • #
  • install_keepalived() {

  • local keepalived_type=$1

  • if [ ! -e &quot;$keep_dir&quot; ];then
  •     tar zxf keepalived*.tar.gz
  •     cd keepalived*/
  •     ./configure --prefix=$keep_dir
  •     make && make install
  •     cd ../

  •     if [ ! -e &quot;$keep_dir&quot; ];then
  •         echo &quot;error! can't install keepalived  please check ! Will now out of the script !&quot;
  •         exit 1
  •     else
  •         if [ &quot;$keepalived_type&quot; = &quot;master&quot; ];then
  •             echo 'master'
  •             cat keepalived.conf | sed -r 's/state BACKUP/state MASTER/g;s/nopreempt/#/g' > &quot;$keep_conf&quot;
  •         elif [ &quot;$keepalived_type&quot; = &quot;backup&quot; ];then
  •             echo 'backup'
  •             cat keepalived.conf >  &quot;$keep_conf&quot;
  •         else
  •             echo 'backup-2'
  •             cat keepalived.conf | sed -r 's/priority 150/priority 100/g;s/nopreempt/#/g' > &quot;$keep_conf&quot;
  •         fi
  •         cp check_haproxy.sh &quot;$keep_dir&quot;
  •         rm -rf keepalived*/
  •     fi

  • else
  •     echo &quot;keepalived is Already exists!&quot;
  • fi
  • }


  • #main
  • case &quot;$1&quot; in
  •         master)
  •             install_keepalived $1
  •         ;;
  •     backup)
  •             install_keepalived $1
  •         ;;
  •     backup-2)
  •             install_keepalived $1
  •         ;;
  •     *)
  •         echo &quot;Usage: $0 {master|backup|backup-2}&quot;
  •             exit 1
  • esac
  

  15 keepalived 配置文档
  


  • # cat cluster/keepalived.conf
  • global_defs {

  • notification_email {
  • root@localhost
  • }
  • notification_mail_form root@localhost
  • smtp_server 127.0.0.1
  • smtp_connect_timeout 30

  • router_id haproxy
  • }

  • vrrp_sync_group VG1 {

  • group {
  • VI_1
  • }

  • }


  • vrrp_instance VI_1 {
  • state BACKUP
  • nopreempt
  • interface eth0

  • track_interface {
  • eth0
  • eth1
  • }

  • virtual_router_id 51
  • priority 150
  • advert_int 1

  • authentication {
  • auth_type PASS
  • auth_pass 1111
  • }


  • virtual_ipaddress {
  • 192.168.57.77/24 dev eth0 scope global
  • }

  • }
  

  16 keepalived 服务脚本
  


  • # cat /usr/local/sbin/keepalived.sh
  • #!/bin/bash
  • #keepalived
  • #ver:0.1bate
  • #20111211 by dongnan

  • #variables
  • keep_dir=&quot;/usr/local/keepalived/&quot;
  • keep_conf=&quot;${keep_dir}keepalived.conf&quot;
  • keep_sbin=&quot;${keep_dir}sbin/keepalived&quot;
  • haproxy_check=&quot;${keep_dir}haproxy_check.sh&quot;
  • haproxy_ps=$(ps -C haproxy --no-header |wc -l)

  • #main
  • case &quot;$1&quot; in
  •   start)
  •             if [ &quot;$haproxy_ps&quot; -ge &quot;1&quot; ];then
  •                 $keep_sbin -f $keep_conf &&  $haproxy_check start &
  •             else
  •                 echo &quot;not found haproxy processes,please startup haproxy &quot;
  •                 exit 1
  •             fi
  •               ;;
  •     stop)
  •             killall keepalived &&  $haproxy_check stop &
  •               ;;
  •     *)
  •         echo &quot;Usage: $0 {start|stop}&quot;
  •             exit 1
  •             ;;
  • esac
  

  17 安全设置脚本
  18 “haproxy + keepalived 一键安装包”  脚本
  


  • # cat 1key_install.sh
  • #!/bin/bash
  • #
  • #install haproxy and keepalived
  • #configure: /usrl/local/keepalived/keepalived.conf ; /usr/local/haproxy/haproxy.cfg
  • #sbin: /usr/local/sbin/keepalived.sh ; /usr/local/sbin/haproxy.sh
  • #haproxy_check.sh: /usr/local/keepalived/haproxy_check.sh
  • #startup: /etc/rc.local
  • #
  • #1key_install.sh ver 0.1
  • #20111218 by dongnan Email:dngood@sina.com
  • #


  • #variables
  • sbin_dir=/usr/local/sbin/
  • keepalived_dir=/usr/local/keepalived/
  • startup=/etc/rc.local

  • #main
  • case &quot;$1&quot; in
  •     keepalived_type)
  • #
  •        bash ./haproxy_install.sh
  •        bash ./keepalived_install.sh &quot;$2&quot;
  • #
  •        ! ls &quot;${sbin_dir}haproxy.sh&quot; > /dev/null 2>&1 && cp ./haproxy.sh &quot;$sbin_dir&quot;
  •        ! ls &quot;${sbin_dir}keepalived.sh&quot; > /dev/null 2>&1 && cp ./keepalived.sh &quot;$sbin_dir&quot;
  •        ! ls &quot;${keepalived_dir}haproxy_check.sh&quot; > /dev/null 2>&1 && cp ./haproxy_check.sh &quot;$keepalived_dir&quot;
  •        chmod 700 &quot;${keepalived_dir}haproxy_check.sh&quot;
  • #
  •        ! grep  'haproxy.sh' &quot;$startup&quot; && echo  &quot;${sbin_dir}haproxy.sh start&quot; >> &quot;$startup&quot;
  •        ! grep  'keepalived.sh' &quot;$startup&quot; && echo  &quot;${sbin_dir}keepalived.sh start&quot; >> &quot;$startup&quot;
  •          ;;
  •     *)
  •        echo &quot;Usage: $0 {keepalived_type backup}&quot;
  •        echo &quot;       keepalived_type:master|backup|backup-2&quot;
  •        exit 1
  •        ;;
  • esac
  

  19 haproxy 监控脚本
  


  • # cat haproxy_check.sh
  • #!/bin/bash
  • #
  • #monitor haproxy
  • #
  • #haproxy_chekc.sh ver 0.3 bate
  • #20111218 by dongnan Email:dngood@sina.com
  • #
  • #0.3 bate版
  • #1 添加了简单容错功能,80端口故障后会再检查多次,如果故障次数超过规定的失败次数,则认定haproxy 失败,否则认定haproxy 正常;举个例子比如haproxy 并发比较大,没有向应监控脚本的请求,监控脚本记录失败次数1,依次执行10次,如果监控失败次数小于5(比如1次),则认为haproxy 正常,进入下次的监控,而不是像0.2版监控脚本一样,只要有一次80端口故障立马就切换了!
  • #2 为了方便keepalived.sh 调用此脚本,将脚本写成daemon形式可以设定监控脚本检查间隔时间(默认2s执行一次),不再需要依赖crontab来执行了,为方便调试脚本添加以启动和关闭监控脚本功能!


  • #variables
  • eth1_ip=$(/sbin/ifconfig eth1  | sed -n '/inet addr:/ s/inet addr://pg' | awk -F&quot; &quot; '{print $1}')
  • #eth0_ip=$(/sbin/ifconfig eth0  | sed -n '/inet addr:/ s/inet addr://pg' | awk -F&quot; &quot; '{print $1}')
  • eth1_url=&quot;http://${eth1_ip}:80&quot;
  • #eth0_url=&quot;http://${eth0_ip}:80&quot;
  • check_time=2         #检查间隔时间,2s
  • check_count=8        #故障后检查次数,8次
  • fault_count=4        #故障次数大于(4次),则认为不可用
  • keepalived_dir=/usr/local/keepalived/
  • fault_log=&quot;${keepalived_dir}fault.log&quot;


  • #function
  • check_haproxy(){
  • pid_file=&quot;${keepalived_dir}haproxy_check.pid&quot;
  • echo &quot;$$&quot; > &quot;$pid_file&quot; && echo `cat $pid_file`
  • keepalived=`ps -C keepalived --no-header |wc -l`

  • while true;do
  • #检查时间,2s
  • sleep &quot;$check_time&quot;             #

  • if curl -IL -A &quot;haproxy_check&quot; &quot;$eth1_url&quot; > /dev/null 2>&1 ;then
  • #if curl -IL -A &quot;haproxy_check&quot; &quot;$eth0_url&quot; > /dev/null 2>&1  &&  curl -IL -A &quot;haproxy_check&quot; &quot;$eth1_url&quot; > /dev/null 2>&1 ;then
  • #
  •    continue
  • #
  • else
  • #
  •     check_failed=0
  • #故障后检查次数,8次
  •     for((i=1;i /dev/null 2>&1 ;then
  •             failed_count=$((check_failed++))
  •             sleep 1
  •         fi
  • #日志
  •         if [ &quot;$keepalived&quot; -ge &quot;2&quot; ];then
  •             echo -e &quot;$check_date $eth1_url $check_failed \t fault&quot; >> &quot;$fault_log&quot; 2>&1
  •         fi
  • #
  •     done
  • #检查失败次数大于故障次数,则停止keepalived
  •     if [ &quot;$failed_count&quot; -gt &quot;$fault_count&quot; ];then
  •          killall keepalived && /etc/init.d/network restart
  •          #mail -s 'haproxy and keepalived fault' root@localhost
  •          break 2
  •     fi
  • fi
  • done
  • #
  • }

  • #main
  • case &quot;$1&quot; in
  •   start)
  •       check_haproxy
  •         ;;
  •     stop)
  •       #echo `cat $pid_file`
  •       kill `cat &quot;${keepalived_dir}haproxy_check.pid&quot;`
  •         ;;
  •     restart)
  •       echo &quot;Sorry The next version will realize this function !!!&quot;
  •         ;;
  •     *)
  •         echo &quot;Usage: $0 {start|stop|restart}&quot;
  •       exit 1
  •       ;;
  • esac
  

  20 iptables 脚本
  


  • $cat /root/sh/iptables.sh
  • #!/bin/bash

  • #Define variable
  • IPTABLES=/sbin/iptables
  • INTRANET=&quot;100.100.100.0/24&quot;
  • Company=&quot;123.x.x.x&quot;
  • Company1=&quot;202.x.x.x&quot;
  • sy=&quot;119.x.x.x/27&quot;
  • sj1=&quot;5x.x.x.x/26&quot;
  • sj2=&quot;5x.x.x.x/28&quot;
  • sj3=&quot;5x.x.x.x/28&quot;

  • $IPTABLES -F
  • $IPTABLES -X
  • $IPTABLES -P FORWARD DROP
  • $IPTABLES -P INPUT DROP

  • echo &quot;#Allow all at the loopback and INTRANET and shunyi sjhl&quot;
  • $IPTABLES -A INPUT -i lo -j ACCEPT
  • $IPTABLES -A INPUT -s $INTRANET -j ACCEPT
  • $IPTABLES -A INPUT -s $sy -j ACCEPT
  • $IPTABLES -A INPUT -s $sj1 -j ACCEPT
  • $IPTABLES -A INPUT -s $sj2 -j ACCEPT
  • $IPTABLES -A INPUT -s $sj3 -j ACCEPT
  • $IPTABLES -A INPUT -s $Company -j ACCEPT
  • $IPTABLES -A INPUT -s $Company1 -j ACCEPT


  • echo &quot;#Accepting HTTP&quot;
  • $IPTABLES -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT

  • echo &quot;ALLOW PING&quot;
  • #$IPTABLES -A INPUT -i eth0 -p icmp -j ACCEPT

  • echo &quot;#Allow alive conn&quot;
  • $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  

  21
  22
  23 memcached 安装脚本
  


  • $ cat mem/memcached_install.sh
  • #!/bin/bash
  • #install memcached
  • #ver: 0.1bate
  • #20111230 by dongnan

  • #variables
  • dir=&quot;/usr/local/&quot;
  • libevent_dir=${dir}libevent
  • memcached_dir=${dir}memcached
  • sbin_dir=&quot;${dir}sbin/&quot;
  • #main

  • #check libevent
  • if [ ! -d &quot;$libevent_dir&quot; ];then
  •      tar zxvf libevent*.tar.gz
  •      cd libevent*/
  •      ./configure --prefix=&quot;$libevent_dir&quot;
  •      make && make install
  •      cd ../
  • #
  • sleep 2

  •      if [ -d &quot;$libevent_dir&quot; ];then
  •          echo &quot;libevent is ok&quot;
  •          rm -rf libevent*/
  •      else
  •          echo &quot;error! can't install libevent please check ! Will now out of the script!&quot;
  •          exit 1
  •      fi
  • else
  •     echo &quot;libevent is already exists!&quot;
  • fi


  • #memcached
  • if [ ! -d &quot;$memcached_dir&quot; ];then
  •      tar zxvf memcached*.tar.gz
  •      cd memcached*/
  •      ./configure --prefix=&quot;$memcached_dir&quot; --with-libevent=&quot;$libevent_dir&quot;
  •      make && make install
  •      cd ../
  • #
  • sleep 2

  •      if [ -d &quot;$memcached_dir&quot; ];then
  •          echo &quot;memcached is ok&quot;
  •          rm -rf memcached*/
  •          ! ls &quot;${sbin_dir}memcached.sh&quot; > /dev/null 2>&1 && cp ./memcached.sh &quot;$sbin_dir&quot; && chmod 700 &quot;${sbin_dir}memcached.sh&quot;
  •      else
  •          echo &quot;error! can't install memcached please check ! Will now out of the script!&quot;
  •          exit 1
  •      fi
  • else
  •     echo &quot;memcached is already exists!&quot;
  • fi
  

  24 memcached 服务脚本
  


  • $ cat mem/memcached.sh
  • #!/bin/bash
  • #memcached
  • #ver:0.1bate
  • #20111230 by dongnan

  • #variables
  • mem_dir=&quot;/usr/local/memcached/&quot;
  • mem_pid=&quot;/var/run/memcached.pid&quot;
  • mem_sbin=&quot;${mem_dir}bin/memcached&quot;
  • mem_size=&quot;64&quot;
  • mem_ip=$(/sbin/ifconfig eth1  | sed -n '/inet addr:/ s/inet addr://pg' | awk -F&quot; &quot; '{print $1}')
  • mem_port=&quot;11211&quot;
  • mem_max_con=&quot;10000&quot;

  • #main
  • case &quot;$1&quot; in
  •         start)
  •             $mem_sbin -d -u root -m &quot;$mem_size&quot; -l &quot;$mem_ip&quot; -p &quot;$mem_port&quot; -c &quot;$mem_max_con&quot; -P &quot;$mem_pid&quot;
  •               ;;
  •     stop)
  •             kill $(cat $mem_pid)
  •               ;;
  •     *)
  •         echo &quot;Usage: $0 {start|stop}&quot;
  •             exit 1
  •                   ;;
  • esac
  

  25# Nginx 分割日志脚本
  


  • $ cat nginx_log.sh
  • #!/bin/bash
  • #split log
  • #

  • #Nginx
  • #Signal Action
  • #TERM, INT Terminate the server immediately
  • #QUIT Stop the server
  • #HUP Configuration changes, start new workers, graceful stop of old workers
  • #USR1 Reopen log files
  • #USR2 Upgrade the server executable
  • #WINCH Graceful Stop (parent process advise the children to exit)


  • #variables
  • log_dir=/data/logs/yum/
  • log_date=$(date +'%F')
  • nginx_pid=/usr/local/nginx/logs/nginx.pid
  • keep_days=30

  • #old_log
  • find &quot;$log_dir&quot; -name &quot;*\.log&quot; -type f -mtime +${keep_days} -exec rm -rf {} \;

  • #new_log
  • if [ -e ${log_dir}access-${log_date}.log ];then
  •      echo &quot;Already exists&quot; && exit 1
  • else
  •     /bin/mv ${log_dir}access.log ${log_dir}access-${log_date}.log && /bin/kill -USR1 $(cat $nginx_pid)
  • fi
  

  26 # php 文件发布脚本
  


  • cat rsync_www.sh
  • #!/bin/bash
  • #
  • #/usr/bin/rsync -avz --password-file=/root/rsync_pw /var/www/test/test rsync@10.0.100.82::www/test
  • #echo &quot;rsync_password&quot; > rsync_pw && chmod 600 rsync_pw

  • #variables
  • #HOSTS=&quot;10.0.100.82 10.0.100.81 10.0.100.83&quot;
  • HOSTS=&quot;10.0.100.82&quot;
  • MOD=www
  • MAILADD=&quot;root@localhost&quot;
  • DATE=`date +%F_%T`
  • src_dir=/var/www/html/
  • rsync_pw=/root/sh/rsync_pw
  • rsync_user=dongnan
  • rsync_file_dir=/var/www/rsync/
  • log_dir=/var/www/rsync/log/
  • regular_replace=/data/

  • #USERNAME=`logname`
  • if [ -n &quot;$SUDO_USER&quot; ];then
  • USERNAME=`echo $SUDO_USER`
  • else
  • USERNAME=&quot;root&quot;
  • fi

  • #function
  • rsync_www(){
  • for host in $HOSTS;do
  •     #if /usr/bin/rsync -avz ${src_dir}$1 $host::${MOD}/$1;then
  •     if /usr/bin/rsync -avz --password-file=&quot;$rsync_pw&quot; ${src_dir}$1 ${rsync_user}@${host}::${MOD}/$1;then
  •         echo &quot;$DATE ${src_dir}$1 rsync to $host ok by $USERNAME&quot; >> ${log_dir}rsync.ok
  •     else
  •         echo &quot;$DATE ${src_dir}$1 rsync to $host error by $USERNAME&quot; >> ${log_dir}rsync.error
  •         echo &quot;$DATE ${src_dir}$1 rsync to $host error by $USERNAME.&quot;| mail -s &quot;rsync error&quot; $MAILADD
  •     fi
  • done
  • }

  • #main
  • #删除空白或空行
  • sed -ri 's/^[ \t]+//g;/^$/d' ${rsync_file_dir}rsync_file.list
  • #
  • sed -e 's#/var/www/html/##g' ${rsync_file_dir}rsync_file.list > ${log_dir}rsync.tmp

  • while read line;do
  •     if [ ! -d &quot;$src_dir$line&quot; -a ! -f &quot;$src_dir$line&quot; ];then
  •         echo &quot;$DATE ${src_dir}$line is't existence by $USERNAME!!!&quot; | tee -a ${log_dir}rsync.error | mail -s &quot;rsync error&quot; $MAILADD
  •     else
  •         rsync_www $line
  •     fi
  • done < ${log_dir}rsync.tmp
  

  27
  28
  29
  30
  31 cygwin 专用
  


  • #!/bin/bash
  • #
  • #

  • #variables
  • pid_dir=/var/run/
  • rsync_pid=${pid_dir}rsync.pid
  • rsync_src_dir=&quot;/cygdrive/f/WebSite/brandimg /cygdrive/f/WebSite/fashion.yoka.com&quot;
  • loop_time=600

  • #function
  • #echo &quot;$$&quot; > &quot;$rsync_pid&quot; && cat &quot;$rsync_pid&quot;
  • rsync_start (){
  • while true;do
  •     for src in $rsync_src_dir ;do
  •         ls $src
  •     done
  •     sleep &quot;$loop_time&quot;
  • done
  • }

  • rsync_pid (){
  • ps aux |awk '/(bash|sleep|sh)$/ {print$1,$3}'| awk 'NR>1&&$NF==var{print line &quot;\n&quot;$0}{line=$0;var=$NF}' | awk '{print $1}'> /var/run/rsync.pid
  • }

  • rsync_stop (){
  • while read line;do
  •     #kill &quot;$line&quot;
  •      echo &quot;$line&quot;
  • done < $rsync_pid
  • }

  • #main
  • case &quot;$1&quot; in
  •     start)
  •     rsync_start &
  •     rsync_pid
  •     ;;
  •     stop)
  •     rsync_stop
  •     ;;
  •     *)
  •     echo &quot;Usage: $0 {start|stop}&quot;
  •     exit 1
  •     ;;
  • esac
  

  # seq 一例子
  


  • #!/bin/bash
  • #usage seq of loop
  • #NAME
  • #       seq - print a sequence of numbers
  • #
  • #SYNOPSIS
  • #       seq [OPTION]... LAST
  • #       seq [OPTION]... FIRST LAST
  • #       seq [OPTION]... FIRST INCREMENT LAST
  • #
  • #DESCRIPTION
  • #       Print numbers from FIRST to LAST, in steps of INCREMENT.

  • for i in $(seq -s ':' 1 10);do
  •     echo $i
  • done

  • #

  • for i in {1..10};do
  •     echo $i
  • done
  

  # 测试Hadoop HDFS
  


  • $ cat test_hdfs.sh

  • #!/bin/bash
  • #test the hadoop HDFS
  • #date +%H%N 随机数
  • ######%N########nanoseconds (000000000..999999999) 亿分之一秒时间&quot;纳秒&quot;
  • ######%H########hour (00..23)以小时为时间戳
  • #echo $RANDOM
  • #/dev/random

  • #variables
  • loop=5000
  • bs=5k
  • count=1
  • dfs_dir=/data/dfs/


  • #main
  • for((j=1;j file
  • #
  • sleep 5
  • for ((i=1;i> file
  • done
  • #
  • sleep 10
  • while read line ;do
  • dd if=/dev/zero of=${dfs_dir}$j/$line bs=$bs count=$count > /dev/null 2>&1 && usleep 10000
  • #echo &quot;$i&quot; > ${dfs_dir}$line && usleep 10000
  • echo $line
  • done < file

  • done
  

  # 挂载CIFS
  


  • #!/bin/bash
  • #completed by dongnan
  • #Mount windows CIFS

  • #variables
  • cifs_ip=192.168.1.100
  • cifs_user=administrator
  • cifs_pw=dongnan
  • cifs_share=aa
  • mount_point=/mnt/fat
  • mount=/sbin/mount.cifs
  • user_id=$(id -u)


  • #main
  • if [ &quot;$user_id&quot; -ne 0 ];then
  •     echo &quot;must root or SUDO_USER !!&quot;
  •     exit 1
  • fi

  • if mount | grep -E &quot;$cifs_share|$cifs_ip&quot; > /dev/null 2>&1;then
  •     echo &quot;Has been mounted&quot;
  •     exit 1
  • fi

  • $mount -o &quot;username=$cifs_user,password=$cifs_pw&quot; //${cifs_ip}/$cifs_share $mount_point
  

  # 统计windows 服务器 20120531
  


  • cat find_windows.sh
  • #!/bin/bash
  • #test windows remote desktop 3389 port

  • for i in {1..254};do nc -nvz -w 2 10.0.0.$i 3389 >/dev/null 2>&1 && echo 10.0.0.$i | tr '\n' ' '; done
  

  # 统计ESXi 服务器 20120601
  


  • cat find_esxi.sh
  • #!/bin/bash
  • #ESXi port 443 and 902
  • for ip in 0 1;do
  • for i in {1..254};do nc -nz -w 2 10.0.${ip}.${i} 443 >/dev/null 2>&1 && nc -nz 10.0.${ip}.${i} 902 >/dev/null 2>&1 && echo 10.0.${ip}.${i} | tr '\n' ' '; done
  • done
  

  #多台linux服务器添加帐号,并添加到sudoers 20120605
  


  • cat add-user.sh
  • #!/bin/bash
  • #add user for this host
  • #20120604 by dongnan

  • #variables
  • ip=10.0.0.
  • hosts='201 202 203 204 205'
  • user='dongnan'
  • pw='dongnan'

  • for host in $hosts;do
  •     echo &quot;${ip}$host&quot;
  •     ssh ${ip}$host &quot;useradd $user && echo $pw | passwd --stdin $user && sed -ir '/^root.*ALL$/ a $user ALL=(ALL)  ALL' /etc/sudoers&quot;
  •     #ssh ${ip}$host userdel -r $user
  • done
  

  #


本帖被以下淘专辑推荐:

  • · kl|主题: 2, 订阅: 0

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-552738-1-1.html 上篇帖子: shell-脚本集合 下篇帖子: shell中实现文件锁
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表