shell编程1000实列

1,keepalive剔除后端服务

#!/bin/bash
#行数请自定义,默认8行
if [ X$2 == X ];then
echo "error: IP null"
read
exit
fi
case $1 in
del)
sed -i '/real_server.*'$2'.*8888/,+8 s/^/#/' /etc/keepalived/keepalived.conf
/etc/init.d/keepalived reload
;;
add)
sed -i '/real_server.*'$2'.*8888/,+8 s/^#//' /etc/keepalived/keepalived.conf
/etc/init.d/keepalived reload
;;
*)
echo "Parameter error"
;;
esac

2,ssh批量执行命令

#版本1
#!/bin/bash
while read line
do
Ip=`echo $line|awk '{print $1}'`
Passwd=`echo $line|awk '{print $2}'`
ssh -n localhost "cat -"
sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no root@$Ip "id"
done

#版本2
#!/bin/bash
Iplist=`awk '{print $1}' iplist.txt`
for Ip in $Iplist
do
Passwd=`awk '/'$Ip'/{print $2}' iplist.txt`
sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no root@$Ip "id"
done

3,两日期间隔天数

D1=`date -d '20070409' +"%s"`
D2=`date -d '20070304 ' +"%s"`
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))

4,远程打包

ssh -n $ip 'find '$path' /data /opt -type f -name "*.sh" -or -name "*.py" -or -name "*.pl" |xargs tar zcvpf /tmp/data_backup.tar.gz'

5,判断日期是否合法

#!/bin/sh
while read a
do
if echo $a | grep -q "-" && date -d $a +%Y%m%d > /dev/null 2>&1
then
if echo $a | grep -e '^[0-9]\{4\}-[01][0-9]-[0-3][0-9]$'
then
break
else
echo "您输入的日期不合法,请从新输入!"
fi
else
echo "您输入的日期不合法,请从新输入!"
fi
done
echo "日期为$a"

6,简单防ddos简本

#!/bin/bash
#netstat -an|grep :80|awk '{ print $5 }'|sort|awk -F: '{print $1}'|uniq -c|awk '$1 > 40'|awk '$2 != ""'>>/usr/local/src/black.txt

/bin/netstat -an|grep :443|awk '{ print $5 }'|sort|awk -F: '{print $1}'|uniq -c|awk ' $1 >150 '|awk '$2 != "0.0.0.0"'|awk '$2 != ""'>>/usr/local/src/black.txt
cnt=0
for i in `awk '{print $2}' /usr/local/src/black.txt`
do
if [ ! -z "$i" ];
then
COUNT=`grep $i /usr/local/src/black.txt | awk '{print $1}'`
DEFINE="150"
ZERO="0"
if [ $COUNT -gt $DEFINE ];
then
grep $i /usr/local/src/white.txt > /dev/null
if [ $? -gt $ZERO ];
then
IPEX=`iptables -nL | grep "$i"`
if [ -z "$IPEX" ];
then
echo "$COUNT $i"
iptables -I INPUT -s $i -j DROP
echo -e "[`date "+%Y-%m-%d %T"`] IP: $i\tCOUNT: $COUNT" >> /usr/local/src/banned.log
fi
fi
fi
((cnt=cnt+1))
fi
done
echo "[`date "+%Y-%m-%d %T"`] Script runned, found $cnt." >> /usr/local/src/run.log

7,shell 与运算

#!/bin/bash

a=24
b=47

if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
then
echo "Test #1 succeeds."
else
echo "Test #1 fails."
fi

8,文件夹复制

#!/bin/bash
E_NOARGS=65

if [ -z "$1" ] # Exit if no argument given.
then
echo "Usage: `basename $0` directory-to-copy-to"
exit $E_NOARGS
fi

ls . | xargs -i -t cp ./{} $1

9,算1+到1000

#!/bin/bash

i=0
n=1                                                  
while  [ $n -lt 1001 ]; do                                 
i=$(( $i + $n ))                                        
n=$(( $n + 1 ))
done
echo  $i
2,最简单的方法
seq -s + 1 1000|bc
10,mysql备份脚本'
#!/bin/bash
nowdate=`date +%Y%m%d`

olddate=`date -d -7day +%Y%m%d`
mysqldump=/data/mysql/bin/mysqldump
user=xxx
pass=xxxxxxx
pwd=/data/mysqlbackup
$mysqldump -u$user -p$pass database>$pwd/database$nowdate.sql
cd $pwd
rm databae$olddate.sql -rf

11,nginx分割日志

#!/bin/bash
LOG_PATH=/user/nginx/logs/
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
PID=/user/nginx/nginx.pid
#分割日志
mv ${LOG_PATH}xxxx-access.log ${LOG_PATH}xxxx-access-${YESTERDAY}.log
#向Nginx主进程发送USR1信号,重新打开日志文件
kill -USR1 `cat ${PID}`

更多教程

 

转载于:https://www.cnblogs.com/hu1313/p/10064118.html

你可能感兴趣的:(shell编程1000实列)