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


2.将以上文件名中的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

3.批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。

法一:

[root@www test]# cat creaccount.sh

#!/sbin/bash

[ $UID -ne 0 ]&&{

echo  "please su - root"

exit 1

}

for n in `seq -w 10`

do

user=fengxiaoli$n

word=`grep -w $user /etc/passwd|wc -l`

if [ $word -eq 1 ];then

echo "useradd $user already exists!"

continue

fi

pass=$(echo $RANDOM|md5sum |cut -c 2-11)

useradd $user && \

echo "$pass"|passwd --stdin $user &>/dev/null

resut=$?

if [ $resut -eq 0 ]

then

echo "$user create succss"

fi

echo "account=$user password=$pass" >>/tmp/acount.txt

done

#Random number method

#openssl rand -base64 40|cut -c 2-11

#echo $RANDOM|md5sum |cut -c 2-11"

#double number method

#seq -w 10

#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

4.写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)

[root@www test]# cat ping.sh

法一:此方法较慢,如果主机禁ping,则不能检测出主机

#!/sbin/bash

ip="192.168.1."

cmd="ping -W 2 -c 2"

for i in `seq 254`

do

$cmd $ip$i &>/dev/null

if [ $? -eq 0 ]

then

echo "$ip$i is ok!"

else

echo "$ip$i is bad!"

fi

done

法二:此方法较快,禁ping也能监测出主机,nmap功能很强大,建议了解

nmap -sP 192.168.1.*|grep "Nmap scan report for"|awk '{print $5 " is ok!"}'

 

5.写一个脚本解决DOS攻击生产案例
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。

法一:

#!/sbin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

account=5

function ipt(){

awk '{print $1}' /application/nginx/logs/access.log |sort |uniq -c|sort -nr -k1 >>/tmp/ip.log#注意access.log日志需要按天或按小时分割出来,再来分析

exec </tmp/ip.log

while read line

do

IP=$(echo "$line"|awk '{print $2}')

if [ `echo "$line"|awk '{print $1}'` -ge $account -a `iptables -L -n|grep "$IP" | wc -l` -lt 1 ];then

iptables -I INPUT -s $IP -j DROP

if [ $? -eq 0 ];then

echo "$IP is DROP ok"

echo "$IP" >>/tmp/ip_drop_`date +%F`.txt

else

echo "$IP is DROP false"

fi

fi

done

}

function del(){

[ -f /tmp/ip_drop_`date +%F -d '-1day'`.txt ]||{

echo "the log is not exist"

exit 1

}

exec </tmp/ip_drop_`date +%F -d '-1day'`.txt

while read line

do

if [ `iptables -L -n|grep "$line"|wc -l` -eq 1 ];then

iptables -D INPUT -s $line -j DROP

fi

done

}

#main 函数也可以用计划任务替代

main(){

flag=0

while true

do

sleep 180 #等待3分钟

((flag++))

ipt

[ $flag -ge 480 ]&&del&&flag=0 #当flag=480,也就是3*480分钟,等于24小时,意思是将前一天drop掉的ip允许访问

done

}

main

法二:注意这里的netstat.log是netstat命令里的内容

grep ESTABLISHED netstat.log |awk -F "[ :]+" '{print $6}'|sort |uniq -c |sort -rn -k1

法二只需将上面法一中IP获取方法替换即可

6.打印下面这句话中字母数不大于6的单词I am oldboy  teacher welcome to oldboy training class

法一:

echo "$word"|xargs -n1|awk 'length <6{print $1}'

法二:

[root@www test]# tail 001.sh

for word in ${array[*]}

do

if [ `expr length $word` -lt 6 ];then

#if [ ${#word} -lt 6 ];then

#if [ `echo $word|wc -L` -lt 6 ];then

echo $word

fi

done

注:取单词长度方法

