shell编程脚本练习题

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

1.使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10html文件,名称例如为:


[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=(echoRANDOM|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=(echoRANDOM|md5sum |cut -c 2-11)

useradd $user && \

echo "pass"|passwdstdinuser &>/dev/null

resut=$?

if [ $resut -eq 0 ]

then

echo "$user create succss"

fi

echo "account=userpassword=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=(echoRANDOM|md5sum |cut -c 2-11);echo "pass"|passwdstdin\1;echo"\1"\tpass>>/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

cmdip$i &>/dev/null

if [ $? -eq 0 ]

 then

echo "ipi is ok!"

 else

echo "ipi 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"|awkprint$1geaccount -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{print1}'

法二:

[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]||[zb ];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 [ agtb ];then

echo "a>b"

exit 0

elif [ altb ];then

echo "a<b"

exit 0

else

echo "a=b"

exit 0

fi

 

脚本传参方式只需将ab 替换会12即可

 

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=(wgetspidertimeout=3tries=2n &>/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]||touchfile

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

rename=(grep"\bname\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=(exprRANDOM % 99 + 1)

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

if [ $zhua_num -ne 1 ];then

echo "nameran_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=(echoi|md5sum |cut -c 1-8)

for n in ${array[*]}

do

if [ md5==n ];then

echo "iisn"

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"typefexecmd5sum>md5_log \;

find "path"typef>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=(catnum_log|wc -l)

while true

do

resultlog=/test/result.log

[ ! -f resultlog ] && touchresultlog

md5_check=(md5sumcmd5_log 2>/dev/null |grep FAILED|wc -l)

new_num=(findpath -type f|wc -l)

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


if [ md5checkne0]||[new_num -ne $num ];then

  echo "(md5sumcmd5_log 2>/dev/null | grep FAILED)" >$resultlog

  diff numlog/test/numnew.log>>resultlog

#  mail -s "web site is changed in (date+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=((ni));j>0;j--))

    do

     echo -n " "

    done

    

    for((m=0;m<((2i-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 [ meqn ];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 lampaxlamp ]||{

      echo "$lamp is error!"

      exit 1

      }

echo "startinstalling lamp..."

$lamp

echo "lamp installed..."

;;

 

2)

[ -f lnmpaxlnmp ]||{

        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 -uUSERpPASS -S $SOCK"

DUMP="mysqldump -uUSERpoldboySSOCK"  

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]||mkdirp/opt/database

DUMPdatabase 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 -uUSERpPASS -S $SOCK"

DUMP="mysqldump -uUSERpoldboySSOCK"  

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

for database in $DATABASE

do

DUMPdatabase -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/mysqldsafedefaultsfile=/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/mysqladminu{mysql_user} -pmysqlpwdS/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/mysqldsafedefaultsfile=/data/{port}/my.cnf 2>&1 > /dev/null &

    else

      printf "MySQL is running...\n"

      exit

    fi

}

 

#stop function

function_stop_mysql()

      /bin/sh CmdPath/mysqldsafedefaultsfile=/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/mysqladminu{mysql_user} -pmysqlpwdS/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/mysqladminu{mysql_user} -pmysqlpwdS/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 -uUSERpPASSWORD -S /data/$PORT/mysql.sock"

error=(1008 1007 1062)

RETVAL=0

[ ! -d path ] && mkdir -ppath

 

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/{printNF}' 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 "SUBJECTCONTENT"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=CONTACTdmessage="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 "SUBJECTCONTENT"|teeLOG_FILE

       MAIL $SUBJECT_CONTENT

       PAGER SUBJECTCONTENTNOW_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 13,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]++;arraysize[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





相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
344
分享
相关文章
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
|
2月前
|
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
131 32
|
6月前
|
一个用于添加/删除定时任务的shell脚本
一个用于添加/删除定时任务的shell脚本
185 1
多种脚本批量下载 Docker 镜像:Shell、PowerShell、Node.js 和 C#
本项目提供多种脚本(Shell、PowerShell、Node.js 和 C#)用于批量下载 Docker 镜像。配置文件 `docker-images.txt` 列出需要下载的镜像及其标签。各脚本首先检查 Docker 是否安装,接着读取配置文件并逐行处理,跳过空行和注释行,提取镜像名称和标签,调用 `docker pull` 命令下载镜像,并输出下载结果。使用时需创建配置文件并运行相应脚本。C# 版本需安装 .NET 8 runtime。
158 2
6种方法打造出色的Shell脚本
6种方法打造出色的Shell脚本
144 2
6种方法打造出色的Shell脚本
|
5月前
|
Shell脚本要点和难点以及具体应用和优缺点介绍
Shell脚本在系统管理和自动化任务中扮演着重要角色。尽管存在调试困难、可读性差等问题,但其简洁高效、易于学习和强大的功能使其在许多场景中不可或缺。通过掌握Shell脚本的基本语法、常用命令和函数,并了解其优缺点,开发者可以编写出高效的脚本来完成各种任务,提高工作效率。希望本文能为您在Shell脚本编写和应用中提供有价值的参考和指导。
215 1
ubuntu/debian shell 脚本自动配置 gitea git 仓库
这是一个自动配置 Gitea Git 仓库的 Shell 脚本,支持 Ubuntu 20+ 和 Debian 12+ 系统。脚本会创建必要的目录、下载并安装 Gitea,创建 Gitea 用户和服务,确保 Gitea 在系统启动时自动运行。用户可以选择从官方或小绿叶技术博客下载安装包。
200 2
ip和ip网段攻击拦截系统-绿叶结界防火墙系统shell脚本
这是一个名为“小绿叶技术博客扫段攻击拦截系统”的Bash脚本,用于监控和拦截TCP攻击。通过抓取网络数据包监控可疑IP,并利用iptables和firewalld防火墙规则对这些IP进行拦截。同时,该系统能够查询数据库中的白名单,确保合法IP不受影响。此外,它还具备日志记录功能,以便于后续分析和审计。
128 6
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等