[转帖]shell脚本合集(一)_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4028 | 回复: 0   主题: [转帖]shell脚本合集(一)        下一篇 
Leon
注册用户
等级:少校
经验:1436
发帖:116
精华:7
注册:2013-1-4
状态:离线
发送短消息息给Leon 加好友    发送短消息息给Leon 发消息
发表于: IP:您无权察看 2013-1-4 13:58:09 | [全部帖] [楼主帖] 楼主

¡¡
1 web server status

  1. #!/bin/bash 
  2. #test web server http status 
  3. #20111011 by dongnan 
  4. for i in $(cat test);do 
  5.    #echo $i 
  6.    if curl -IL -m 2 $i 2>&1 | grep '200 OK' > /dev/null ;then 
  7.       echo "$i status is 200" 
  8.    elif curl -IL -m 2 $i 2>&1 | grep '403 Forbidden' > /dev/null ;then  
  9.       echo "$i status is 403" 
  10.    else 
  11.       echo "$i is bad" 
  12.    fi   
  13. done 
  1. #./domain.sh 
  2. www.aa.com status is 200 
  3. www.gunduzi.sb is bad 
  4. www.sx.sb is bad 

2 test url 502

  1. #!/bin/bash 
  2. #20111015 by dongnan
  3. #test the url 502 
  4. for((i=1;i<=50;i++));do  
  5.     if curl -LI http://www.a.com/set.php?id=123456 2>&1 | grep '502' > /dev/null ;then 
  6.         echo "echo $i 502" 
  7.     fi   
  8.     sleep 1; 
  9. done 

3 test cdn ip loop

  1. #!/bin/bash 
  2. #20111101 by dongnan 
  3. #test the cdn ip loop 
  4. for((i=1;i<=60;i++));do 
  5.  ping -c 1 *.cdns.com | grep -v PING | grep -i 'from' | awk '{print $4}' | sed -r 's/(:)|(\-BJ\-CNC)//g' 
  6. done 

测试

  1. ./cdn-loop.sh | sort | uniq -c 
  2.      20 123.1.1.1 
  3.      20 123.1.2.2 
  4.      20 21.1.3.3 

    4 测试 apache vhost

  1. grep -vE '^#|^[ \t].&#' httpd-vhosts.conf | grep ServerName | awk '{print $2}' >> bbs_vhost.txt
  2. while read line;do dig $line | grep -E 'IN.*A|IN.*CNAME'|grep -v '^;';done < bbs_vhost.txt 
  3. bbs.a.com. 956 IN A *.*.*.* 
  4. msn.bbs.a.com. 807 IN CNAME bbs.a.com. 
  5. bbs.a.com. 956 IN A *.*.*.* 
  6. bbsimg.a.com. 1460 IN A *.*.*.* 
  7. pq.a.com. 861 IN A *.*.*.* 
  8. wenda.a.com. 1794 IN A *.*.*.* 

5 批量上传ssh id_rsa.pub 公匙

