用shell脚本分析Nginx访问日志IP地址来源


该日志分析脚本以Ubuntu server 10.04为基础,由于Nginx没有自带切割日志功能,用以下脚本把Nginx生成的日志每天零点做一次切割,目录按年/月分,例如10月份31天的都放在2010/10/下面.
切割脚本:

#!/bin/bash
PATH_LOGS="/usr/local/nginx/logs"
YEAR=`date -d "-1 days" +"%Y"`
MONTH=`date -d "-1 days" +"%m"`
mkdir -p $PATH_LOGS/$YEAR/$MONTH
mv $PATH_LOGS/access.log $PATH_LOGS/$YEAR/$MONTH/access_$(date -d "-1 days" +"%Y%m%d").log
kill -USR1 `cat $PATH_LOGS/nginx.pid`

分析IP地址来源的脚本,以ip168网站为基础的:

#![......]

继续阅读

为Linux下manpage添加彩色高亮


1.vim /etc/profile 添加

export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;38;5;74m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[38;5;246m'
export LESS_TERMCAP_ue=$'\E[0m'

export LESS_TERMCAP_us=$’\E[04;38;5;146m’

2.source /etc/profile 或重新退出重新登录

3.效果:

gawkcolor



expect 自动登录服务器执行操作


以下expect脚本是用root用户登录某server并执行touch test.txt

#!/usr/bin/expect -f
#Filename:login.exp
#write by zhangll
set ip [lindex $argv 0 ]
set password [lindex $argv 1 ]
set timeout 50
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
expect "]*"
send "touch test.txt\r"
send "exit\r"
expect eof

执行:

chmod +x login.exp
[......]

继续阅读

Shell Script-查看文件上次修改访问时间


#!/bin/bash
# Time of last access
# Time of last modification
# Time of last change
 
FILE="$1"
 
# 判断是否带有参数
if [ $# -eq 0 ]
then
echo "$0 file-name"
exit 1
fi
 
which stat > /dev/null
 
# 查看是否安装有stat
if [ $? -eq 1 ]
then
echo "stat command not found!"
exit 2
fi
 
# 判断文件是否存在
if [ ! -e $FILE ]
then
echo "$FILE not a file"
exit 3
fi
 
# 用stat得到文件的相关信息
echo "Time of last access : $([......]

继续阅读