${#变量}

expr length 变量

echo 变量 |wc -L

7.开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。

#!/sbin/bash

read -p "please input two int num:" a b

if [ -z $a ]||[ -z $b ];then

echo "please input two num!"

exit 1

fi

expr $a + 10 >/dev/null 2>&1

if [ $? -ne 0 ];then

echo "please input int num!"

exit 1

fi

expr $b + 10 >/dev/null 2>&1

if [ $? -ne 0 ];then

echo "please input int num!"

exit 1

fi

if [ $a -gt $b ];then

echo "$a >$b"

exit 0

elif [ $a -lt $b ];then

echo "$a <$b"

exit 0

else

echo "$a=$b"

exit 0

fi

脚本传参方式只需将$a $b 替换会$1 $2即可

8.批量检查多个网站地址是否正常 

要求:shell数组方法实现,检测策略尽量模拟用户访问思路

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://oldboy.blog.51cto.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 30 62 #将脚本添加进chkconfig时2345向需设置10-90之间,必须添加这句才能将该脚本添加进chkconfig

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

pidfile=/var/run/rsyncd.pid

judge(){

result=$?

if [ $result = 0 ];then

action "rsync is $1" /bin/true

result=$?

else

action "rsync is $1" /bin/false

result=$?

fi

}

start() {

if [ -f $pidfile ];then

echo "rsync is running"

result=$?

else

rsync --daemon

judge started

result=$?

fi

}

stop(){

if [ ! -f $pidfile ];then

echo "rsync is stopping"

result=$?

else

kill `cat $pidfile`

rm -f $pidfile

judge stopd

result=$?

fi

}

case "$1" in

start)

start

result=$?

;;

stop)

stop

result=$?

;;

restart)

stop

sleep 2

start

result=$?

;;

*)

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之间

[root@server init.d]# ll /etc/rc.d/rc3.d/|grep 30#30没被使用

[root@server init.d]# ll /etc/rc.d/rc3.d/|grep 61#62没被使用

[root@server init.d]# chkconfig --add rsyncd

[root@server init.d]# chkconfig --list|grep rsync

rsyncd         0:off1:off2:on3:on4:on5:on6:off

10.好消息,老男孩培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。

因此需要挑选学生,因此需要一个抓阄的程序:

要求:

1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。

2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入

[root@server ~]# cat 002.sh

#!/bin/bash

while true

do

file=/tmp/file.txt

[ -f $file ]|| touch $file

read -p "please input your English name:" name

rename=$(grep "\b$name\b" $file|wc -l)

if [ -z $name ];then

echo "Please do not enter empty characters"

continue

elif [ $rename -eq 1 ];then

echo "The user already exists"

continue

else

while true

do

flag=0

ran_num=$(expr $RANDOM % 99 + 1)

zhua_num=$(grep "\b${ran_num}\b" $file|wc -l)

if [ $zhua_num -ne 1 ];then

echo "$name $ran_num"|tee -a $file

flag=1

fi

[ $flag -eq 1 ] && break

done

fi

done

10.已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?

21029299

00205d1c

a3da1677

1f6d12dd

[root@www ~]# cat 003.sh

#!/bin/bash

array=(

21029299

00205d1c

a3da1677

1f6d12dd

)

for i in {0..32767}

do

md5=$(echo $i|md5sum |cut -c 1-8)

for n in ${array[*]}

do

if [ $md5 == $n ];then

echo "$i is $n"

fi

done

done

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、按字母出现频率降序排序!

[root@www ~]# cat file.txt |tr "[,.]" " "|sed "s# ##g"|sed -r "s#(.)#\1\n#g"|sort|uniq -c|sort -rn

12.面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。

解决方案:如果是正常的代码上线,则暂时不监控,正常代码上线之后先执行001.sh建立指纹库和记录文件数目,再继续监控执行002.sh

[root@web01 shell]# cat 001.sh

#!/bin/bash

path=/var/html/www/

[ -d /test ]|| mkdir /test -p

md5_log=/test/md5_old.log

num_log=/test/num_old.log

find "$path" -type f -exec md5sum {} >$md5_log \;

find "$path" -type f > $num_log

[root@web01 shell]# cat 002.sh

#!/bin/bash

path=/var/html/www#检测站点路径

[ -d /test ]|| mkdir /test -p