自动: 需要使用 expect 命令

  1. cat auto_ssh_key.sh
  2. #!/bin/bash 
  3. #auto ssh key 
  4. #20111125 by dongnan
  5. #variables 
  6. hosts='2 3 4 5 6 7 8 9' 
  7. ip=10.100.100. 
  8. user=dn 
  9. password=mima 
  10. #function 
  11. 参考
  12. auto_ssh_copy_id () { 
  13.  expect -c "set timeout -1; 
  14.  spawn ssh-copy-id -i /home/dn/.ssh/id_rsa.pub $1; 
  15.  expect { 
  16.  *(yes/no)* {send -- yes\r;exp_continue;} 
  17.  *assword:* {send -- $2\r;exp_continue;} 
  18.  eof {exit 0;} 
  19.  }"; 
  20. for i in $hosts;do 
  21.  echo "${ip}$i" 
  22. # cat ~/.ssh/id_rsa.pub | ssh dn@${ip}$i "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat - >~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys" 
  23.  auto_ssh_copy_id $user@${ip}$i $password 
  24. done 

    6 批量部署 .bash_profile ,前提需要 ssh通过私匙登陆ssh服务器

关于如何配置ssh key,请参考这里

  1. $ grep -E 'space.$|bbs.$' /etc/hosts | awk '{print $1}' > hosts 
  2. $ while read line;do echo $line;scp ~/.bash_profile dn@${line}:/home/dn/;done < hosts 
  3.  10.100.100.2 
  4. .bash_profile 100% 622 0.6KB/s 00:00 
  5. 10.100.100.3 
  6. .bash_profile 100% 622 0.6KB/s 00:00 
  7. 10.100.100.4 
  8. .bash_profile 100% 622 0.6KB/s 00:00 
  9. ...... 

7 stdin and while

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

    8 自定义.bash_profile 文件!

  1. #将以下行添加到.bash_profile
  2. ip=$(/sbin/ifconfig eth1 | grep Bcast | sed -e 's/^.* addr:\(.*\) Bcast.*$/\1/') 
  3. if [ $ip ];then 
  4.     PS1="\[\e[32;1m\]$ip\[\e[0m\][\W]\$ " 
  5.     export PS1 
  6. fi 
  7. echo "[LOAD]";cat /proc/loadavg;echo "[DISK]";df -Th 

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

  1. #!/bin/bash 
  2. #chek login user 
  3. #variables 
  4. user=`id -u` 
  5. if [ $user -ne 0 ];then 
  6.  echo "must root !!" 
  7.  exit 1 
  8. fi 
  9. for i in `ls /var/log/secure*`;do 
  10.  echo "$i" 
  11.  #cat /var/log/secure | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c 
  12.  cat "$i" | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c 
  13. done

效果如下

  1. ./login.sh 
  2. /var/log/secure 
  3.       6 dn 100.100.100.99 
  4. /var/log/secure.1 
  5.      15 dn 100.100.100.99 

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

  1. #!/bin/bash 
  2. #du -sh 
  3. #variables 
  4. ip=100.100.100. 
  5. #hosts='24' 
  6. hosts='22 23 24 25 26 27 28 29' 
  7. for host in $hosts;do 
  8.  echo ${ip}$host 
  9.  ssh ${ip}$host "find /var/www/bbs/ -type d -name 'day*' | xargs du -h | awk '{print \$1}'" 
  10.  ssh ${ip}$host "du -sh /var/www/bbs/" 
  11. done 

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

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

#统计硬盘使用情况

  1. cat bbs_df.sh 
  2. #!/bin/bash 
  3. #vdisk usage 
  4. #20111129 by dongnan 
  5. #variables 
  6. ip=10.100.100. 
  7. #hosts='102' 
  8. hosts='102 103 104 105 106 107 108 109' 
  9. for host in $hosts;do 
  10.  echo -en "${ip}$host " 
  11.  ssh ${ip}$host "df -h | grep -E '^/dev.*(/|/usr|/var)$' | awk '{print \$6,\$5}'" 
  12. done 

11 haproxy 服务脚本

  1. # cat /usr/local/sbin/haproxy.sh 
  2. #!/bin/bash 
  3. #haproxy command 
  4. #ver:0.1bate 
  5. #20111129 by dongnan 
  6. #/usr/local/haproxy/sbin/haproxy 
  7. #HA-Proxy version 1.4.18 2011/09/16 
  8. #Copyright 2000-2011 Willy Tarreau <w@1wt.eu> 
  9. #Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ] 
  10. # [ -p <pidfile> ] [ -m <max megs> ] 
  11. # -v displays version ; -vv shows known build options. 
  12. # -d enters debug mode ; -db only disables background mode. 
  13. # -V enters verbose mode (disables quiet mode) 
  14. # -D goes daemon 
  15. # -q quiet mode : don't display messages 
  16. # -c check mode : only check config files and exit 
  17. # -n sets the maximum total # of connections (2000) 
  18. # -m limits the usable amount of memory (in MB) 
  19. # -N sets the default, per-proxy maximum # of connections (2000) 
  20. # -p writes pids of all children to this file 
  21. # -de disables epoll() usage even when available 
  22. # -ds disables speculative epoll() usage even when available 
  23. # -dp disables poll() usage even when available 
  24. # -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments. 
  25. #variables 
  26. haproxy_dir=/usr/local/haproxy/ 
  27. haproxy_conf=${haproxy_dir}haproxy.cfg 
  28. haproxy_pid=${haproxy_dir}logs/haproxy.pid 
  29. haproxy_cmd=${haproxy_dir}sbin/haproxy 
  30. #test variables 
  31. #file $haproxy_dir; file $haproxy_conf; file $haproxy_cmd; file $haproxy_pid 
  32. if [ "$#" -eq "0" ];then 
  33.  echo "usage: $0 {start|stop|restart}" 
  34.  exit 1 
  35. fi 
  36. if [ "$1" = "start" ];then 
  37. #echo $1 
  38.  $haproxy_cmd -f $haproxy_conf 
  39. elif [ "$1" = "stop" ];then 
  40. #echo $1 
  41.  kill `cat $haproxy_pid` 
  42. elif [ "$1" = "restart" ];then 
  43. #echo $1 
  44.  $haproxy_cmd -f $haproxy_conf -st `cat $haproxy_pid` 
  45. else 
  46.  echo "usage: $0 arguments only start and stop or restart !" 
  47. fi 

12 cpu load 测试

  1. $ cat load.sh
  2. #! /bin/sh 
  3. # filename killcpu.sh 
  4. for i in `seq $1` 
  5. do 
  6.  echo -ne " 
  7. i=0; 
  8. while true 
  9. do 
  10. ii=i+1; 
  11. done" | /bin/sh & 
  12.  pid_array[$i]=$! ; 
  13. done 
  14. for i in "${pid_array[@]}"; do 
  15.  echo 'kill ' $i ';'; 
  16. done 
  17. 使用方法很简单,参数3表示消耗3颗CPU的资源,运行后,会有一堆 kill 命令,方便 kill 进程: 
  18. [root@test02 ~]# ./killcpu.sh 3 
  19. kill 30104 ; 
  20. kill 30106 ; 
  21. kill 30108 ; 

13 haproxy 安装脚本

  1. cat haproxy_install.sh

  2. #!/bin/bash 
  3. #install haproxy 
  4. #20111207 by dongnan 
  5. #variables 
  6. dir=/usr/local 
  7. ha_dir=${dir}/haproxy 
  8. ha_cfg=${ha_dir}/haproxy.cfg 
  9. kernel=`uname -r | grep '2.6'` 
  10. pcre=$(rpm -qa | grep 'pcre' | wc -l) 
  11. echo "$dir, $ha_dir, $ha_cfg, $kernel, $pcre" 
  12. #check 
  13. if [ ! "$kernel" -o "$pcre" -lt "2" ];then 
  14.  echo -e "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'" 
  15.  exit 1 
  16. fi 
  17. #function 
  18. install_ha_cfg (){ 
  19.  #configure haproxy.cfg 
  20.  #default configure file for test,but need your change the frontend server and backend server ip address, 
  21.  #good luck! 
  22.  
  23.  echo ' 
  24.  global 
  25.  log 127.0.0.1 local0 
  26.  maxconn 4096 #最大连接数 
  27.  chroot /usr/local/haproxy #安装目录 
  28.  uid 99 #用户haproxy 
  29.  gid 99 #组haproxy 
  30.  daemon #守护进程运行 
  31.  nbproc 1 #进程数量 
  32.  pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid 
  33.  
  34.  defaults 
  35.  log global 
  36.  mode http #7层 http;4层tcp 
  37.  option httplog #http 日志格式 
  38.  option httpclose #主动关闭http通道 
  39.  option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器 
  40.  
  41.  option dontlognull 
  42.  maxconn 2000 #最大连接数 
  43.  contimeout 5000 #连接超时(毫秒) 
  44.  clitimeout 50000 #客户端超时(毫秒) 
  45.  srvtimeout 50000 #服务器超时(毫秒) 
  46.  
  47.  frontend haproxy_test #定义前端服务器(haproxy) 
  48.  bind 10.0.1.251:80 #监听地址 
  49.  default_backend server_pool #指定后端服务器群 
  50.  #errorfile 502 /usr/local/haproxy/html/maintain.html 
  51.  #errorfile 503 /usr/local/haproxy/html/maintain.html 
  52.  #errorfile 504 /usr/local/haproxy/html/maintain.html 
  53.  
  54.  backend server_pool #定义后端服务器群(web server/apache/nginx/iis..) 
  55.  mode http 
  56.  option forwardfor #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP 
  57.  #balance roundrobin #负载均衡的方式,轮询方式 
  58.  balance leastconn #负载均衡的方式,最小连接 
  59.  cookie SERVERID #插入serverid到cookie中,serverid后面可以定义 
  60.  option httpchk HEAD /check.html #用来做健康检查html文档 
  61.  server server1 10.0.1.252:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3 
  62.  server server2 10.0.1.253:80 cookie server2 check inter 2000 rise 3 fall 3 maxconn 120 weight 3 
  63.  server server3 10.0.1.254:80 cookie server3 check maxconn 90 rise 2 fall 3 weight 3 
  64.  #服务器定义: 
  65.  #cookie server1表示serverid为server1; 
  66.  #check inter 2000 是检测心跳频率(check 默认 ); 
  67.  #rise 3 表示 3次正确认为服务器可用; 
  68.  #fall 3 表示 3次失败认为服务器不可用; 
  69.  #weight 表示权重。 
  70.  
  71.  listen admin_stat #status 
  72.  bind *:8080 #监听端口 
  73.  mode http #http的7层模式 
  74.  stats refresh 30s #统计页面自动刷新时间 
  75.  stats uri /haproxy-stats #统计页面URL 
  76.  stats realm Haproxy\ Statistics #统计页面密码框上提示文本 
  77.  stats auth admin:admin #统计页面用户名和密码设置 
  78.  stats hide-version #隐藏统计页面上HAProxy的版本信息 
  79.  stats admin if TRUE #手工启用/禁用,后端服务器 
  80.  ' > "$ha_cfg" && sed -i '1 d' "$ha_cfg" 
  81. #install 
  82. if [ ! -e "$ha_dir" ];then 
  83.  tar zxf haproxy*.tar.gz 
  84.  cd haproxy*/ 
  85.  make TARGET=linux26 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && mkdir /usr/local/haproxy/{html,logs} 
  86.  cd ../ 
  87.  if [ ! -e "$ha_dir" ];then 
  88.  echo "error! can't install haproxy please check ! Will now out of the script !" 
  89.  exit 1 
  90.  else 
  91.  ! grep 'haproxy' /etc/syslog.conf && echo 'local1.* /var/log/haproxy.log' >> /etc/syslog.conf 
  92.  sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart 
  93.  install_ha_cfg 
  94.  rm -rf haproxy*/ 
  95.  fi 
  96. else 
  97.  echo "haproxy is already exists!" 
  98. fi 

14 keepalived 安装脚本

  1. $ cat keepalived_install.sh 
  2. #!/bin/bash 
  3. #install keepalived 
  4. #20111209 by dongnan 
  5. #variables 
  6. keep_dir="/usr/local/keepalived/" 
  7. keep_conf="${keep_dir}keepalived.conf" 
  8. haproxy_dir="/usr/local/haproxy/" 
  9. #echo "$keep_dir $keep_conf $haproxy_dir" 
  10. #function 
  11. install_keepalived() { 
  12.  
  13.  local keepalived_type=$1 
  14.  
  15.  if [ ! -e "$keep_dir" ];then 
  16.  tar zxf keepalived*.tar.gz 
  17.  cd keepalived*/ 
  18.  ./configure --prefix=$keep_dir 
  19.  make && make install 
  20.  cd ../ 
  21.  
  22.  if [ ! -e "$keep_dir" ];then 
  23.  echo "error! can't install keepalived please check ! Will now out of the script !" 
  24.  exit 1 
  25.  else 
  26.  if [ "$keepalived_type" = "master" ];then 
  27.  echo 'master' 
  28.  cat keepalived.conf | sed -r 's/state BACKUP/state MASTER/g;s/nopreempt/#/g' > "$keep_conf" 
  29.  elif [ "$keepalived_type" = "backup" ];then 
  30.  echo 'backup' 
  31.  cat keepalived.conf > "$keep_conf" 
  32.  else 
  33.  echo 'backup-2' 
  34.  cat keepalived.conf | sed -r 's/priority 150/priority 100/g;s/nopreempt/#/g' > "$keep_conf" 
  35.  fi 
  36.  cp check_haproxy.sh "$keep_dir" 
  37.  rm -rf keepalived*/ 
  38.  fi 
  39.  
  40.  else 
  41.  echo "keepalived is Already exists!" 
  42.  fi 
  43. #main 
  44. case "$1" in 
  45.  master) 
  46.  install_keepalived $1 
  47.  ;; 
  48.  backup) 
  49.  install_keepalived $1 
  50.  ;; 
  51.  backup-2) 
  52.  install_keepalived $1 
  53.  ;; 
  54.  *) 
  55.  echo "Usage: $0 {master|backup|backup-2}" 
  56.  exit 1 
  57. esac 

