shell-腳本集合php
shell-腳本集合2html
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
linux
- #!/bin/bash
- #20111015 by dongnan
- #test the url 502
- for((i=1;i<=50;i++));do
- if curl -LI http://www.a.com/set.php?id=123456 2>&1 | grep '502' > /dev/null ;then
- echo "echo $i 502"
- fi
- sleep 1;
- done
3 test cdn ip loop nginx
- #!/bin/bash
- #20111101 by dongnan
- #test the cdn ip loop
- for((i=1;i<=60;i++));do
- ping -c 1 *.cdns.com | grep -v PING | grep -i 'from' | awk '{print $4}' | sed -r 's/(:)|(\-BJ\-CNC)//g'
- done
測試web
- ./cdn-loop.sh | sort | uniq -c
- 20 123.1.1.1
- 20 123.1.2.2
- 20 21.1.3.3
4 測試 apache vhostredis
- grep -vE '^#|^[ \t].&#' httpd-vhosts.conf | grep ServerName | awk '{print $2}' >> 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 *.*.*.*
自動: 須要使用 expect 命令apache
- 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 "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;}
- }";
- }
- for i in $hosts;do
- echo "${ip}$i"
- # cat ~/.ssh/id_rsa.pub | ssh dn@${ip}$i "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat - >~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys"
- auto_ssh_copy_id $user@${ip}$i $password
- done
6 批量部署 .bash_profile ,前提須要 ssh經過私匙登錄ssh服務器windows
關於如何配置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="\[\e[32;1m\]$ip\[\e[0m\][\W]\$ "
- export PS1
- fi
- echo "[LOAD]";cat /proc/loadavg;echo "[DISK]";df -Th
9 統計用戶登錄次數與 ip 地址
- #!/bin/bash
- #chek login user
- #
- #variables
- user=`id -u`
- #
- if [ $user -ne 0 ];then
- echo "must root !!"
- exit 1
- fi
- for i in `ls /var/log/secure*`;do
- echo "$i"
- #cat /var/log/secure | grep -i ' Accepted password' | awk '{print $9, $11}' | sort | uniq -c
- cat "$i" | 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 "find /var/www/bbs/ -type d -name 'day*' | xargs du -h | awk '{print \$1}'"
- ssh ${ip}$host "du -sh /var/www/bbs/"
- done
#ssh 傳遞 awk 命令時 "$" 須要使用"\"轉義字符,例如:
- ssh ${ip}$host "df -h | grep -E '^/dev.*(/|/usr|/var)$' | awk '{print \$5,\$6}'"
#統計硬盤使用狀況
- 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 "${ip}$host "
- ssh ${ip}$host "df -h | grep -E '^/dev.*(/|/usr|/var)$' | awk '{print \$6,\$5}'"
- done
- # 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 <w@1wt.eu>
- #
- #Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
- # [ -p <pidfile> ] [ -m <max megs> ]
- # -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 [ "$#" -eq "0" ];then
- echo "usage: $0 {start|stop|restart}"
- exit 1
- fi
- if [ "$1" = "start" ];then
- #echo $1
- $haproxy_cmd -f $haproxy_conf
- elif [ "$1" = "stop" ];then
- #echo $1
- kill `cat $haproxy_pid`
- elif [ "$1" = "restart" ];then
- #echo $1
- $haproxy_cmd -f $haproxy_conf -st `cat $haproxy_pid`
- else
- echo "usage: $0 arguments only start and stop or restart !"
- fi
12 cpu load 測試
- $ cat load.sh
- #! /bin/sh
- # filename killcpu.sh
- for i in `seq $1`
- do
- echo -ne "
- i=0;
- while true
- do
- ii=i+1;
- done" | /bin/sh &
- pid_array[$i]=$! ;
- done
- for i in "${pid_array[@]}"; do
- echo 'kill ' $i ';';
- done
- 使用方法很簡單,參數3表示消耗3顆CPU的資源,運行後,會有一堆 kill 命令,方便 kill 進程:
- [root@test02 ~]# ./killcpu.sh 3
- kill 30104 ;
- kill 30106 ;
- kill 30108 ;
- 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 "$dir, $ha_dir, $ha_cfg, $kernel, $pcre"
- #check
- if [ ! "$kernel" -o "$pcre" -lt "2" ];then
- 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'"
- 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 #手工啓用/禁用,後端服務器
- ' > "$ha_cfg" && sed -i '1 d' "$ha_cfg"
- }
- #install
- if [ ! -e "$ha_dir" ];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 "$ha_dir" ];then
- echo "error! can't install haproxy please check ! Will now out of the script !"
- exit 1
- else
- ! grep 'haproxy' /etc/syslog.conf && echo 'local1.* /var/log/haproxy.log' >> /etc/syslog.conf
- sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart
- install_ha_cfg
- rm -rf haproxy*/
- fi
- else
- echo "haproxy is already exists!"
- fi
- $ cat keepalived_install.sh
- #!/bin/bash
- #install keepalived
- #20111209 by dongnan
- #variables
- keep_dir="/usr/local/keepalived/"
- keep_conf="${keep_dir}keepalived.conf"
- haproxy_dir="/usr/local/haproxy/"
- #echo "$keep_dir $keep_conf $haproxy_dir"
- #function
- #
- install_keepalived() {
- local keepalived_type=$1
- if [ ! -e "$keep_dir" ];then
- tar zxf keepalived*.tar.gz
- cd keepalived*/
- ./configure --prefix=$keep_dir
- make && make install
- cd ../
- if [ ! -e "$keep_dir" ];then
- echo "error! can't install keepalived please check ! Will now out of the script !"
- exit 1
- else
- if [ "$keepalived_type" = "master" ];then
- echo 'master'
- cat keepalived.conf | sed -r 's/state BACKUP/state MASTER/g;s/nopreempt/#/g' > "$keep_conf"
- elif [ "$keepalived_type" = "backup" ];then
- echo 'backup'
- cat keepalived.conf > "$keep_conf"
- else
- echo 'backup-2'
- cat keepalived.conf | sed -r 's/priority 150/priority 100/g;s/nopreempt/#/g' > "$keep_conf"
- fi
- cp check_haproxy.sh "$keep_dir"
- rm -rf keepalived*/
- fi
- else
- echo "keepalived is Already exists!"
- fi
- }
- #main
- case "$1" in
- master)
- install_keepalived $1
- ;;
- backup)
- install_keepalived $1
- ;;
- backup-2)
- install_keepalived $1
- ;;
- *)
- echo "Usage: $0 {master|backup|backup-2}"
- exit 1
- esac
- # 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
- }
- }
- # cat /usr/local/sbin/keepalived.sh
- #!/bin/bash
- #keepalived
- #ver:0.1bate
- #20111211 by dongnan
- #variables
- keep_dir="/usr/local/keepalived/"
- keep_conf="${keep_dir}keepalived.conf"
- keep_sbin="${keep_dir}sbin/keepalived"
- haproxy_check="${keep_dir}haproxy_check.sh"
- haproxy_ps=$(ps -C haproxy --no-header |wc -l)
- #main
- case "$1" in
- start)
- if [ "$haproxy_ps" -ge "1" ];then
- $keep_sbin -f $keep_conf && $haproxy_check start &
- else
- echo "not found haproxy processes,please startup haproxy "
- exit 1
- fi
- ;;
- stop)
- killall keepalived && $haproxy_check stop &
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- 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 "$1" in
- keepalived_type)
- #
- bash ./haproxy_install.sh
- bash ./keepalived_install.sh "$2"
- #
- ! ls "${sbin_dir}haproxy.sh" > /dev/null 2>&1 && cp ./haproxy.sh "$sbin_dir"
- ! ls "${sbin_dir}keepalived.sh" > /dev/null 2>&1 && cp ./keepalived.sh "$sbin_dir"
- ! ls "${keepalived_dir}haproxy_check.sh" > /dev/null 2>&1 && cp ./haproxy_check.sh "$keepalived_dir"
- chmod 700 "${keepalived_dir}haproxy_check.sh"
- #
- ! grep 'haproxy.sh' "$startup" && echo "${sbin_dir}haproxy.sh start" >> "$startup"
- ! grep 'keepalived.sh' "$startup" && echo "${sbin_dir}keepalived.sh start" >> "$startup"
- ;;
- *)
- echo "Usage: $0 {keepalived_type backup}"
- echo " keepalived_type:master|backup|backup-2"
- exit 1
- ;;
- esac
- # 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" " '{print $1}')
- #eth0_ip=$(/sbin/ifconfig eth0 | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}')
- eth1_url="http://${eth1_ip}:80"
- #eth0_url="http://${eth0_ip}:80"
- check_time=2 #檢查間隔時間,2s
- check_count=8 #故障後檢查次數,8次
- fault_count=4 #故障次數大於(4次),則認爲不可用
- keepalived_dir=/usr/local/keepalived/
- fault_log="${keepalived_dir}fault.log"
- #function
- check_haproxy(){
- pid_file="${keepalived_dir}haproxy_check.pid"
- echo "$$" > "$pid_file" && echo `cat $pid_file`
- keepalived=`ps -C keepalived --no-header |wc -l`
- while true;do
- #檢查時間,2s
- sleep "$check_time" #
- if curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
- #if curl -IL -A "haproxy_check" "$eth0_url" > /dev/null 2>&1 && curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
- #
- continue
- #
- else
- #
- check_failed=0
- #故障後檢查次數,8次
- for((i=1;i<="$check_count";i++));do
- check_date=$(date '+ %F_%T')
- if ! curl -IL -A "haproxy_check" "$eth1_url" > /dev/null 2>&1 ;then
- failed_count=$((check_failed++))
- sleep 1
- fi
- #日誌
- if [ "$keepalived" -ge "2" ];then
- echo -e "$check_date $eth1_url $check_failed \t fault" >> "$fault_log" 2>&1
- fi
- #
- done
- #檢查失敗次數大於故障次數,則中止keepalived
- if [ "$failed_count" -gt "$fault_count" ];then
- killall keepalived && /etc/init.d/network restart
- #mail -s 'haproxy and keepalived fault' root@localhost
- break 2
- fi
- fi
- done
- #
- }
- #main
- case "$1" in
- start)
- check_haproxy
- ;;
- stop)
- #echo `cat $pid_file`
- kill `cat "${keepalived_dir}haproxy_check.pid"`
- ;;
- restart)
- echo "Sorry The next version will realize this function !!!"
- ;;
- *)
- echo "Usage: $0 {start|stop|restart}"
- exit 1
- ;;
- esac
20 iptables 腳本
- $cat /root/sh/iptables.sh
- #!/bin/bash
- #Define variable
- IPTABLES=/sbin/iptables
- INTRANET="100.100.100.0/24"
- Company="123.x.x.x"
- Company1="202.x.x.x"
- sy="119.x.x.x/27"
- sj1="5x.x.x.x/26"
- sj2="5x.x.x.x/28"
- sj3="5x.x.x.x/28"
- $IPTABLES -F
- $IPTABLES -X
- $IPTABLES -P FORWARD DROP
- $IPTABLES -P INPUT DROP
- echo "#Allow all at the loopback and INTRANET and shunyi sjhl"
- $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 "#Accepting HTTP"
- $IPTABLES -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT
- echo "ALLOW PING"
- #$IPTABLES -A INPUT -i eth0 -p icmp -j ACCEPT
- echo "#Allow alive conn"
- $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
21
22
- $ cat mem/memcached_install.sh
- #!/bin/bash
- #install memcached
- #ver: 0.1bate
- #20111230 by dongnan
- #variables
- dir="/usr/local/"
- libevent_dir=${dir}libevent
- memcached_dir=${dir}memcached
- sbin_dir="${dir}sbin/"
- #main
- #check libevent
- if [ ! -d "$libevent_dir" ];then
- tar zxvf libevent*.tar.gz
- cd libevent*/
- ./configure --prefix="$libevent_dir"
- make && make install
- cd ../
- #
- sleep 2
- if [ -d "$libevent_dir" ];then
- echo "libevent is ok"
- rm -rf libevent*/
- else
- echo "error! can't install libevent please check ! Will now out of the script!"
- exit 1
- fi
- else
- echo "libevent is already exists!"
- fi
- #memcached
- if [ ! -d "$memcached_dir" ];then
- tar zxvf memcached*.tar.gz
- cd memcached*/
- ./configure --prefix="$memcached_dir" --with-libevent="$libevent_dir"
- make && make install
- cd ../
- #
- sleep 2
- if [ -d "$memcached_dir" ];then
- echo "memcached is ok"
- rm -rf memcached*/
- ! ls "${sbin_dir}memcached.sh" > /dev/null 2>&1 && cp ./memcached.sh "$sbin_dir" && chmod 700 "${sbin_dir}memcached.sh"
- else
- echo "error! can't install memcached please check ! Will now out of the script!"
- exit 1
- fi
- else
- echo "memcached is already exists!"
- fi
- $ cat mem/memcached.sh
- #!/bin/bash
- #memcached
- #ver:0.1bate
- #20111230 by dongnan
- #variables
- mem_dir="/usr/local/memcached/"
- mem_pid="/var/run/memcached.pid"
- mem_sbin="${mem_dir}bin/memcached"
- mem_size="64"
- mem_ip=$(/sbin/ifconfig eth1 | sed -n '/inet addr:/ s/inet addr://pg' | awk -F" " '{print $1}')
- mem_port="11211"
- mem_max_con="10000"
- #main
- case "$1" in
- start)
- $mem_sbin -d -u root -m "$mem_size" -l "$mem_ip" -p "$mem_port" -c "$mem_max_con" -P "$mem_pid"
- ;;
- stop)
- kill $(cat $mem_pid)
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- 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 "$log_dir" -name "*\.log" -type f -mtime +${keep_days} -exec rm -rf {} \;
- #new_log
- if [ -e ${log_dir}access-${log_date}.log ];then
- echo "Already exists" && 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 "rsync_password" > rsync_pw && chmod 600 rsync_pw
- #variables
- #HOSTS="10.0.100.82 10.0.100.81 10.0.100.83"
- HOSTS="10.0.100.82"
- MOD=www
- MAILADD="root@localhost"
- 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 "$SUDO_USER" ];then
- USERNAME=`echo $SUDO_USER`
- else
- USERNAME="root"
- 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="$rsync_pw" ${src_dir}$1 ${rsync_user}@${host}::${MOD}/$1;then
- echo "$DATE ${src_dir}$1 rsync to $host ok by $USERNAME" >> ${log_dir}rsync.ok
- else
- echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME" >> ${log_dir}rsync.error
- echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME."| mail -s "rsync error" $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 "$src_dir$line" -a ! -f "$src_dir$line" ];then
- echo "$DATE ${src_dir}$line is't existence by $USERNAME!!!" | tee -a ${log_dir}rsync.error | mail -s "rsync error" $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="/cygdrive/f/WebSite/brandimg /cygdrive/f/WebSite/fashion.yoka.com"
- loop_time=600
- #function
- #echo "$$" > "$rsync_pid" && cat "$rsync_pid"
- rsync_start (){
- while true;do
- for src in $rsync_src_dir ;do
- ls $src
- done
- sleep "$loop_time"
- done
- }
- rsync_pid (){
- 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
- }
- rsync_stop (){
- while read line;do
- #kill "$line"
- echo "$line"
- done < $rsync_pid
- }
- #main
- case "$1" in
- start)
- rsync_start &
- rsync_pid
- ;;
- stop)
- rsync_stop
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- 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) 億分之一秒時間"納秒"
- ######%H########hour (00..23)以小時爲時間戳
- #echo $RANDOM
- #/dev/random
- #variables
- loop=5000
- bs=5k
- count=1
- dfs_dir=/data/dfs/
- #main
- for((j=1;j<=100;j++));do
- mkdir -p ${dfs_dir}$j
- #echo $dfs_dir && exit 1
- #
- sleep 1
- cat /dev/null > file
- #
- sleep 5
- for ((i=1;i<=$loop;i++));do
- date +%H%N >> 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 "$i" > ${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 [ "$user_id" -ne 0 ];then
- echo "must root or SUDO_USER !!"
- exit 1
- fi
- if mount | grep -E "$cifs_share|$cifs_ip" > /dev/null 2>&1;then
- echo "Has been mounted"
- exit 1
- fi
- $mount -o "username=$cifs_user,password=$cifs_pw" //${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 "${ip}$host"
- ssh ${ip}$host "useradd $user && echo $pw | passwd --stdin $user && sed -ir '/^root.*ALL$/ a $user ALL=(ALL) ALL' /etc/sudoers"
- #ssh ${ip}$host userdel -r $user
- done
#