md5_log=/test/md5_old.log

num_log=/test/num_old.log

num=$(cat $num_log|wc -l)

while true

do

resultlog=/test/result.log

[ ! -f $resultlog ] && touch $resultlog

md5_check=$(md5sum -c $md5_log 2>/dev/null |grep FAILED|wc -l)

new_num=$(find $path -type f|wc -l)

find $path -type f >/test/num_new.log

if [ $md5_check -ne 0 ]||[ $new_num -ne $num ];then

echo "$(md5sum -c $md5_log 2>/dev/null | grep FAILED)" >$resultlog

diff $num_log /test/num_new.log >>$resultlog

#  mail -s "web site is changed in $(date +%F\ %T)" 15683988767@163.com <$resultlog

fi

sleep 3

done

[root@web01 test]# ll /test/

total 16

-rw-r--r--. 1 root root 597 Jul 15 23:20 md5_old.log

-rw-r--r--. 1 root root 223 Jul 15 23:24 num_new.log

-rw-r--r--. 1 root root 223 Jul 15 23:20 num_old.log

-rw-r--r--. 1 root root  29 Jul 15 23:24 result.log

13.请用shell或Python编写一个等腰三角形(oldboy2_triangle.sh),接收用户输入的数字。

例如:

[root@web01 shell]# sh 004.sh

pleash enter a number:6

*

***

*****

*******

*********

***********

[root@web01 shell]# cat 004.sh

#!/bin/bash

read -p "pleash enter a number:" n

for ((i=1;i<=$n;i++))

do

for((j=(($n-$i));j>0;j--))

do

echo -n " "

done

for((m=0;m<$((2*$i-1));m++))

do

echo -n  "*"

done

echo

done

[root@web01 shell]# sh 005.sh 2 6

* *

* * *

* * * *

* * * * *

* * * * * *

[root@web01 shell]# cat 005.sh

#!/bin/bash

for ((n=$1;n<=$2;n++))

do

for ((m=1;m<$n;m++))

do

echo -n "* "

done

if [ $m -eq $n ];then

echo "* "

fi

done

 

14.打印选择菜单,一键安装Web服务:

[root@oldboyscripts]# sh menu.sh

    1.[install lamp]

    2.[install lnmp]

    3.[exit]

    pls input the num you want:

要求:

1、当用户输入1时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本;

2、当用户输入2时,输出“startinstalling lnmp.”然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本;

3、当输入3时,退出当前菜单及脚本;

4、当输入任何其它字符,给出提示“Input error”后退出脚本。

5、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。

[root@web01 shell]# cat 006.sh

#!/bin/bash

lnmp=/server/scripts/lnmp.sh

lamp=/server/scripts/lamp.sh

echo "1.[install lamp]"

echo "2.[install lnmp]"

echo "3.[exit]"

read -p "please input num:" num

case $num in

1)

[ -f $lamp -a -x $lamp ]||{

echo "$lamp is error!"

exit 1

}

echo "startinstalling lamp..."

$lamp

echo "lamp installed..."

;;

2)

[ -f $lnmp -a -x $lnmp ]||{

echo "$lnmp is error!"

exit 1

}

echo "startinstalling lnmp..."

$lnmp

echo "lnmp installed..."

;;

3)

exit 0

;;

*)

echo "input error"

exit 1

Esac

15.对MySQL数据库进行分库加分表备份,请用脚本实现

[root@mysql-01 ~]# cat 001.sh

#!/bin/bash

USER=root

PASS=oldboy

SOCK=/data/3306/mysql.sock

LOGIN="mysql -u$USER -p$PASS -S $SOCK"

DUMP="mysqldump -u$USER -poldboy -S $SOCK"

DATABASE=$($LOGIN -e "show databases;"|sed 1d|grep -Ev "*_schema|mysql")

for database in $DATABASE

do

TABLES=$($LOGIN -e "use $database;show tables;"|sed 1d)

for tables in $TABLES

do

[ -d /opt/$database ]||mkdir -p /opt/$database