15 keepalived 配置文档

  1. # cat cluster/keepalived.conf 
  2. global_defs { 
  3.  
  4.  notification_email { 
  5.  root@localhost 
  6.  } 
  7.  notification_mail_form root@localhost 
  8.  smtp_server 127.0.0.1 
  9.  smtp_connect_timeout 30 
  10.  
  11.  router_id haproxy 
  12. vrrp_sync_group VG1 { 
  13.  
  14.  group { 
  15.  VI_1 
  16.  } 
  17.  
  18. vrrp_instance VI_1 { 
  19.  state BACKUP 
  20.  nopreempt 
  21.  interface eth0 
  22.  
  23.  track_interface { 
  24.  eth0 
  25.  eth1 
  26.  } 
  27.  
  28.  virtual_router_id 51 
  29.  priority 150 
  30.  advert_int 1 
  31.  
  32.  authentication { 
  33.  auth_type PASS 
  34.  auth_pass 1111 
  35.  } 
  36.  
  37.  
  38.  virtual_ipaddress { 
  39.  192.168.57.77/24 dev eth0 scope global 
  40.  } 
  41.  

16 keepalived 服务脚本

  1. # cat /usr/local/sbin/keepalived.sh 
  2. #!/bin/bash 
  3. #keepalived 
  4. #ver:0.1bate 
  5. #20111211 by dongnan 
  6. #variables 
  7. keep_dir="/usr/local/keepalived/" 
  8. keep_conf="${keep_dir}keepalived.conf" 
  9. keep_sbin="${keep_dir}sbin/keepalived" 
  10. haproxy_check="${keep_dir}haproxy_check.sh" 
  11. haproxy_ps=$(ps -C haproxy --no-header |wc -l) 
  12. #main 
  13. case "$1" in 
  14.  start) 
  15.  if [ "$haproxy_ps" -ge "1" ];then 
  16.  $keep_sbin -f $keep_conf && $haproxy_check start & 
  17.  else 
  18.  echo "not found haproxy processes,please startup haproxy " 
  19.  exit 1 
  20.  fi 
  21.  ;; 
  22.  stop) 
  23.  killall keepalived && $haproxy_check stop & 
  24.  ;; 
  25.  *) 
  26.  echo "Usage: $0 {start|stop}" 
  27.  exit 1 
  28.  ;; 
  29. esac 

