启动Shell编写

netkiller- 发布于 2014/05/24 10:37
阅读 311
收藏 3

启动Shell编写

MrNeo Chen (netkiller)陈景峰(BG7NYT)


中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890
+86 755 29812080
<netkiller@msn.com>

$Id: shell.xml 449 2012-08-10 10:38:08Z netkiller $

版权 © 2011, 2012, 2013, 2014 http://netkiller.github.io

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

文档出处:
http://netkiller.github.io
http://netkiller.sourceforge.net

$Data$

摘要

.


1. 启用 shell

1 解决重复运行问题

1 记录PID以便可以停止Shell运维

#!/bin/bash
##############################################
# $Author: netkiller $
# $Id: shell.xml 449 2012-08-10 10:38:08Z netkiller $
##############################################
NAME=info
BASEDIR='/www'
PROG=$BASEDIR/bin/$(basename $0)
LOGFILE=/var/tmp/$NAME.log
PIDFILE=/var/tmp/$NAME.pid
##############################################
PHP=/usr/local/webserver/php/bin/php
##############################################
#echo $$
#echo $BASHPID
function start(){
	if [ -f "$PIDFILE" ]; then
		echo $PIDFILE
		exit 2
	fi

	for (( ; ; ))
	do
		cd $BASEDIR/crontab/
		$PHP readfile.php > $LOGFILE
		$PHP chart_gold_silver_xml.php > /dev/null
		sleep 60
	done &
	echo $! > $PIDFILE
}
function stop(){
  	[ -f $PIDFILE ] && kill `cat $PIDFILE` && rm -rf $PIDFILE
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
  	ps ax | grep chart.xml | grep -v grep | grep -v status
	;;
  restart)
  	stop
	start
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart}"
	exit 2
esac

exit $?

2. htpasswd 密码批量生成

#!/bin/bash

PASSFILE=nginx.password
[ ! -f $PASSFILE ] && touch $PASSFILE

while read username password
do
        htpasswd -b -d $PASSFILE $username $password
done << EOF
neo     FwJSYxD4WBzPr4CQvxI8HIbV0yDkQi
chen    2hsD3OgkeM4GPPcNYUceqL8ccMzXjU
bg7nyt  XAq7Zcln8dGCTIIKt8GwwEwqmCN8d1
netkiller       fcCIY3GaroTPCSW40XBrg0HNlmbLD7
neochen DPSiWJtqUIaI2bUUobuX2PjdyzDGgI
EOF

3. firewall

分析access.log 文件,将 top 30 的IP放入黑名单.

脚本具有黑白名单功能

#!/bin/bash

ACCCESS_LOG=/tmp/access.log
TIMEPOINT='24/May/2012'
BLACKLIST=/var/tmp/black
WHITELIST=/var/tmp/white
if [ ! -f ${BLACKLIST} ]; then
    touch ${BLACKLIST}
fi

if [ ! -f ${WHITELIST} ]; then
    touch ${WHITELIST}
fi

for deny in $(grep ${TIMEPOINT} ${ACCCESS_LOG} | awk '{print $1}' | awk -F'.' '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -r -n | head -n 30| awk '{print $2}')
do

    if [ $(grep -c $deny ${WHITELIST}) -ne 0 ]; then
        echo 'Allow IP:' $deny
	iptables -D INPUT -p tcp --dport 443 -s $deny -j DROP
	iptables -D INPUT -p tcp --dport 80 -s $deny -j DROP
	continue
    fi

    if [ $(grep -c $deny ${BLACKLIST}) -eq 0 ] ; then

	echo 'Deny IP:' $deny
        echo $deny >> ${BLACKLIST}
        iptables -I INPUT -p tcp --dport 443 -s $deny -j DROP
        iptables -I INPUT -p tcp --dport 80 -s $deny -j DROP
    fi
done
加载中
返回顶部
顶部