shell-脚本集合

shell-脚本集合2

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. }
  21. for i in $hosts;do
  22. echo "${ip}$i"
  23. #    cat ~/.ssh/id_rsa.pub | ssh dn@${ip}$i "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat - >~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys"
  24. auto_ssh_copy_id $user@${ip}$i $password
  25. 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. #
  4. #variables
  5. user=`id -u`
  6. #
  7. if [ $user -ne 0 ];then
  8. echo "must root !!"
  9. exit 1
  10. fi
  11. for i in `ls /var/log/secure*`;do
  12. echo "$i"
  13. #cat /var/log/secure | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c
  14. cat "$i" | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c
  15. 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. #
  10. #Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
  11. #        [ -p <pidfile> ] [ -m <max megs> ]
  12. #        -v displays version ; -vv shows known build options.
  13. #        -d enters debug mode ; -db only disables background mode.
  14. #        -V enters verbose mode (disables quiet mode)
  15. #        -D goes daemon
  16. #        -q quiet mode : don't display messages
  17. #        -c check mode : only check config files and exit
  18. #        -n sets the maximum total # of connections (2000)
  19. #        -m limits the usable amount of memory (in MB)
  20. #        -N sets the default, per-proxy maximum # of connections (2000)
  21. #        -p writes pids of all children to this file
  22. #        -de disables epoll() usage even when available
  23. #        -ds disables speculative epoll() usage even when available
  24. #        -dp disables poll() usage even when available
  25. #        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.
  26. #variables
  27. haproxy_dir=/usr/local/haproxy/
  28. haproxy_conf=${haproxy_dir}haproxy.cfg
  29. haproxy_pid=${haproxy_dir}logs/haproxy.pid
  30. haproxy_cmd=${haproxy_dir}sbin/haproxy
  31. #test variables
  32. #file $haproxy_dir; file $haproxy_conf; file $haproxy_cmd; file $haproxy_pid
  33. if [ "$#" -eq "0" ];then
  34. echo "usage: $0 {start|stop|restart}"
  35. exit 1
  36. fi
  37. if [ "$1" = "start" ];then
  38. #echo $1
  39. $haproxy_cmd -f $haproxy_conf
  40. elif [ "$1" = "stop" ];then
  41. #echo $1
  42. kill `cat $haproxy_pid`
  43. elif [ "$1" = "restart" ];then
  44. #echo $1
  45. $haproxy_cmd -f $haproxy_conf -st `cat $haproxy_pid`
  46. else
  47. echo "usage: $0 arguments only start and stop or restart !"
  48. 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. echo '
  23. global
  24. log 127.0.0.1   local0
  25. maxconn 4096              #最大连接数
  26. chroot /usr/local/haproxy #安装目录
  27. uid 99                    #用户haproxy
  28. gid 99                    #组haproxy
  29. daemon                    #守护进程运行
  30. nbproc 1                  #进程数量
  31. pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
  32. defaults
  33. log     global
  34. mode    http               #7层 http;4层tcp
  35. option  httplog            #http 日志格式
  36. option  httpclose          #主动关闭http通道
  37. option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
  38. option  dontlognull
  39. maxconn 2000               #最大连接数
  40. contimeout      5000       #连接超时(毫秒)
  41. clitimeout      50000      #客户端超时(毫秒)
  42. srvtimeout      50000      #服务器超时(毫秒)
  43. frontend haproxy_test         #定义前端服务器(haproxy)
  44. bind 10.0.1.251:80    #监听地址
  45. default_backend server_pool  #指定后端服务器群
  46. #errorfile 502 /usr/local/haproxy/html/maintain.html
  47. #errorfile 503 /usr/local/haproxy/html/maintain.html
  48. #errorfile 504 /usr/local/haproxy/html/maintain.html
  49. backend server_pool           #定义后端服务器群(web server/apache/nginx/iis..)
  50. mode http
  51. option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
  52. #balance roundrobin    #负载均衡的方式,轮询方式
  53. balance leastconn     #负载均衡的方式,最小连接
  54. cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义
  55. option  httpchk HEAD /check.html #用来做健康检查html文档
  56. server server1 10.0.1.252:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
  57. server server2 10.0.1.253:80 cookie server2 check inter 2000 rise 3 fall 3 maxconn 120 weight 3
  58. server server3 10.0.1.254:80 cookie server3 check maxconn 90 rise 2 fall 3 weight 3
  59. #服务器定义:
  60. #cookie server1表示serverid为server1;
  61. #check inter 2000 是检测心跳频率(check 默认 );
  62. #rise 3 表示 3次正确认为服务器可用;
  63. #fall 3 表示 3次失败认为服务器不可用;
  64. #weight 表示权重。
  65. listen admin_stat                   #status
  66. bind *:8080                     #监听端口
  67. mode http                       #http的7层模式
  68. stats refresh 30s               #统计页面自动刷新时间
  69. stats uri /haproxy-stats        #统计页面URL
  70. stats realm Haproxy\ Statistics #统计页面密码框上提示文本
  71. stats auth admin:admin          #统计页面用户名和密码设置
  72. stats hide-version              #隐藏统计页面上HAProxy的版本信息
  73. stats admin if TRUE             #手工启用/禁用,后端服务器
  74. ' > "$ha_cfg" && sed -i '1 d' "$ha_cfg"
  75. }
  76. #install
  77. if [ ! -e "$ha_dir" ];then
  78. tar zxf haproxy*.tar.gz
  79. cd haproxy*/
  80. make TARGET=linux26 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && mkdir /usr/local/haproxy/{html,logs}
  81. cd ../
  82. #
  83. if [ ! -e "$ha_dir" ];then
  84. echo "error! can't install haproxy  please check ! Will now out of the script !"
  85. exit 1
  86. else
  87. ! grep 'haproxy' /etc/syslog.conf && echo 'local1.*            /var/log/haproxy.log' >> /etc/syslog.conf
  88. sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart
  89. install_ha_cfg
  90. rm -rf haproxy*/
  91. fi
  92. else
  93. echo "haproxy is already exists!"
  94. 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. #
  12. install_keepalived() {
  13. local keepalived_type=$1
  14. if [ ! -e "$keep_dir" ];then
  15. tar zxf keepalived*.tar.gz
  16. cd keepalived*/
  17. ./configure --prefix=$keep_dir
  18. make && make install
  19. cd ../
  20. if [ ! -e "$keep_dir" ];then
  21. echo "error! can't install keepalived  please check ! Will now out of the script !"
  22. exit 1
  23. else
  24. if [ "$keepalived_type" = "master" ];then
  25. echo 'master'
  26. cat keepalived.conf | sed -r 's/state BACKUP/state MASTER/g;s/nopreempt/#/g' > "$keep_conf"
  27. elif [ "$keepalived_type" = "backup" ];then
  28. echo 'backup'
  29. cat keepalived.conf >  "$keep_conf"
  30. else
  31. echo 'backup-2'
  32. cat keepalived.conf | sed -r 's/priority 150/priority 100/g;s/nopreempt/#/g' > "$keep_conf"
  33. fi
  34. cp check_haproxy.sh "$keep_dir"
  35. rm -rf keepalived*/
  36. fi
  37. else
  38. echo "keepalived is Already exists!"
  39. fi
  40. }
  41. #main
  42. case "$1" in
  43. master)
  44. install_keepalived $1
  45. ;;
  46. backup)
  47. install_keepalived $1
  48. ;;
  49. backup-2)
  50. install_keepalived $1
  51. ;;
  52. *)
  53. echo "Usage: $0 {master|backup|backup-2}"
  54. exit 1
  55. esac