17 安全设置脚本

18 “haproxy + keepalived 一键安装包”  脚本

  1. # cat 1key_install.sh 
  2. #!/bin/bash 
  3. #install haproxy and keepalived 
  4. #configure: /usrl/local/keepalived/keepalived.conf ; /usr/local/haproxy/haproxy.cfg 
  5. #sbin: /usr/local/sbin/keepalived.sh ; /usr/local/sbin/haproxy.sh 
  6. #haproxy_check.sh: /usr/local/keepalived/haproxy_check.sh 
  7. #startup: /etc/rc.local 
  8. #1key_install.sh ver 0.1 
  9. #20111218 by dongnan Email:dngood@sina.com 
  10. #variables 
  11. sbin_dir=/usr/local/sbin/ 
  12. keepalived_dir=/usr/local/keepalived/ 
  13. startup=/etc/rc.local 
  14. #main 
  15. case "$1" in 
  16.  keepalived_type) 
  17.  bash ./haproxy_install.sh 
  18.  bash ./keepalived_install.sh "$2" 
  19.  ! ls "${sbin_dir}haproxy.sh" > /dev/null 2>&1 && cp ./haproxy.sh "$sbin_dir" 
  20.  ! ls "${sbin_dir}keepalived.sh" > /dev/null 2>&1 && cp ./keepalived.sh "$sbin_dir" 
  21.  ! ls "${keepalived_dir}haproxy_check.sh" > /dev/null 2>&1 && cp ./haproxy_check.sh "$keepalived_dir" 
  22.  chmod 700 "${keepalived_dir}haproxy_check.sh" 
  23.  ! grep 'haproxy.sh' "$startup" && echo "${sbin_dir}haproxy.sh start" >> "$startup" 
  24.  ! grep 'keepalived.sh' "$startup" && echo "${sbin_dir}keepalived.sh start" >> "$startup" 
  25.  ;; 
  26.  *) 
  27.  echo "Usage: $0 {keepalived_type backup}" 
  28.  echo " keepalived_type:master|backup|backup-2" 
  29.  exit 1 
  30.  ;; 
  31. esac 

