1.使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:
[root@oldboy oldboy]# sh /server/scripts/oldboy.sh
[root@oldboy oldboy]# ls
coaolvajcq_oldboy.html qnvuxvicni_oldboy.html vioesjmcbu_oldboy.html
gmkhrancxh_oldboy.html tmdjormaxr_oldboy.html wzewnojiwe_oldboy.html
jdxexendbe_oldboy.html ugaywanjlm_oldboy.html xzzruhdzda_oldboy.html
qcawgsrtkp_oldboy.html vfrphtqjpc_oldboy.html
[root@www test]# cat shuijishu1.sh
#!/sbin/bash
path=/oldboy
[ -d "$path" ]|| mkdir -p /oldboy
for n in `seq 10`
do
randomnu=$(echo $RANDOM|md5sum |tr "[0-9]" "[a-z]"|cut -c 2-11)
touch "$path/$randomnu"_oldboy.html
done
注获取随机字符法2: openssl rand -base64 40|sed s#[^a-z]##g|cut -c 2-11
@H_301_126@2.@H_301_126@将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。
法一:[root@www oldboy]# rename "oldboy.html" "oldgirl.HTML" *.html
法二:
[root@www test]# cat chongminming.sh
#!/sbin/bash
path=/oldboy
cd $path
for n in `ls`
do
name=$(echo ${n}|awk -F"_" '{print $1}')
mv $n ${name}_oldgirl.HTML
done
法三:
ls /oldboy|xargs -n1|awk -F"_" '{print "mv " $0" "$1"_oldgirl.HTML"}'|bash
@H_301_126@3.@H_301_126@批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
@H_301_126@
@H_301_126@法一:
@H_301_126@[root@www test]# cat creaccount.sh
@H_301_126@#!/sbin/bash
@H_301_126@[ $UID -ne 0 ]&&{
@H_301_126@echo "please su - root"
@H_301_126@exit 1
@H_301_126@}
@H_301_126@for n in `seq -w 10`
@H_301_126@do
@H_301_126@user=fengxiaoli$n
@H_301_126@word=`grep -w $user /etc/passwd|wc -l`
@H_301_126@if [ $word -eq 1 ];then
@H_301_126@echo "useradd $user already exists!"
@H_301_126@continue
@H_301_126@fi
@H_301_126@pass=$(echo $RANDOM|md5sum |cut -c 2-11)
@H_301_126@useradd $user && \
@H_301_126@echo "$pass"|passwd --stdin $user &>/dev/null
@H_301_126@resut=$?
@H_301_126@if [ $resut -eq 0 ]
@H_301_126@then
@H_301_126@echo "$user create succss"
@H_301_126@echo "account=$user password=$pass" >>/tmp/acount.txt
@H_301_126@done
@H_301_126@
@H_301_126@#Random number method
@H_301_126@#openssl rand -base64 40|cut -c 2-11
@H_301_126@#echo $RANDOM|md5sum |cut -c 2-11"
@H_301_126@#double number method
@H_301_126@#seq -w 10
@H_301_126@#echo {00..10}
法二:
echo feng{01..10}|xargs -n1|sed -r ' s#(.*)#useradd \1;pass=$(echo $RANDOM|md5sum |cut -c 2-11);echo "$pass"|passwd --stdin \1;echo "\1" \t$pass>>/tmp/user.txt#g'|bash
@H_301_126@
@H_301_126@4.@H_301_126@写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)
@H_301_126@
@H_301_126@[root@www test]# cat ping.sh
@H_301_126@法一:此方法较慢,如果主机禁ping,则不能检测出主机
@H_301_126@ip="192.168.1."
@H_301_126@cmd="ping -W 2 -c 2"
@H_301_126@for i in `seq 254`
@H_301_126@$cmd $ip$i &>/dev/null
@H_301_126@if [ $? -eq 0 ]
@H_301_126@echo "$ip$i is ok!"
@H_301_126@else
@H_301_126@echo "$ip$i is bad!"
@H_301_126@法二:此方法较快,禁ping也能监测出主机,nmap功能很强大,建议了解
@H_301_126@nmap -sP 192.168.1.*|grep "Nmap scan report for"|awk '{print $5 " is ok!"}'
@H_301_126@
@H_301_126@5.@H_301_126@写一个脚本解决DOS攻击生产案例@H_301_126@
@H_301_126@提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。
@H_301_126@[ -f /etc/init.d/functions ] && . /etc/init.d/functions
@H_301_126@account=5
@H_301_126@function ipt(){
@H_301_126@awk '{print $1}' /application/Nginx/logs/access.log |sort |uniq -c|sort -nr -k1 >>/tmp/ip.log@H_301_126@#注意access.log日志需要按天或按小时分割出来,再来分析
@H_301_126@exec </tmp/ip.log
@H_301_126@while read line
@H_301_126@IP=$(echo "$line"|awk '{print $2}')
@H_301_126@if [ `echo "$line"|awk '{print $1}'` -ge $account -a `iptables -L -n|grep "$IP" | wc -l` -lt 1 ];then
@H_301_126@iptables -I INPUT -s $IP -j DROP
@H_301_126@if [ $? -eq 0 ];then
@H_301_126@echo "$IP is DROP ok"
@H_301_126@echo "$IP" >>/tmp/ip_drop_`date +%F`.txt
@H_301_126@else
@H_301_126@echo "$IP is DROP false"
@H_301_126@function del(){
@H_301_126@[ -f /tmp/ip_drop_`date +%F -d '-1day'`.txt ]||{
@H_301_126@echo "the log is not exist"
@H_301_126@exec </tmp/ip_drop_`date +%F -d '-1day'`.txt
@H_301_126@if [ `iptables -L -n|grep "$line"|wc -l` -eq 1 ];then
@H_301_126@iptables -D INPUT -s $line -j DROP
@H_301_126@main(){
@H_301_126@flag=0
@H_301_126@while true
@H_301_126@sleep 180 #等待3分钟
@H_301_126@((flag++))
@H_301_126@ipt
@H_301_126@[ $flag -ge 480 ]&&del&&flag=0 #当flag=480,也就是3*480分钟,等于24小时,意思是将前一天drop掉的ip允许访问
@H_301_126@main
@H_301_126@法二:注意这里的netstat.log是netstat命令里的内容
@H_301_126@grep ESTABLISHED netstat.log |awk -F "[ :]+" '{print $6}'|sort |uniq -c |sort -rn -k1
@H_301_126@法二只需将上面法一中IP获取方法替换即可
@H_301_126@
@H_301_126@6.打印下面这句话中字母数不大于6的单词@H_301_126@I am oldboy teacher welcome to oldboy training class
@H_301_126@echo "$word"|xargs -n1|awk 'length <6{print $1}'
@H_301_126@法二:
@H_301_126@[root@www test]# tail 001.sh
@H_301_126@for word in ${array[*]}
@H_301_126@if [ `expr length $word` -lt 6 ];then
@H_301_126@#if [ ${#word} -lt 6 ];then
@H_301_126@#if [ `echo $word|wc -L` -lt 6 ];then
@H_301_126@echo $word
@H_301_126@${#变量}
@H_301_126@expr length 变量
@H_301_126@echo 变量 |wc -L
@H_301_126@7.@H_301_126@开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。
@H_301_126@read -p "please input two int num:" a b
@H_301_126@if [ -z $a ]||[ -z $b ];then
@H_301_126@echo "please input two num!"
@H_301_126@expr $a + 10 >/dev/null 2>&1
@H_301_126@if [ $? -ne 0 ];then
@H_301_126@echo "please input int num!"
@H_301_126@expr $b + 10 >/dev/null 2>&1
@H_301_126@if [ $a -gt $b ];then
@H_301_126@echo "$a >$b"
@H_301_126@exit 0
@H_301_126@elif [ $a -lt $b ];then
@H_301_126@echo "$a <$b"
@H_301_126@echo "$a=$b"
@H_301_126@脚本传参方式只需将$a $b 替换会$1 $2即可
@H_301_126@
8.批量检查多个网站地址是否正常
http://www.etiantian.org
http://www.taobao.com
http://oldboy.blog.51cto.com
http://10.0.0.7
[root@www test]# cat 003.sh
#!/sbin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
array=(
http://www.etiantian.org
http://www.taobao.com
http://10.117.33.193
)
for n in ${array[*]}
do
curl=$(wget --spider --timeout=3 --tries=2 $n &>/dev/null)
if [ `echo $?` -eq 0 ];then
action "curl $n" /bin/true
else
action "curl $n" /bin/false
fi
Done
9.企业案例:写网络服务独立进程模式下rsync的系统启动脚本
例如:/etc/init.d/rsyncd{start|stop|restart}。
要求:1.要使用系统函数库技巧。2.要用函数,不能一坨SHI的方式。3.可被chkconfig管理。
[root@server ~]# cat 001.sh
#!/bin/bash
# chkconfig: 2345 3062 #将脚本添加进chkconfig时2345向需设置10-90之间,必须添加这句才能将该脚本添加进chkconfig
pidfile=/var/run/rsyncd.pid
judge(){
result=$?
if [ $result = 0 ];then
action "rsync is $1" /bin/true
action "rsync is $1" /bin/false
}
start() {
if [ -f $pidfile ];then
echo "rsync is running"
rsync --daemon
judge started
stop(){
if [ ! -f $pidfile ];then
echo "rsync is stopping"
kill `cat $pidfile`
rm -f $pidfile
judge stopd
case "$1" in
start)
start
;;
stop)
stop
restart)
sleep 2
*)
echo "usage:$0 {start|stop|restart}"
exit 1
esac
注:添加进chkconfig 设置开机自启动
cp 001.sh /etc/init.d/rsyncd
# chkconfig: 2345 10 90#将脚本添加进chkconfig时2345向需设置10-90之间
@H_301_126@[root@server init.d]# ll /etc/rc.d/rc3.d/|grep 30@H_301_126@#30没被使用
@H_301_126@[root@server init.d]# ll /etc/rc.d/rc3.d/|grep 61@H_301_126@#62没被使用
@H_301_126@[root@server init.d]# chkconfig --add rsyncd
@H_301_126@[root@server init.d]# chkconfig --list|grep rsync
@H_301_126@rsyncd @H_301_126@0:off@H_301_126@1:off@H_301_126@2:on@H_301_126@3:on@H_301_126@4:on@H_301_126@5:on@H_301_126@6:off
@H_301_126@
10.好消息,老男孩培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。
因此需要挑选学生,因此需要一个抓阄的程序:
要求:
1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。
2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入。
@H_301_126@[root@server ~]# cat 002.sh
@H_301_126@#!/bin/bash
@H_301_126@file=/tmp/file.txt
@H_301_126@[ -f $file ]|| touch $file
@H_301_126@read -p "please input your English name:" name
@H_301_126@rename=$(grep "\b$name\b" $file|wc -l)
@H_301_126@if [ -z $name ];then
@H_301_126@echo "Please do not enter empty characters"
@H_301_126@elif [ $rename -eq 1 ];then
@H_301_126@echo "The user already exists"
@H_301_126@ran_num=$(expr $RANDOM % 99 + 1)
@H_301_126@zhua_num=$(grep "\b${ran_num}\b" $file|wc -l)
@H_301_126@if [ $zhua_num -ne 1 ];then
@H_301_126@echo "$name $ran_num"|tee -a $file
@H_301_126@flag=1
@H_301_126@[ $flag -eq 1 ] && break
已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的对应数字?
21029299
00205d1c
a3da1677
1f6d12dd
@H_301_126@[root@www ~]# cat 003.sh
@H_301_126@array=(
@H_301_126@21029299
@H_301_126@00205d1c
@H_301_126@a3da1677
@H_301_126@1f6d12dd
@H_301_126@)
@H_301_126@for i in {0..32767}
@H_301_126@md5=$(echo $i|md5sum |cut -c 1-8)
@H_301_126@for n in ${array[*]}
@H_301_126@if [ $md5 == $n ];then
@H_301_126@echo "$i is $n"
@H_301_126@
11:用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
[root@www ~]# cat file.txt
the squid project provides a number of resources toassist users
design,implement and support squid installations. Please browsethe
documentation and support sections for more infomation
1、按单词出现频率降序排序!
[root@www ~]# cat file.txt |tr "[.,]" " "|sed "s# #\n#g"|grep -v "^$"|sort|uniq -c|sort -rn
[root@www ~]# cat file.txt |sed "s#[.,]#\n#g"|grep -v "^$"|sort |uniq -c|sort -rn
2、按字母出现频率降序排序!
@H_301_126@[root@www ~]# cat file.txt |tr "[,.]" " "|sed "s# ##g"|sed -r "s#(.)#\1\n#g"|sort|uniq -c|sort -rn
@H_301_126@12.面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。@H_301_126@
@H_301_126@解决方案:如果是正常的代码上线,则暂时不监控,正常代码上线之后先执行001.sh建立指纹库和记录文件数目,再继续监控执行002.sh
@H_301_126@[root@web01 shell]# cat 001.sh
@H_301_126@path=/var/html/www/
@H_301_126@[ -d /test ]|| mkdir /test -p
@H_301_126@md5_log=/test/md5_old.log
@H_301_126@num_log=/test/num_old.log
@H_301_126@find "$path" -type f -exec md5sum {} >$md5_log \;
@H_301_126@find "$path" -type f > $num_log
@H_301_126@[root@web01 shell]# cat 002.sh
@H_301_126@path=/var/html/www@H_301_126@#检测站点路径
@H_301_126@num=$(cat $num_log|wc -l)
@H_301_126@resultlog=/test/result.log
@H_301_126@[ ! -f $resultlog ] && touch $resultlog
@H_301_126@md5_check=$(md5sum -c $md5_log 2>/dev/null |grep Failed|wc -l)
@H_301_126@new_num=$(find $path -type f|wc -l)
@H_301_126@find $path -type f >/test/num_new.log
@H_301_126@if [ $md5_check -ne 0 ]||[ $new_num -ne $num ];then
@H_301_126@echo "$(md5sum -c $md5_log 2>/dev/null | grep Failed)" >$resultlog
@H_301_126@diff $num_log /test/num_new.log >>$resultlog
@H_301_126@# mail -s "web site is changed in $(date +%F\ %T)" 15683988767@163.com <$resultlog
@H_301_126@sleep 3
@H_301_126@[root@web01 test]# ll /test/
@H_301_126@total 16
@H_301_126@-rw-r--r--. 1 root root 597 Jul 15 23:20 md5_old.log
@H_301_126@-rw-r--r--. 1 root root 223 Jul 15 23:24 num_new.log
@H_301_126@-rw-r--r--. 1 root root 223 Jul 15 23:20 num_old.log
@H_301_126@-rw-r--r--. 1 root root 29 Jul 15 23:24 result.log
@H_301_126@13.请用shell或Python编写一个等腰三角形(oldboy2_triangle.sh),接收用户输入的数字。
@H_301_126@例如:
@H_301_126@[root@web01 shell]# sh 004.sh
@H_301_126@pleash enter a number:6
@H_301_126@*
@H_301_126@***
@H_301_126@*****
@H_301_126@*******
@H_301_126@*********
@H_301_126@***********
@H_301_126@[root@web01 shell]# cat 004.sh
@H_301_126@read -p "pleash enter a number:" n
@H_301_126@for ((i=1;i<=$n;i++))
@H_301_126@for((j=(($n-$i));j>0;j--))
@H_301_126@echo -n " "
@H_301_126@for((m=0;m<$((2*$i-1));m++))
@H_301_126@@H_301_126@echo -n "*"
@H_301_126@echo
@H_301_126@[root@web01 shell]# sh 005.sh 2 6
@H_301_126@* *
@H_301_126@* * *
@H_301_126@* * * *
@H_301_126@* * * * *
@H_301_126@* * * * * *
@H_301_126@[root@web01 shell]# cat 005.sh
@H_301_126@for ((n=$1;n<=$2;n++))
@H_301_126@for ((m=1;m<$n;m++))
@H_301_126@echo -n "* "
@H_301_126@if [ $m -eq $n ];then
@H_301_126@echo "* "
@H_301_126@
14.打印选择菜单,一键安装Web服务:
[root@oldboyscripts]# sh menu.sh
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
、当用户输入时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本;
2startinstalling lnmp./server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本;
4、当输入任何其它字符,给出提示“Input error”后退出脚本。
5、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。
@H_301_126@[root@web01 shell]# cat 006.sh
@H_301_126@lnmp=/server/scripts/lnmp.sh
@H_301_126@lamp=/server/scripts/lamp.sh
@H_301_126@echo "1.[install lamp]"
@H_301_126@echo "2.[install lnmp]"
@H_301_126@echo "3.[exit]"
@H_301_126@read -p "please input num:" num
@H_301_126@case $num in
@H_301_126@1)
@H_301_126@[ -f $lamp -a -x $lamp ]||{
@H_301_126@echo "$lamp is error!"
@H_301_126@echo "startinstalling lamp..."
@H_301_126@$lamp
@H_301_126@echo "lamp installed..."
@H_301_126@;;
@H_301_126@2)
@H_301_126@[ -f $lnmp -a -x $lnmp ]||{
@H_301_126@echo "$lnmp is error!"
@H_301_126@echo "startinstalling lnmp..."
@H_301_126@$lnmp
@H_301_126@echo "lnmp installed..."
@H_301_126@3)
@H_301_126@*)
@H_301_126@echo "input error"
@H_301_126@15.对MysqL数据库进行分库加分表备份,请用脚本实现
@H_301_126@[root@MysqL-01 ~]# cat 001.sh
@H_301_126@USER=root
@H_301_126@PASS=oldboy
@H_301_126@SOCK=/data/3306/MysqL.sock
@H_301_126@LOGIN="MysqL -u$USER -p$PASS -S $SOCK"
@H_301_126@DUMP="MysqLdump -u$USER -poldboy -S $SOCK"
@H_301_126@DATABASE=$($LOGIN -e "show databases;"|sed 1d|grep -Ev "*_schema|MysqL")
@H_301_126@for database in $DATABASE
@H_301_126@TABLES=$($LOGIN -e "use $database;show tables;"|sed 1d)
@H_301_126@for tables in $TABLES
@H_301_126@[ -d /opt/$database ]||mkdir -p /opt/$database
@H_301_126@$DUMP $database $TABLES |gzip > /opt/$database/${database}_${tables}_$(date +%F).sql.gz
@H_301_126@[root@MysqL-01 ~]# cat 002.sh
@H_301_126@$DUMP $database -B |gzip > /opt/${database}_${tables}_$(date +%F).sql.gz
@H_301_126@done
17.开发MysqL多实例启动脚本:@H_301_126@已知MysqL多实例启动命令为:MysqLd_safe--defaults-file=/data/3306/my.cnf &@H_301_126@停止命令为:MysqLadmin -u root -poldboy123 -S /data/3306/MysqL.sockshutdown@H_301_126@请完成MysqL多实例启动启动脚本的编写@H_301_126@要求:用函数,case语句、if语句等实现。
@H_301_126@
@H_301_126@[root@MysqL-01 3306]# vim MysqL
@H_301_126@#!/bin/sh
@H_301_126@#init
@H_301_126@port=3306
@H_301_126@CmdPath="/application/MysqL/bin"
@H_301_126@MysqL_sock="/data/${port}/MysqL.sock"
@H_301_126@#startup function
@H_301_126@function_start_MysqL()
@H_301_126@{
@H_301_126@if [ ! -e "$MysqL_sock" ];then
@H_301_126@printf "Starting MysqL...\n"
@H_301_126@/bin/sh ${CmdPath}/MysqLd_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
@H_301_126@printf "MysqL is running...\n"
@H_301_126@exit
@H_301_126@#stop function
@H_301_126@function_stop_MysqL()
@H_301_126@printf "MysqL is stopped...\n"
@H_301_126@printf "Stoping MysqL...\n"
@H_301_126@${CmdPath}/MysqLadmin -u ${MysqL_user} -p${MysqL_pwd} -S /data/${port}/MysqL.sock shutdown
@H_301_126@#restart function
@H_301_126@function_restart_MysqL()
@H_301_126@printf "Restarting MysqL...\n"
@H_301_126@function_stop_MysqL
@H_301_126@sleep 2
@H_301_126@function_start_MysqL
@H_301_126@case $1 in
@H_301_126@start)
@H_301_126@stop)
@H_301_126@restart)
@H_301_126@function_restart_MysqL
@H_301_126@printf "Usage: /data/${port}/MysqL {start|stop|restart}\n"
@H_301_126@esac
@H_301_126@
18.企业面试题1:(生产实战案例):监控MysqL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
@H_301_126@[root@oldboy C13]# cat 13_6_3.sh
@H_301_126@###########################################
@H_301_126@# this script function is :
@H_301_126@# check_MysqL_slave_replication_status
@H_301_126@############################################
@H_301_126@path=/server/scripts
@H_301_126@MAIL_GROUP="1111@qq.com 2222@qq.com"
@H_301_126@PAGER_GROUP="18600338340 18911718229"
@H_301_126@LOG_FILE="/tmp/web_check.log"
@H_301_126@PASSWORD=oldboy123
@H_301_126@PORT=3307
@H_301_126@MysqLCMD="MysqL -u$USER -p$PASSWORD -S /data/$PORT/MysqL.sock"
@H_301_126@error=(1008 1007 1062)
@H_301_126@RETVAL=0
@H_301_126@[ ! -d $path ] && mkdir -p $path
@H_301_126@function JudgeError(){
@H_301_126@for((i=0;i<${#error[*]};i++))
@H_301_126@if [ "$1" == "${error[$i]}" ]
@H_301_126@echo "MysqL slave errorno is $1,auto repairing it."
@H_301_126@$MysqLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
@H_301_126@return $1
@H_301_126@function CheckDb(){
@H_301_126@status=($(awk -F ': ' '/_Running|Last_Errno|_Behind/{print $NF}' slave.log))
@H_301_126@expr ${status[3]} + 1 &>/dev/null
@H_301_126@status[3]=300
@H_301_126@if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a ${status[3]} -lt 120 ]
@H_301_126@#echo "MysqL slave status is ok"
@H_301_126@return 0
@H_301_126@#echo "MysqL replcation is Failed"
@H_301_126@JudgeError ${status[2]}
@H_301_126@function MAIL(){
@H_301_126@local SUBJECT_CONTENT=$1
@H_301_126@for MAIL_USER in `echo $MAIL_GROUP`
@H_301_126@mail -s "$SUBJECT_CONTENT " $MAIL_USER <$LOG_FILE
@H_301_126@function PAGER(){
@H_301_126@for PAGER_USER in `echo $PAGER_GROUP`
@H_301_126@TITLE=$1
@H_301_126@CONTACT=$PAGER_USER
@H_301_126@HTTPGW=http://oldboy.sms.cn/smsproxy/sendsms.action
@H_301_126@#send_message method1
@H_301_126@curl -d cdkey=5ADF-EFA -d password=OLDBOY -d phone=$CONTACT -d message="$TITLE[$2]" $HTTPGW
@H_301_126@function SendMsg(){
@H_301_126@if [ $1 -ne 0 ]
@H_301_126@then
@H_301_126@RETVAL=1
@H_301_126@NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"`
@H_301_126@SUBJECT_CONTENT="MysqL slave is error,errorno is $2,${NOW_TIME}."
@H_301_126@echo -e "$SUBJECT_CONTENT"|tee $LOG_FILE
@H_301_126@MAIL $SUBJECT_CONTENT
@H_301_126@PAGER $SUBJECT_CONTENT $NOW_TIME
@H_301_126@echo "MysqL slave status is ok"
@H_301_126@return $RETVAL
@H_301_126@function main(){
@H_301_126@CheckDb
@H_301_126@SendMsg $?
@H_301_126@sleep 300
@H_301_126@
@H_301_126@19.将域名取出,并根据域名进行计数排序处理。
@H_301_126@[root@fengxiaoli ~]# cat yuming.txt
@H_301_126@http://a.example.com/1.html
@H_301_126@http://b.example.com/1.html
@H_301_126@http://c.example.com/1.html
@H_301_126@http://a.example.com/2.html
@H_301_126@http://b.example.com/2.html
@H_301_126@http://a.example.com/3.html
@H_301_126@[root@fengxiaoli ~]# cat yuming.txt |awk -F "/" '{print $3}'|sort|uniq -c
@H_301_126@3 a.example.com
@H_301_126@2 b.example.com
@H_301_126@1 c.example.com
[root@fengxiaoli ~]# cat yuming.txt|awk -F / '{S[$3]++}END{for(k in S)print k,S[k]}'|sort
@H_301_126@a.example.com 3
@H_301_126@b.example.com 2
@H_301_126@c.example.com 1
@H_301_126@[root@fengxiaoli ~]# cat yuming.txt|awk -F / '{++S[$3]}END{for(k in S)print k,255);">c.example.com 1
@H_301_126@[root@fengxiaoli ~]# netstat -n |awk '/^tcp/ {print $NF}' |sort |uniq -c|sort
@H_301_126@[root@fengxiaoli ~]# netstat -n |awk '/^tcp/ {++S[$NF]} END {for(k in S) print k,S[k]}'
@H_301_126@TIME_WAIT 9137
@H_301_126@CLOSE_WAIT 207
@H_301_126@FIN_WAIT1 547
@H_301_126@ESTABLISHED 597
@H_301_126@FIN_WAIT2 74
@H_301_126@SYN_RECV 70
@H_301_126@CLOSING 55
@H_301_126@LAST_ACK 8
@H_301_126@21.分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小
@H_301_126@说明:这个功能可以用于IDC及CDN网站流量带宽很高,然后通过分析服务器日志哪些元素占用流量过大,进而进行优化裁剪该图片,压缩js等措施。
@H_301_126@本题需要输出三个指标: 【访问次数】 【访问次数*单个文件大小】 【文件名(可以带URL)】
@H_301_126@[root@fengxiaoli ~]# cat fs.txt
@H_301_126@59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
@H_301_126@59.33.26.105 - - [08/Dec/2010:15:44:02 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 200 3583 "http://oldboy.blog.51cto.com/static/flex/AdobeVideoPlayer.swf?width=590&height=328&url=/[[DYNAMIC]]/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
@H_301_126@124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-"
@H_301_126@124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-"
@H_301_126@124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"
@H_301_126@[root@fengxiaoli ~]# awk '{print $7"\t" $10}' fs.txt |sort |uniq -c|awk '{print $1*$3,$1,$2}'|sort -rn|head
@H_301_126@46232 1 /?=
@H_301_126@22598 2 /static/images/photos/2.jpg
@H_301_126@4460 1 /static/js/web_js.js
@H_301_126@3583 1 /static/flex/vedioLoading.swf
@H_301_126@1627 1 /static/js/jquery.lazyload.js
@H_301_126@通过两个数组来计算
@H_301_126@因为我们要的最终结果是某个文件的访问次数和消耗的流量,所以考虑建立以文件名为索引的两个数组,一个存储访问次数,一个保存消耗的流量,这样当使用awk按行遍历文件时,对次数数组+1,同时对流量数组进行文件大小的累加,等文件扫描完成,再遍历输出两个数组既可以得到该文件的反问次数和总的流量消耗。
@H_301_126@[root@fengxiaoli ~]# awk '{array_num[$7]++;array_size[$7]+=$10}END{for(x in array_num) print array_size[x],array_num[x],x}' fs.txt |sort -rn -k1 |head -10
@H_301_126@1627 1 /static/js/jquery.lazyload.js