9 ¸öÊÊÓà shell ¾ç±¾£¬½¨ÒéÕä²Ø£¡
Dos ¹¥»÷Ìá·À£¨×Ô¶¯ÆÁÕϹ¥»÷ IP£©
#!/bin/bashDATE=$(date +%d/%b/%Y:%H:%M)LOG_FILE=/usr/local/nginx/logs/demo2.access.logABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}')for IP in $ABNORMAL_IP; do if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then iptables -I INPUT -s $IP -j DROP echo "$(date +'%F_%T') $IP" >> /tmp/drop_ip.log fidone
µÇ¼ºó¸´ÖÆ
Linux ϵͳ·¢Ë͸澯¾ç±¾
# yum install mailx# vi /etc/mail.rcset from=baojingtongzhi@163.com smtp=smtp.163.comset smtp-auth-user=baojingtongzhi@163.com smtp-auth-password=123456set smtp-auth=login
µÇ¼ºó¸´ÖÆ
MySQL Êý¾Ý¿â±¸·Ýµ¥Ñ»·
#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=123.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; do BACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sql if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; then echo "$BACKUP_NAME ±¸·Ýʧ°Ü!" fidone
µÇ¼ºó¸´ÖÆ
MySQL Êý¾Ý¿â±¸·Ý¶àÑ»·
#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=123.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; do BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE} [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null) for TABLE in $TABLE_LIST; do BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then echo "$BACKUP_NAME ±¸·Ýʧ°Ü!" fi donedone
µÇ¼ºó¸´ÖÆ
Nginx »á¼û»á¼ûÈÕÖ¾°´ÌìÇÐ¸î ¹Ø×¢LinuxÖÐÎÄÉçÇø
#!/bin/bashLOG_DIR=/usr/local/nginx/logsYESTERDAY_TIME=$(date -d "yesterday" +%F)LOG_MONTH_DIR=$LOG_DIR/$(date +"%Y-%m")LOG_FILE_LIST="default.access.log"for LOG_FILE in $LOG_FILE_LIST; do [ ! -d $LOG_MONTH_DIR ] && mkdir -p $LOG_MONTH_DIR mv $LOG_DIR/$LOG_FILE $LOG_MONTH_DIR/${LOG_FILE}_${YESTERDAY_TIME}donekill -USR1 $(cat /var/run/nginx.pid)
µÇ¼ºó¸´ÖÆ
Nginx »á¼ûÈÕÖ¾ÆÊÎö¾ç±¾
#!/bin/bash# ÈÕÖ¾ÃûÌÃ: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"LOG_FILE=$1echo "ͳ¼Æ»á¼û×î¶àµÄ10¸öIP"awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr |head -10echo "----------------------"echo "ͳ¼Æʱ¼ä¶Î»á¼û×î¶àµÄIP"awk '$4>="[01/Dec/2018:13:20:25" && $4<="[27/Nov/2018:16:20:49"{a[$1]++}END{for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr|head -10echo "----------------------"echo "ͳ¼Æ»á¼û×î¶àµÄ10¸öÒ³Ãæ"awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}' $LOG_FILE |sort -k2 -nrecho "----------------------"echo "ͳ¼Æ»á¼ûÒ³Ãæ״̬ÂëÊýÄ¿"awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}'
µÇ¼ºó¸´ÖÆ
Éó²éÍø¿¨ÊµÊ±Á÷Á¿¾ç±¾
#!/bin/bashNIC=$1echo -e " In ------ Out"while true; do OLD_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev) OLD_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev) sleep 1 NEW_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev) NEW_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev) IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN)/1024))" "KB/s") OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT)/1024))" "KB/s") echo "$IN $OUT" sleep 1done
µÇ¼ºó¸´ÖÆ
ЧÀÍÆ÷ϵͳÉèÖóõʼ»¯¾ç±¾ ÁíÍ⣬ËÑË÷¹«ÖÚºÅLinux¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°Linux¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£
#/bin/bash# ÉèÖÃʱÇø²¢Í¬²½Ê±¼äln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l |grep ntpdate &>/dev/null ; then (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontabfi# ½ûÓÃselinuxsed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config# ¹Ø±Õ·À»ðǽif egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then systemctl stop firewalld systemctl disable firewalldelif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then service iptables stop chkconfig iptables offfi# ÀúÊ·ÏÂÁîÏÔʾ²Ù×÷ʱ¼äif ! grep HISTTIMEFORMAT /etc/bashrc; then echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrcfi# SSH³¬Ê±Ê±¼äif ! grep "TMOUT=600" /etc/profile &>/dev/null; then echo "export TMOUT=600" >> /etc/profilefi# եȡrootÔ¶³ÌµÇ¼sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config# եȡ׼ʱʹÃüÏò·¢ËÍÓʼþsed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab# ÉèÖÃ×î´ó·¿ªÎļþÊýif ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then cat >> /etc/security/limits.conf << EOF * soft nofile 65535 * hard nofile 65535EOFfi# ϵͳÄÚºËÓÅ»¯cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_tw_buckets = 20480net.ipv4.tcp_max_syn_backlog = 20480net.core.netdev_max_backlog = 262144net.ipv4.tcp_fin_timeout = 20EOF# ïÔÌSWAPʹÓÃecho "0" > /proc/sys/vm/swappiness# ×°ÖÃϵͳÐÔÄÜÆÊÎö¹¤¾ß¼°ÆäËûyum install gcc make autoconf vim sysstat net-tools iostat if
µÇ¼ºó¸´ÖÆ
¼à¿Ø 100 ̨ЧÀÍÆ÷´ÅÅÌʹÓÃÂʾ籾
#!/bin/bashHOST_INFO=host.infofor IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO) PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO) TMP_FILE=/tmp/disk.tmp ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE) for USE_RATE in $USE_RATE_LIST; do PART_NAME=${USE_RATE%=*} USE_RATE=${USE_RATE#*=} if [ $USE_RATE -ge 80 ]; then echo "Warning: $PART_NAME Partition usage $USE_RATE%!" fi donedone
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇ9 ¸öÊÊÓà shell ¾ç±¾£¬½¨ÒéÕä²Ø£¡µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£