19 haproxy 监控脚本

  1. # cat haproxy_check.sh 
  2. #!/bin/bash 
  3. #monitor haproxy 
  4. #haproxy_chekc.sh ver 0.3 bate 
  5. #20111218 by dongnan Email:dngood@sina.com 
  6. #0.3 bate版 
  7. #1 添加了简单容错功能,80端口故障后会再检查多次,如果故障次数超过规定的失败次数,则认定haproxy 失败,否则认定haproxy 正常;举个例子比如haproxy 并发比较大,没有向应监控脚本的请求,监控脚本记录失败次数1,依次执行10次,如果监控失败次数小于5(比如1次),则认为haproxy 正常,进入下次的监控,而不是像0.2版监控脚本一样,只要有一次80端口故障立马就切换了! 
  8. #2 为了方便keepalived.sh 调用此脚本,将脚本写成daemon形式可以设定监控脚本检查间隔时间(默认2s执行一次),不再需要依赖crontab来执行了,为方便调试脚本添加以启动和关闭监控脚本功能! 
  9. #variables 
  10. eth1_ip=$(/sbin/ifconfig eth1 | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}') 
  11. #eth0_ip=$(/sbin/ifconfig eth0 | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}') 
  12. eth1_url="http://${eth1_ip}:80" 
  13. #eth0_url="http://${eth0_ip}:80" 
  14. check_time=2 #检查间隔时间,2s 
  15. check_count=8 #故障后检查次数,8次 
  16. fault_count=4 #故障次���大于(4次),则认为不可用 
  17. keepalived_dir=/usr/local/keepalived/ 
  18. fault_log="${keepalived_dir}fault.log" 
  19. #function 
  20. check_haproxy(){ 
  21. pid_file="${keepalived_dir}haproxy_check.pid" 
  22.  echo "$$" > "$pid_file" && echo `cat $pid_file` 
  23.  keepalived=`ps -C keepalived --no-header |wc -l` 
  24.  
  25.  while true;do 
  26.  #检查时间,2s 
  27.  sleep "$check_time" # 
  28.  
  29.  if curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then 
  30.  #if curl -IL -A "haproxy_check" "$eth0_url" > /dev/null 2>&1 && curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then 
  31.  # 
  32.  continue 
  33.  # 
  34.  else 
  35.  # 
  36.  check_failed=0 
  37.  #故障后检查次数,8次 
  38.  for((i=1;i<="$check_count";i++));do 
  39.  
  40.  check_date=$(date '+ %F_%T') 
  41.  
  42.  if ! curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then 
  43.  failed_count=$((check_failed++)) 
  44.  sleep 1 
  45.  fi 
  46.  #日志 
  47.  if [ "$keepalived" -ge "2" ];then 
  48.  echo -e "$check_date $eth1_url $check_failed \t fault" >> "$fault_log" 2>&1 
  49.  fi 
  50.  # 
  51.  done 
  52.  #检查失败次数大于故障次数,则停止keepalived 
  53.  if [ "$failed_count" -gt "$fault_count" ];then 
  54.  killall keepalived && /etc/init.d/network restart 
  55.  #mail -s 'haproxy and keepalived fault' root@localhost 
  56.  break 2 
  57.  fi 
  58.  fi 
  59.  done 
  60.  # 
  61. #main 
  62. case "$1" in 
  63.  start) 
  64.  check_haproxy 
  65.  ;; 
  66.  stop) 
  67.  #echo `cat $pid_file` 
  68.  kill `cat "${keepalived_dir}haproxy_check.pid"` 
  69.  ;; 
  70.  restart) 
  71.  echo "Sorry The next version will realize this function !!!" 
  72.  ;; 
  73.  *) 
  74.  echo "Usage: $0 {start|stop|restart}" 
  75.  exit 1 
  76.  ;; 
  77. esac 

    20 iptables 脚本

  1. $cat /root/sh/iptables.sh
  2. #!/bin/bash 
  3. #Define variable 
  4. IPTABLES=/sbin/iptables 
  5. INTRANET="100.100.100.0/24" 
  6. Company="123.x.x.x" 
  7. Company1="202.x.x.x" 
  8. sy="119.x.x.x/27" 
  9. sj1="5x.x.x.x/26" 
  10. sj2="5x.x.x.x/28" 
  11. sj3="5x.x.x.x/28" 
  12. $IPTABLES -F 
  13. $IPTABLES -X 
  14. $IPTABLES -P FORWARD DROP 
  15. $IPTABLES -P INPUT DROP 
  16. echo "#Allow all at the loopback and INTRANET and shunyi sjhl" 
  17. $IPTABLES -A INPUT -i lo -j ACCEPT 
  18. $IPTABLES -A INPUT -s $INTRANET -j ACCEPT 
  19. $IPTABLES -A INPUT -s $sy -j ACCEPT 
  20. $IPTABLES -A INPUT -s $sj1 -j ACCEPT 
  21. $IPTABLES -A INPUT -s $sj2 -j ACCEPT 
  22. $IPTABLES -A INPUT -s $sj3 -j ACCEPT 
  23. $IPTABLES -A INPUT -s $Company -j ACCEPT 
  24. $IPTABLES -A INPUT -s $Company1 -j ACCEPT 
  25. echo "#Accepting HTTP" 
  26. $IPTABLES -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT 
  27. echo "ALLOW PING" 
  28. #$IPTABLES -A INPUT -i eth0 -p icmp -j ACCEPT 
  29. echo "#Allow alive conn" 
  30. $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