$DUMP $database $TABLES |gzip > /opt/$database/${database}_${tables}_$(date +%F).sql.gz

done

done

16.对mysql实现分库备份

[root@mysql-01 ~]# cat 002.sh

#!/bin/bash

USER=root

PASS=oldboy

SOCK=/data/3306/mysql.sock

LOGIN="mysql -u$USER -p$PASS -S $SOCK"

DUMP="mysqldump -u$USER -poldboy -S $SOCK"

DATABASE=$($LOGIN -e "show databases;"|sed 1d|grep -Ev "*_schema|mysql")

for database in $DATABASE

do

$DUMP $database -B |gzip > /opt/${database}_${tables}_$(date +%F).sql.gz

done

 

 

 


17.开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf &
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。

 

[root@mysql-01 3306]# vim mysql

#!/bin/sh

#init

port=3306

mysql_user="root"

mysql_pwd="oldboy"

CmdPath="/application/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

if [ ! -e "$mysql_sock" ];then

printf "Starting MySQL...\n"

/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

else

printf "MySQL is running...\n"

exit

fi

}

#stop function

function_stop_mysql()

{

if [ ! -e "$mysql_sock" ];then

printf "MySQL is stopped...\n"

printf "MySQL is stopped...\n"

exit

else

printf "Stoping MySQL...\n"

${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

#!/bin/sh

#init

port=3306

mysql_user="root"

mysql_pwd="oldboy"

CmdPath="/application/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

if [ ! -e "$mysql_sock" ];then

printf "Starting MySQL...\n"

/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

else

printf "MySQL is running...\n"

exit

fi

}

#stop function

function_stop_mysql()

/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

else

printf "MySQL is running...\n"

exit

fi

}

#stop function

function_stop_mysql()

{

if [ ! -e "$mysql_sock" ];then

printf "MySQL is stopped...\n"

exit

else

printf "Stoping MySQL...\n"

${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

exit

fi

}

#stop function

function_stop_mysql()

{

if [ ! -e "$mysql_sock" ];then

printf "MySQL is stopped...\n"

exit

else

printf "Stoping MySQL...\n"

${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

fi

}

#restart function

function_restart_mysql()

{

printf "Restarting MySQL...\n"

function_stop_mysql

sleep 2

function_start_mysql

}

case $1 in

start)

function_start_mysql

;;

stop)

function_stop_mysql

;;

restart)

function_restart_mysql

;;

*)

printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

esac

18.企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)

[root@oldboy C13]# cat 13_6_3.sh

#!/bin/bash

###########################################

# this script function is :

# check_mysql_slave_replication_status

############################################

path=/server/scripts

MAIL_GROUP="1111@qq.com 2222@qq.com"

PAGER_GROUP="18600338340 18911718229"

LOG_FILE="/tmp/web_check.log"

USER=root

PASSWORD=oldboy123

PORT=3307

MYSQLCMD="mysql -u$USER -p$PASSWORD -S /data/$PORT/mysql.sock"

error=(1008 1007 1062)

RETVAL=0

[ ! -d $path ] && mkdir -p $path

