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