21
22


23 memcached 安装脚本

  1. $ cat mem/memcached_install.sh 
  2. #!/bin/bash 
  3. #install memcached 
  4. #ver: 0.1bate 
  5. #20111230 by dongnan 
  6. #variables 
  7. dir="/usr/local/" 
  8. libevent_dir=${dir}libevent 
  9. memcached_dir=${dir}memcached 
  10. sbin_dir="${dir}sbin/" 
  11. #main 
  12. #check libevent 
  13. if [ ! -d "$libevent_dir" ];then 
  14.  tar zxvf libevent*.tar.gz 
  15.  cd libevent*/ 
  16.  ./configure --prefix="$libevent_dir" 
  17.  make && make install 
  18.  cd ../ 
  19. sleep 2 
  20.  if [ -d "$libevent_dir" ];then 
  21.  echo "libevent is ok" 
  22.  rm -rf libevent*/ 
  23.  else 
  24.  echo "error! can't install libevent please check ! Will now out of the script!" 
  25.  exit 1 
  26.  fi 
  27. else 
  28.  echo "libevent is already exists!" 
  29. fi 
  30. #memcached 
  31. if [ ! -d "$memcached_dir" ];then 
  32.  tar zxvf memcached*.tar.gz 
  33.  cd memcached*/ 
  34.  ./configure --prefix="$memcached_dir" --with-libevent="$libevent_dir" 
  35.  make && make install 
  36.  cd ../ 
  37. sleep 2 
  38.  if [ -d "$memcached_dir" ];then 
  39.  echo "memcached is ok" 
  40.  rm -rf memcached*/ 
  41.  ! ls "${sbin_dir}memcached.sh" > /dev/null 2>&1 && cp ./memcached.sh "$sbin_dir" && chmod 700 "${sbin_dir}memcached.sh" 
  42.  else 
  43.  echo "error! can't install memcached please check ! Will now out of the script!" 
  44.  exit 1 
  45.  fi 
  46. else 
  47.  echo "memcached is already exists!" 
  48. fi 

    24 memcached 服务脚本

  1. $ cat mem/memcached.sh 
  2. #!/bin/bash 
  3. #memcached 
  4. #ver:0.1bate 
  5. #20111230 by dongnan 
  6. #variables 
  7. mem_dir="/usr/local/memcached/" 
  8. mem_pid="/var/run/memcached.pid" 
  9. mem_sbin="${mem_dir}bin/memcached" 
  10. mem_size="64" 
  11. mem_ip=$(/sbin/ifconfig eth1 | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}') 
  12. mem_port="11211" 
  13. mem_max_con="10000" 
  14. #main 
  15. case "$1" in 
  16.  start) 
  17.  $mem_sbin -d -u root -m "$mem_size" -l "$mem_ip" -p "$mem_port" -c "$mem_max_con" -P "$mem_pid" 
  18.  ;; 
  19.  stop) 
  20.  kill $(cat $mem_pid) 
  21.  ;; 
  22.  *) 
  23.  echo "Usage: $0 {start|stop}" 
  24.  exit 1 
  25.  ;; 
  26. esac 