function JudgeError(){

for((i=0;i<${#error[*]};i++))

do

if [ "$1" == "${error[$i]}" ]

then

echo "MySQL slave errorno is $1,auto repairing it."

$MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"

fi

done

return $1

}

function CheckDb(){

status=($(awk -F ': ' '/_Running|Last_Errno|_Behind/{print $NF}' slave.log))

expr ${status[3]} + 1 &>/dev/null

if [ $? -ne 0 ];then

status[3]=300

fi

if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a ${status[3]} -lt 120 ]

then

#echo "Mysql slave status is ok"

return 0

else

#echo "mysql replcation is failed"

JudgeError ${status[2]}

fi

}

function MAIL(){

local SUBJECT_CONTENT=$1

for MAIL_USER  in `echo $MAIL_GROUP`

do

mail -s "$SUBJECT_CONTENT " $MAIL_USER <$LOG_FILE

done

}

function PAGER(){

for PAGER_USER  in `echo $PAGER_GROUP`

do

TITLE=$1

CONTACT=$PAGER_USER

HTTPGW=http://oldboy.sms.cn/smsproxy/sendsms.action

#send_message method1

curl -d  cdkey=5ADF-EFA -d password=OLDBOY -d phone=$CONTACT -d message="$TITLE[$2]" $HTTPGW

done

}

function SendMsg(){

if [ $1 -ne 0 ]

then

RETVAL=1

NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"`

SUBJECT_CONTENT="mysql slave is error,errorno is $2,${NOW_TIME}."

echo -e "$SUBJECT_CONTENT"|tee $LOG_FILE

MAIL $SUBJECT_CONTENT

PAGER $SUBJECT_CONTENT $NOW_TIME

else

echo "Mysql slave status is ok"

RETVAL=0

fi

return $RETVAL

}

function main(){

while true

do

CheckDb

SendMsg $?

sleep 300

done

}

main

19.将域名取出,并根据域名进行计数排序处理。

[root@fengxiaoli ~]# cat yuming.txt

http://a.example.com/1.html

http://b.example.com/1.html

http://c.example.com/1.html

http://a.example.com/2.html

http://b.example.com/2.html

http://a.example.com/3.html

[root@fengxiaoli ~]# cat yuming.txt |awk -F "/" '{print $3}'|sort|uniq -c

3 a.example.com

2 b.example.com

1 c.example.com

[root@fengxiaoli ~]# cat yuming.txt|awk  -F / '{S[$3]++}END{for(k in S)print k,S[k]}'|sort

a.example.com 3

b.example.com 2

c.example.com 1

[root@fengxiaoli ~]# cat yuming.txt|awk  -F / '{++S[$3]}END{for(k in S)print k,S[k]}'|sort

a.example.com 3

b.example.com 2

c.example.com 1

 

20.统计服务器上连接状态数量

[root@fengxiaoli ~]# netstat -n |awk '/^tcp/ {print $NF}' |sort |uniq -c|sort

[root@fengxiaoli ~]# netstat -n |awk '/^tcp/ {++S[$NF]} END {for(k in S) print k, S[k]}'

TIME_WAIT 9137

CLOSE_WAIT 207

FIN_WAIT1 547

ESTABLISHED 597

FIN_WAIT2 74

SYN_RECV 70

CLOSING 55

LAST_ACK 8

21.分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小

说明:这个功能可以用于IDC及CDN网站流量带宽很高,然后通过分析服务器日志哪些元素占用流量过大,进而进行优化裁剪该图片,压缩js等措施。

本题需要输出三个指标: 【访问次数】    【访问次数*单个文件大小】   【文件名(可以带URL)】

[root@fengxiaoli ~]# cat fs.txt

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)"

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)"

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)"

124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-"

124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-"

124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"

法一:

[root@fengxiaoli ~]# awk '{print $7"\t" $10}' fs.txt |sort |uniq -c|awk '{print $1*$3,$1,$2}'|sort -rn|head

46232 1 /?=

22598 2 /static/images/photos/2.jpg

4460 1 /static/js/web_js.js

3583 1 /static/flex/vedioLoading.swf

1627 1 /static/js/jquery.lazyload.js

法二:

通过两个数组来计算

因为我们要的最终结果是某个文件的访问次数和消耗的流量,所以考虑建立以文件名为索引的两个数组,一个存储访问次数,一个保存消耗的流量,这样当使用awk按行遍历文件时,对次数数组+1,同时对流量数组进行文件大小的累加,等文件扫描完成,再遍历输出两个数组既可以得到该文件的反问次数和总的流量消耗。