15 keepalived 配置文档

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

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

19 haproxy 监控脚本

  1. # cat haproxy_check.sh
  2. #!/bin/bash
  3. #
  4. #monitor haproxy
  5. #
  6. #haproxy_chekc.sh ver 0.3 bate
  7. #20111218 by dongnan Email:dngood@sina.com
  8. #
  9. #0.3 bate版
  10. #1 添加了简单容错功能,80端口故障后会再检查多次,如果故障次数超过规定的失败次数,则认定haproxy 失败,否则认定haproxy 正常;举个例子比如haproxy 并发比较大,没有向应监控脚本的请求,监控脚本记录失败次数1,依次执行10次,如果监控失败次数小于5(比如1次),则认为haproxy 正常,进入下次的监控,而不是像0.2版监控脚本一样,只要有一次80端口故障立马就切换了!
  11. #2 为了方便keepalived.sh 调用此脚本,将脚本写成daemon形式可以设定监控脚本检查间隔时间(默认2s执行一次),不再需要依赖crontab来执行了,为方便调试脚本添加以启动和关闭监控脚本功能!
  12. #variables
  13. eth1_ip=$(/sbin/ifconfig eth1  | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}')
  14. #eth0_ip=$(/sbin/ifconfig eth0  | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}')
  15. eth1_url="http://${eth1_ip}:80"
  16. #eth0_url="http://${eth0_ip}:80"
  17. check_time=2         #检查间隔时间,2s
  18. check_count=8        #故障后检查次数,8次
  19. fault_count=4        #故障次数大于(4次),则认为不可用
  20. keepalived_dir=/usr/local/keepalived/
  21. fault_log="${keepalived_dir}fault.log"
  22. #function
  23. check_haproxy(){
  24. pid_file="${keepalived_dir}haproxy_check.pid"
  25. echo "$$" > "$pid_file" && echo `cat $pid_file`
  26. keepalived=`ps -C keepalived --no-header |wc -l`
  27. while true;do
  28. #检查时间,2s
  29. sleep "$check_time"             #
  30. if curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
  31. #if curl -IL -A "haproxy_check" "$eth0_url" > /dev/null 2>&1  &&  curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
  32. #
  33. continue
  34. #
  35. else
  36. #
  37. check_failed=0
  38. #故障后检查次数,8次
  39. for((i=1;i<="$check_count";i++));do
  40. check_date=$(date '+ %F_%T')
  41. if ! curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
  42. failed_count=$((check_failed++))
  43. sleep 1
  44. fi
  45. #日志
  46. if [ "$keepalived" -ge "2" ];then
  47. echo -e "$check_date $eth1_url $check_failed \t fault" >> "$fault_log" 2>&1
  48. fi
  49. #
  50. done
  51. #检查失败次数大于故障次数,则停止keepalived
  52. if [ "$failed_count" -gt "$fault_count" ];then
  53. killall keepalived && /etc/init.d/network restart
  54. #mail -s 'haproxy and keepalived fault' root@localhost
  55. break 2
  56. fi
  57. fi
  58. done
  59. #
  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. #
  20. sleep 2
  21. if [ -d "$libevent_dir" ];then
  22. echo "libevent is ok"
  23. rm -rf libevent*/
  24. else
  25. echo "error! can't install libevent please check ! Will now out of the script!"
  26. exit 1
  27. fi
  28. else
  29. echo "libevent is already exists!"
  30. fi
  31. #memcached
  32. if [ ! -d "$memcached_dir" ];then
  33. tar zxvf memcached*.tar.gz
  34. cd memcached*/
  35. ./configure --prefix="$memcached_dir" --with-libevent="$libevent_dir"
  36. make && make install
  37. cd ../
  38. #
  39. sleep 2
  40. if [ -d "$memcached_dir" ];then
  41. echo "memcached is ok"
  42. rm -rf memcached*/
  43. ! ls "${sbin_dir}memcached.sh" > /dev/null 2>&1 && cp ./memcached.sh "$sbin_dir" && chmod 700 "${sbin_dir}memcached.sh"
  44. else
  45. echo "error! can't install memcached please check ! Will now out of the script!"
  46. exit 1
  47. fi
  48. else
  49. echo "memcached is already exists!"
  50. 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. #
  5. #Nginx
  6. #Signal Action
  7. #TERM, INT Terminate the server immediately
  8. #QUIT Stop the server
  9. #HUP Configuration changes, start new workers, graceful stop of old workers
  10. #USR1 Reopen log files
  11. #USR2 Upgrade the server executable
  12. #WINCH Graceful Stop (parent process advise the children to exit)
  13. #variables
  14. log_dir=/data/logs/yum/
  15. log_date=$(date +'%F')
  16. nginx_pid=/usr/local/nginx/logs/nginx.pid
  17. keep_days=30
  18. #old_log
  19. find "$log_dir" -name "*\.log" -type f -mtime +${keep_days} -exec rm -rf {} \;
  20. #new_log
  21. if [ -e ${log_dir}access-${log_date}.log ];then
  22. echo "Already exists" && exit 1
  23. else
  24. /bin/mv ${log_dir}access.log ${log_dir}access-${log_date}.log && /bin/kill -USR1 $(cat $nginx_pid)
  25. fi

26 # php 文件发布脚本

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

27

28

29

30

31 cygwin 专用

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

# seq 一例子

  1. #!/bin/bash
  2. #usage seq of loop
  3. #NAME
  4. #       seq - print a sequence of numbers
  5. #
  6. #SYNOPSIS
  7. #       seq [OPTION]... LAST
  8. #       seq [OPTION]... FIRST LAST
  9. #       seq [OPTION]... FIRST INCREMENT LAST
  10. #
  11. #DESCRIPTION
  12. #       Print numbers from FIRST to LAST, in steps of INCREMENT.
  13. for i in $(seq -s ':' 1 10);do
  14. echo $i
  15. done
  16. #
  17. for i in {1..10};do
  18. echo $i
  19. 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. #
  19. sleep 1
  20. cat /dev/null > file
  21. #
  22. sleep 5
  23. for ((i=1;i<=$loop;i++));do
  24. date +%H%N >> file
  25. done
  26. #
  27. sleep 10
  28. while read line ;do
  29. dd if=/dev/zero of=${dfs_dir}$j/$line bs=$bs count=$count > /dev/null 2>&1 && usleep 10000
  30. #echo "$i" > ${dfs_dir}$line && usleep 10000
  31. echo $line
  32. done < file
  33. 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 51CTO博客,原文链接:http://blog.51cto.com/dngood/703347

shell-脚本集合相关推荐

  1. 常用shell脚本集合

    常用shell脚本集合 !/bin/sh # 在脚本第一行脚本头 # sh为当前系统默认shell,可指定为bash等shell sh -x # 执行过程 sh -n # 检查语法 (a=bbk) # ...

  2. Linux下Shell 备份脚本集合

    说到Shell大家都不陌生,Shell是Linux下批处理脚本文件,类似windows下的Vbs脚本等等,能协助我们完成一些自动化的任务,我们前面也介绍了一些Shell脚本协助我们完成了一些计划任务, ...

  3. Shell脚本基础介绍

    shell基础简介: 编写脚本通常使用某种基于解释器的编程语言.而shell脚本不过就是一些文件,我们能将一系列需要执行的命令写入其中,然后通过shell来执行这些脚本. 进入Linux系统(Ubun ...

  4. 一篇文章学懂Shell脚本

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...

  5. 7月11日任务 shell介绍、shell结构和执行 、date命令用法、shell脚本中的变量

    2019独角兽企业重金招聘Python工程师标准>>> 20.1 Shell脚本介绍 • shell是一种脚本语言  aming_linux  blog.lishiming.net ...

  6. 69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量

    2019独角兽企业重金招聘Python工程师标准>>> 1.shell脚本介绍: shell是一种脚本语言和传统的开发语言相比,会比较简单: shell有自己语法,可以支持逻辑判断. ...

  7. shell脚本编程学习之路-shell数组

    1.数组的介绍 在开发shell脚本时,定义变量采用的形式为"a=1;b=2;c=3",变量多了再一个一个定义就比较麻烦,并且要是有多个不确定的变量内容也会难以进行变量控制,于是为 ...

  8. Linux阶段总结shell脚本

    shell脚本知识储备汇总 语言类型 强类型:定义变量必须指定类型:参与的运算必须要符合类型要求(字符串不能和数值相加等):调用未声明变量会报错弱类型:定义变量无需指定类型:默认为字符型参与运算时会自 ...

  9. Linux配置脚本导出运行,linux服务器部署jar包以及shell脚本的书写

    背景:记录在linux环境下部署jar程序的过程 1 部署过程记录 1.1 程序结构 这里的main函数就在DemRest2.java 文件中. 为了部署方便,要做到以下两点: 1 在导出的jar包中 ...

  10. Linux 中启用 Shell 脚本的调试模式

    shell 脚本调试系列 Linux 中启用 Shell 脚本的调试模式 在 Shell 脚本中执行语法检查调试模式 在 Shell 脚本中跟踪调试命令的执行 概述 脚本是存储在一个文件的一系列命令. ...

最新文章

  1. 我的csdnmark
  2. vue的Virtual Dom实现- snabbdom解密
  3. idea springboot热部署无效问题
  4. Redmine使用手册
  5. 14. GD32F103C8T6入门教程-Systick定时器
  6. xargs命令_Linux xargs命令:一个给其他命令传递参数的过滤器
  7. 单线程多线程_理解线程,就会由单线程向多线程进军
  8. Python 最抢手、Java 最流行,前线程序员揭秘 2019 软件开发现状
  9. Windows Server 启用或关闭ping端口
  10. python 百度识图_python 调用百度识图识取本地图片
  11. 《程序员》杂志 · 2017 精华本
  12. 【四足机器人那些事】足端轨迹规划(一)
  13. 【文本检测与识别-白皮书-3.1】第四节:算法模型 2
  14. 你的睡眠时间和睡眠质量达标了么
  15. Newton-Raphson切线法解高次方程近似根
  16. 闵帆老师《论文写作》心得体会
  17. uniapp自定义搜索组件实现全部功能
  18. CSS学习笔记--文本格式化
  19. 嵌入式软件设计第7次实验报告-140201235-陈宇
  20. 利用GAE+WallProxy-plugins搭建个人代理服务器

热门文章

  1. 如何理解 “Dense object detection“中的dense?
  2. c226打印机驱动安装_爱普生喷墨打印机安装WIFI驱动
  3. javlib有新地址吗,《郭杰自杀》_郭杰自杀_NEWS下载网
  4. 员工上班时间内高效率逛淘宝,企业如何解决?
  5. PHP支付-Stripe支付对接支付宝
  6. SQL LIKE操作符、通配符以及通配符的使用
  7. 产品助理实习day2
  8. McAfee:黑客借"极光漏洞"获取企业源代码
  9. 普速铁路中级工技能精选100题
  10. BGP SOO 防环机制