25# Nginx 分割日志脚本

  1. $ cat nginx_log.sh 
  2. #!/bin/bash 
  3. #split log 
  4. #Nginx 
  5. #Signal Action 
  6. #TERM, INT Terminate the server immediately 
  7. #QUIT Stop the server 
  8. #HUP Configuration changes, start new workers, graceful stop of old workers 
  9. #USR1 Reopen log files 
  10. #USR2 Upgrade the server executable 
  11. #WINCH Graceful Stop (parent process advise the children to exit) 
  12. #variables 
  13. log_dir=/data/logs/yum/ 
  14. log_date=$(date +'%F') 
  15. nginx_pid=/usr/local/nginx/logs/nginx.pid 
  16. keep_days=30 
  17. #old_log 
  18. find "$log_dir" -name "*\.log" -type f -mtime +${keep_days} -exec rm -rf {} \; 
  19. #new_log 
  20. if [ -e ${log_dir}access-${log_date}.log ];then 
  21.  echo "Already exists" && exit 1 
  22. else 
  23.  /bin/mv ${log_dir}access.log ${log_dir}access-${log_date}.log && /bin/kill -USR1 $(cat $nginx_pid) 
  24. fi 

26 # php 文件发布脚本

  1.  cat rsync_www.sh 
  2. #!/bin/bash 
  3. #/usr/bin/rsync -avz --password-file=/root/rsync_pw /var/www/test/test rsync@10.0.100.82::www/test 
  4. #echo "rsync_password" > rsync_pw && chmod 600 rsync_pw 
  5. #variables 
  6. #HOSTS="10.0.100.82 10.0.100.81 10.0.100.83" 
  7. HOSTS="10.0.100.82" 
  8. MOD=www 
  9. MAILADD="root@localhost" 
  10. DATE=`date +%F_%T` 
  11. src_dir=/var/www/html/ 
  12. rsync_pw=/root/sh/rsync_pw 
  13. rsync_user=dongnan 
  14. rsync_file_dir=/var/www/rsync/ 
  15. log_dir=/var/www/rsync/log/ 
  16. regular_replace=/data/ 
  17. #USERNAME=`logname` 
  18. if [ -n "$SUDO_USER" ];then 
  19. USERNAME=`echo $SUDO_USER` 
  20. else 
  21. USERNAME="root" 
  22. fi 
  23. #function 
  24. rsync_www(){ 
  25.  for host in $HOSTS;do 
  26.  #if /usr/bin/rsync -avz ${src_dir}$1 $host::${MOD}/$1;then 
  27.  if /usr/bin/rsync -avz --password-file="$rsync_pw" ${src_dir}$1 ${rsync_user}@${host}::${MOD}/$1;then 
  28.  echo "$DATE ${src_dir}$1 rsync to $host ok by $USERNAME" >> ${log_dir}rsync.ok 
  29.  else 
  30.  echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME" >> ${log_dir}rsync.error 
  31.  echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME."| mail -s "rsync error" $MAILADD 
  32.  fi 
  33. done 
  34. #main 
  35. #删除空白或空行 
  36. sed -ri 's/^[ \t]+//g;/^$/d' ${rsync_file_dir}rsync_file.list 
  37. sed -e 's#/var/www/html/##g' ${rsync_file_dir}rsync_file.list > ${log_dir}rsync.tmp 
  38. while read line;do 
  39.  if [ ! -d "$src_dir$line" -a ! -f "$src_dir$line" ];then 
  40.  echo "$DATE ${src_dir}$line is't existence by $USERNAME!!!" | tee -a ${log_dir}rsync.error | mail -s "rsync error" $MAILADD 
  41.  else 
  42.  rsync_www $line 
  43.  fi 
  44. done < ${log_dir}rsync.tmp 

27
28
29
30


31 cygwin 专用

  1. #!/bin/bash 
  2. #variables 
  3. pid_dir=/var/run/ 
  4. rsync_pid=${pid_dir}rsync.pid 
  5. rsync_src_dir="/cygdrive/f/WebSite/brandimg /cygdrive/f/WebSite/fashion.yoka.com" 
  6. loop_time=600 
  7. #function 
  8. #echo "$$" > "$rsync_pid" && cat "$rsync_pid" 
  9. rsync_start (){ 
  10.  while true;do 
  11.  for src in $rsync_src_dir ;do 
  12.  ls $src 
  13.  done 
  14.  sleep "$loop_time" 
  15.  done 
  16. rsync_pid (){ 
  17. ps aux |awk '/(bash|sleep|sh)$/ {print$1,$3}'| awk 'NR>1&&$NF==var{print line "\n"$0}{line=$0;var=$NF}' | awk '{print $1}'> /var/run/rsync.pid 
  18. rsync_stop (){ 
  19.  while read line;do 
  20.  #kill "$line" 
  21.  echo "$line" 
  22.  done < $rsync_pid 
  23. #main 
  24. case "$1" in 
  25.  start) 
  26.  rsync_start & 
  27.  rsync_pid 
  28.  ;; 
  29.  stop) 
  30.  rsync_stop 
  31.  ;; 
  32.  *) 
  33.  echo "Usage: $0 {start|stop}" 
  34.  exit 1 
  35.  ;; 
  36. esac 

# seq 一例子

  1. #!/bin/bash 
  2. #usage seq of loop 
  3. #NAME 
  4. # seq - print a sequence of numbers 
  5. #SYNOPSIS 
  6. # seq [OPTION]... LAST 
  7. # seq [OPTION]... FIRST LAST 
  8. # seq [OPTION]... FIRST INCREMENT LAST 
  9. #DESCRIPTION 
  10. # Print numbers from FIRST to LAST, in steps of INCREMENT. 
  11. for i in $(seq -s ':' 1 10);do 
  12.  echo $i 
  13. done 
  14. for i in {1..10};do 
  15.  echo $i 
  16. done 

# 测试Hadoop HDFS

  1. $ cat test_hdfs.sh
  2. #!/bin/bash 
  3. #test the hadoop HDFS 
  4. #date +%H%N 随机数 
  5. ######%N########nanoseconds (000000000..999999999) 亿分之一秒时间"纳秒" 
  6. ######%H########hour (00..23)以小时为时间戳
  7. #echo $RANDOM 
  8. #/dev/random 
  9. #variables 
  10. loop=5000 
  11. bs=5k 
  12. count=1 
  13. dfs_dir=/data/dfs/ 
  14. #main 
  15. for((j=1;j<=100;j++));do 
  16.  mkdir -p ${dfs_dir}$j 
  17. #echo $dfs_dir && exit 1 
  18. sleep 1 
  19. cat /dev/null > file 
  20. sleep 5 
  21. for ((i=1;i<=$loop;i++));do 
  22. date +%H%N >> file 
  23. done 
  24. sleep 10 
  25. while read line ;do 
  26. dd if=/dev/zero of=${dfs_dir}$j/$line bs=$bs count=$count > /dev/null 2>&1 && usleep 10000 
  27. #echo "$i" > ${dfs_dir}$line && usleep 10000 
  28. echo $line 
  29. done < file 
  30. done 

# 挂载CIFS

  1. #!/bin/bash 
  2. #completed by dongnan 
  3. #Mount windows CIFS 
  4. #variables 
  5. cifs_ip=192.168.1.100 
  6. cifs_user=administrator 
  7. cifs_pw=dongnan 
  8. cifs_share=aa 
  9. mount_point=/mnt/fat 
  10. mount=/sbin/mount.cifs 
  11. user_id=$(id -u) 
  12. #main 
  13. if [ "$user_id" -ne 0 ];then 
  14.  echo "must root or SUDO_USER !!" 
  15.  exit 1 
  16. fi 
  17. if mount | grep -E "$cifs_share|$cifs_ip" > /dev/null 2>&1;then 
  18.  echo "Has been mounted" 
  19.  exit 1 
  20. fi 
  21. $mount -o "username=$cifs_user,password=$cifs_pw" //${cifs_ip}/$cifs_share $mount_point 

# 统计windows 服务器 20120531

  1. cat find_windows.sh 
  2. #!/bin/bash 
  3. #test windows remote desktop 3389 port 
  4. 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

  1. cat find_esxi.sh 
  2. #!/bin/bash 
  3. #ESXi port 443 and 902 
  4. for ip in 0 1;do 
  5. 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 
  6. done 

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

  1. cat add-user.sh 
  2. #!/bin/bash 
  3. #add user for this host 
  4. #20120604 by dongnan 
  5. #variables 
  6. ip=10.0.0. 
  7. hosts='201 202 203 204 205' 
  8. user='dongnan' 
  9. pw='dongnan' 
  10. for host in $hosts;do 
  11.  echo "${ip}$host" 
  12.  ssh ${ip}$host "useradd $user && echo $pw | passwd --stdin $user && sed -ir '/^root.*ALL$/ a $user ALL=(ALL) ALL' /etc/sudoers" 
  13.  #ssh ${ip}$host userdel -r $user 
  14. done


本文出自 “dongnan” 博客。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论