[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

46232 1 /?=

22598 2 /static/images/photos/2.jpg

4460 1 /static/js/web_js.js

3583 1 /static/flex/vedioLoading.swf

1627 1 /static/js/jquery.lazyload.js

本文转自 fxl风 51CTO博客,原文链接:http://blog.51cto.com/fengxiaoli/1952464

shell编程脚本练习题相关推荐

  1. 常见Shell编程脚本

    常见Shell编程脚本 一.Linux运维监控相关 1.创建 Linux 系统账户及密码 #!/bin/bash # 通过位置变量创建 Linux 系统账户及密码 #$1 是执行脚本的第一个参数,$2 ...

  2. linux编程 —— shell编程脚本常用语法总结 【学习笔记】

    文档声明: 以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正.并且该文档在后期会随着学习的深入不断补充完善.感谢各位的参考查看. 笔记资料仅供学习交流使用,转载请标明出处 ...

  3. Shell编程~脚本cheo命令

    Shell脚本命令 echo命令解释 echo命令解释 echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用,在终端下打 ...

  4. 【linux】shell编程 脚本语法

    1.对shell的认识 shell是linux中一个重要的层次,他是用户与系统交互作用的界面.最常见的使用方式:在介绍linux命令时,shell都是作为命令解释程序出现:他接收用户打入的命令,进行分 ...

  5. linux运维脚本编写,最强Linux自动化运维 Shell高级脚本编程实战 带习题+项目实战案例+全套配置脚本...

    最强Linux自动化运维 Shell高级脚本编程实战 带习题+项目实战案例+全套配置脚本 大家可以通过参考下面的课程学习目录,就会发现单单只从目录上来分析就知道这是一部非常系统的Shell自动化脚本运 ...

  6. Linux高级专题详解--shell编程大全(shell变量,if语句,case语句,for循环,while循环,函数调用,数组,正则表达式,shell脚本三剑客--grep,sed,awk家族)

    shell编程 初始shell 程序 语言 编程 ---------------------------------- 语言 自然语言:汉语.英语 计算机语言:c语言.c++.(java php py ...

  7. Linux网络服务与shell脚本——Shell编程之条件语句

    第八章 Shell编程之条件语句 一.条件测试 1.测试命令 (1)test 条件表达式 (2)[条件表达式] (3)$?:根据返回值判断前者是否成立 2.文件测试:根据指定路径名称,判断对应文件或目 ...

  8. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)Shell脚本与MySQL数据库交互(增删改查)# 环境准备:安装mariadb 数据库 [root ...

  9. linux Shell(脚本)编程入门实例讲解详解

    linux Shell(脚本)编程入门实例讲解详解 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集, ...

最新文章

  1. php解决链接 amp,php处理替换链接参数
  2. UVA 10254 The Priest Mathematician
  3. 超形象!著名的三角不等式演示动图!
  4. matlab作业1参考答案,matlab课后习题答案1到6章
  5. 数据库去重查询问题详解
  6. 修改Tomcat端口号
  7. SpatiaLite空间索引(一)
  8. c盘能达到1T吗,为什么?
  9. diff 命令用法--如何打补丁【原创--学习笔记】
  10. Windows窗体编程基础学习:更改TabControl 的外观(如qq用的)
  11. matplotlib.pyplot库解析
  12. 好用的图吧工具云资源
  13. java实现查询Word是否包含批注和修订内容
  14. 无涯教程: Laravel 8 - Excel和CSV介绍
  15. 国赛学习——5种数学规划模型
  16. 2022年1月语音合成(TTS)和语音识别(ASR)论文月报
  17. 关于AndroidStudio打包后apk包名乱码的问题
  18. java中的udp丢包_udp丢包 处理
  19. Altium Designer 21 原理图库元件模型的组成介绍以及简单的电阻电容元件模型的创建
  20. 芯片行业名词简写——来自实操小白经验积累1.0

热门文章

  1. “方法X对于类型Y是模糊的” Java模糊方法调用null错误
  2. 函数调用的汇编语言详解
  3. 开展人力资源数据分析的目的和原因
  4. C++基础知识:fflush(stdin)的误区
  5. 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化,而报【远程服务器返回错误: (500) 内部服务器错误】...
  6. ansible软件模块参数
  7. Java Web之Cookie和Session的理解
  8. HTML 代码复用实践 (静态页面公共部分提取复用)
  9. nyoj349 poj1094 Sorting It All Out(拓扑排序)
  10. 宽字节与多字节之间的转换